Motor led RGB con atmega328

12
UNIVERSIDAD NACIONAL DE CHIMBORAZO FACULTAD DE INGENIERÍA ESCUELA DE ELECTRÓNICA Y TELECOMUNICACIONES CATEDRA MICROPROCESADORES Y LAB. PROYECTO FINAL DE MICROPROCESADORES Héctor Cajilema [email protected] Javier LoLópez [email protected] Tema: Motor Led RGB, Sensor de Temperatura mediante Atmel Studio 6.2 y Arduino con el Atmega 328. Resumen—En este informe muestra el diseño e implementación del motor led con RGB de la palabra PARADA que será programado mediante el software AVRStudio 6.2, también se realizara un sensor de temperatura mediante Arduino y AVR Studio los tres serán programados con el Atmega 328. A. OBJETIVO GENERAL Realizar Un Motor Led, Sensor De Temperatura Mediante Atmel Studio 6.2 Y Arduino Con El Atmega 328. B. OBJETIVOS ESPECIFICOS. Realizar la respectiva programación mediante el hardware AVR Studio 6.2. Probar la programación en el simulador Proteus. Quemar y comprobar en el pic Atmega328 el correcto funcionamiento de la programación y simulación. Proceder a sacar las respectivas conclusiones. I. MARCO TEORICO PIC ATmega328P

description

Motor led RGB con pic atmega328 en visual studio, tambien un sensor de temperature en arduino.

Transcript of Motor led RGB con atmega328

Page 1: Motor led RGB con atmega328

UNIVERSIDAD NACIONAL DE CHIMBORAZO

FACULTAD DE INGENIERÍAESCUELA DE ELECTRÓNICA Y TELECOMUNICACIONES

CATEDRA MICROPROCESADORES Y LAB.

PROYECTO FINAL DE MICROPROCESADORES Héctor Cajilema

[email protected] LoLópez

[email protected]

Tema: Motor Led RGB, Sensor de Temperatura mediante Atmel Studio 6.2 y Arduino con el Atmega 328.

Resumen—En este informe muestra el diseño e implementación del motor led con RGB de la palabra PARADA que será programado mediante el software AVRStudio 6.2, también se realizara un sensor de temperatura mediante Arduino y AVR Studio los tres serán programados con el Atmega 328.

A. OBJETIVO GENERAL

Realizar Un Motor Led, Sensor De Temperatura Mediante Atmel Studio 6.2 Y Arduino Con El Atmega 328.

B. OBJETIVOS ESPECIFICOS.

Realizar la respectiva programación mediante el hardware AVR Studio 6.2.Probar la programación en el simulador Proteus.Quemar y comprobar en el pic Atmega328 el correcto funcionamiento de la programación y simulación.Proceder a sacar las respectivas conclusiones.

I. MARCO TEORICO

PIC ATmega328P

Microcontrolador de 8 bits basados en los procesadores AVR., 23 pines I/O disponibles, memoria de programa flash 32 kB, SRAM 2 kB, EEPROM de datos 1 kB, ADC de 10 bits y 6 canales, 3 timers/comparadores/contadores, 6 canales PWM, programador de modo USART, SPI puerto serial, TWI (I2C compatible), comparador análogo, "watchdog timer" programable con oscilador interno, y cinco modos de ahorro de energía seleccionables por software.

Page 2: Motor led RGB con atmega328

Figure 1_Pic Atmega328

El dispositivo opera entre 1.8 y 5.5 voltios. Por medio de la ejecución de poderosas instrucciones en un solo ciclo de reloj, el dispositivo alcanza una respuesta de 1 MIPS, balanceando consumo de energía y velocidad de proceso.

Figure 2_Puertos

Registros del CPU

Cada registro 4ene asignada una dirección en memoria.Se utilizan las primeras 32 localidades del espacio de memoria para datos.Los registros X, Y y Z se utilizan también como apuntadores de 16 bits.

Características eléctricas

Arquitectura RISC

Operación entre 1.8 y 5.5 voltios

131 instrucciones

32 registros de propósito general

1 Kbyte de EEPROM

Protección de código programable

Cumple directivas RoHS

Encapsulado PDIP de 28 pines

Figure 3_Mikroc

LCD

Figure 4_lcd

Las siglas LCD significan “Liquid Cristal Display” ó pantalla de cristal líquido. Es una pantalla plana basada en el uso de una

Page 3: Motor led RGB con atmega328

sustancia liquida atrapada entre dos placas de vidrio, haciendo pasar por este una corriente eléctrica a una zona específica, para que así esta se vuelva opaca, y además cuenta (generalmente) con iluminación trasera.

Figure 5_carateristicas_LCD

II. PROCEDIMIENTO

a) PROGRAMACION

AVR STUDIO 6.2

Programación En Ensamblador Del Motor Led Con La Palabra ‘PARADA’.

.include <m328def.inc>

.def Temp1 = r17

.def Temp2 = r18

.def Temp3 = r19

.def data = r20

.def byte = r21

.equ INPUT = 0x00 ;

.equ OUTPUT = 0xFF ;

.org 0x0000

rjmp RESET ; Reset Handler

retiloop_for_ever:rjmp loop_for_ever

RESET1:ldi r16, high(RAMEND); Main program startout SPH,r16 ; Set Stack Pointer to top of RAMldi r16, low(RAMEND)out SPL,r16sei ; Enable interrupts

;--------------------------------------------------------------delay500ms:;-*****CALCULADO 8MHZ*****************

; delay loop generator ; 80000 cycles:; ----------------------------- ; delaying 7998 cycles: ldi temp1, $1FWGLOOP0: ldi temp2, $55

call delay500msldi temp1,0b0001001out portb,temp1call delay500msldi temp1,0b0001001out portb,temp1call delay500msldi temp1,0b1111110out portb,temp1call delay500ms

;Rcall delay500msldi temp1,0b1111110out portb,temp1call delay500msldi temp1,0b0010001out portb,temp1call delay500msldi temp1,0b0010001out portb,temp1call delay500msldi temp1,0b0101001out portb,temp1call delay500msldi temp1,0b1000110out portb,temp1call delay500msldi temp1,$00out portb,temp1call delay500mscall delay500ms

WGLOOP1: dec temp2 brne WGLOOP1 dec temp1 brne WGLOOP0; ----------------------------- ; delaying 2 cycles: nop nop; =============================

ret;--------------------------------------------------------------

RESET:ldi Temp1, low(RAMEND)out SPL, Temp1ldi Temp1, high(RAMEND)out SPH, Temp1ldi temp1,0xFFout DDRB,temp1

out DDRC,temp1out DDRD,temp1sei;********************************************; Bucle Principal;********************************************final:;Pcall delay500msldi temp1,0b1111110out portb,temp1call delay500msldi temp1,0b0010001out portb,temp1call delay500msldi temp1,0b0010001out portb,temp1call delay500msldi temp1,0b0010001out portb,temp1call delay500msldi temp1,0b0001110out portb,temp1call delay500msldi temp1,$00out portb,temp1call delay500mscall delay500ms

;Acall delay500msldi temp1,0b1111110out portb,temp1call delay500msldi temp1,0b0001001out portb,temp1ldi temp1,$00out portb,temp1call delay500mscall delay500ms

;Acall delay500msldi temp1,0b1111110out portb,temp1call delay500msldi temp1,0b0001001out portb,temp1call delay500msldi temp1,0b0001001out portb,temp1call delay500msldi temp1,0b0001001out portb,temp1call delay500msldi temp1,0b1111110out portb,temp1call delay500msldi temp1,$00out portb,temp1call delay500mscall delay500ms

;Dcall delay500msldi temp1,0b1111111out portb,temp1call delay500msldi temp1,0b1000001out portb,temp1call delay500msldi temp1,0b1000001out portb,temp1call delay500msldi temp1,0b0100010out portb,temp1call delay500msldi temp1,0b0011100out portb,temp1call delay500msldi temp1,$00out portb,temp1call delay500mscall delay500ms

;Acall delay500msldi temp1,0b1111110out portb,temp1call delay500msldi temp1,0b0001001out portb,temp1call delay500msldi temp1,0b0001001out portb,temp1call delay500msldi temp1,0b0001001out portb,temp1call delay500msldi temp1,0b1111110out portb,temp1call delay500msldi temp1,$00out portb,temp1call delay500mscall delay500ms

Programación En AVR Studio6.2 Mikroc Del Sensor De Temperatura.

Page 4: Motor led RGB con atmega328

/* * LCD_4.c * * Created: 31/07/2015 19:46:18 * Author: User */#define F_CPU 16000000UL

#include <avr/io.h>#include <avr/interrupt.h>#include <util/delay.h>

// LCD interface (should agree with the diagram above)// make sure that the LCD RW pin is connected to GND#define lcd_D7_port PORTD // lcd D7 connection#define lcd_D7_bit PORTD7#define lcd_D7_ddr DDRD

#define lcd_D6_port PORTD // lcd D6 connection#define lcd_D6_bit PORTD6#define lcd_D6_ddr DDRD

#define lcd_D5_port PORTD // lcd D5 connection#define lcd_D5_bit PORTD5#define lcd_D5_ddr DDRD

#define lcd_D4_port PORTD // lcd D4 connection#define lcd_D4_bit PORTD4#define lcd_D4_ddr DDRD

#define lcd_E_port PORTD // lcd Enable pin#define lcd_E_bit PORTD1#define lcd_E_ddr DDRD

#define lcd_RS_port PORTD // lcd Register Select pin#define lcd_RS_bit PORTD0#define lcd_RS_ddr DDRD

// LCD module information#define lcd_LineOne 0x00 // start of line 1#define lcd_LineTwo 0x40 // start of line 2

// LCD instructions#define lcd_Clear 0b00000001 // replace all characters with ASCII 'space'#define lcd_Home 0b00000010 // return cursor to first position on first line#define lcd_EntryMode 0b00000110 // shift cursor from left to right on read/write#define lcd_DisplayOff 0b00001000 // turn display off#define lcd_DisplayOn 0b00001100 // display on, cursor off, don't blink character#define lcd_FunctionReset 0b00110000 // reset the LCD#define lcd_FunctionSet4bit 0b00101000 // 4-bit data, 2-line display, 5 x 7 font#define lcd_SetCursor 0b10000000 // set cursor position//****************************************************************#include <avr/io.h>#define PORT_ON(port,pin) port |= (1<<pin)#define PORT_OFF(port,pin) port &= ~(1<<pin)

// Program IDuint8_t visual1[] = "SEN TEMPERATURA"; //***********************uint8_t visual2[] = " REF:25 GRADOS";

uint8_t ch ;

// Function Prototypesvoid lcd_write_4(uint8_t);void lcd_write_instruction_4d(uint8_t);void lcd_write_character_4d(uint8_t);void lcd_write_string_4d(uint8_t *);void lcd_init_4d(void);

/******************************* Main Program Code *************************/int main(void){

// configure the microprocessor pins for the data lines lcd_D7_ddr |= (1<<lcd_D7_bit); // 4 data lines - output

// Reset the LCD controller lcd_write_4(lcd_FunctionReset); // first part of reset sequence

lcd_write_4(lcd_FunctionReset); // second part of reset sequence _delay_us(200); // 100uS delay (min)

lcd_write_4(lcd_FunctionReset); // third part of reset sequence _delay_us(200); // this delay is omitted in the data sheet

lcd_write_4(lcd_FunctionSet4bit); // set 4-bit mode _delay_us(80); // 40uS delay (min)

// Function Set instruction lcd_write_instruction_4d(lcd_FunctionSet4bit); // set mode, lines, and font _delay_us(80); // 40uS delay (min)

// lcd_write_instruction_4d(lcd_DisplayOff); // turn display OFF _delay_us(80); // 40uS delay (min)

// Clear Display instruction lcd_write_instruction_4d(lcd_Clear); // clear display RAM _delay_ms(4); // 1.64 mS delay (min)

// ; Entry Mode Set instruction lcd_write_instruction_4d(lcd_EntryMode); // set desired shift characteristics _delay_us(80); // 40uS delay (min)

// This is the end of the LCD controller initialization as specified in the data sheet, but the display// has been left in the OFF condition. This is a good time to turn the display back ON. // Display On/Off Control instruction lcd_write_instruction_4d(lcd_void lcd_init_4d(void){// Power-up delay _delay_ms(100); // initial 40 mSec delay

// Set up the RS and E lines for the 'lcd_write_4' subroutine. lcd_RS_port &= ~(1<<lcd_RS_bit); // select the Instruction Register (RS low) lcd_E_port &= ~(1<<lcd_E_bit); // make sure E is initially low

_delay_ms(10); // 4.1 mS delay (min)

DisplayOn); // turn the display ON _delay_us(80); // 40uS delay (min)}

void lcd_write_string_4d(uint8_t theString[]){

lcd_D6_ddr |= (1<<lcd_D6_bit); lcd_D5_ddr |= (1<<lcd_D5_bit); lcd_D4_ddr |= (1<<lcd_D4_bit);

// configure the microprocessor pins for the control lines lcd_E_ddr |= (1<<lcd_E_bit); // E line - output lcd_RS_ddr |= (1<<lcd_RS_bit); // RS line - output

// initialize the LCD controller as determined by the defines (LCD instructions) lcd_init_4d(); // initialize the LCD display for a 4-bit interface

// display the first line of information lcd_write_string_4d(visual1); //******************11111111111111// set cursor to start of second linelcd_write_instruction_4d(lcd_SetCursor | lcd_LineTwo);_delay_us(80); // 40 uS delay (min)

// display the second line of information lcd_write_string_4d(visual2);

lcd_write_instruction_4d(lcd_SetCursor | lcd_LineTwo);

_delay_us(80);lcd_write_character_4d(ch);

// endless loop///*********************ADC*******************************//**********************ADC******************************** unsigned int adc_value; // Variable to hold ADC result long tlong; DDRB=0xff; // Set Port D as Output PORTB = 0x00; ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS0); // ADEN: Set to turn on ADC , by default it is turned off //ADPS2: ADPS2 and ADPS0 set to make division factor 32 ADMUX=0x05; // ADC input channel set to PC5

while (1) {

ADCSRA |= (1<<ADSC); // Start conversion

while (ADCSRA & (1<<ADSC)); // wait for conversion to complete

adc_value = ADCW; //Store ADC value

tlong = (long)adc_value * 1000; tlong = tlong / 1023;

if ( tlong <= 50) {

PORT_ON (PORTB,6); //**ENCIENDE FOCO****

PORT_OFF(PORTB,7); // ***APAGA VENTILADOR***

} else {

PORT_ON(PORTB,7); // ***ENCIENDE VENTILADOR***

volatile int i = 0; // character counter*/ while (theString[i] != 0) { lcd_write_character_4d(theString[i]); i++; _delay_us(80); // 40 uS delay (min) }}

void lcd_write_character_4d(uint8_t theData){ lcd_RS_port |= (1<<lcd_RS_bit); // select the Data Register (RS high) lcd_E_port &= ~(1<<lcd_E_bit); // make sure E is initially low lcd_write_4(theData); // write the upper 4-bits of the data lcd_write_4(theData << 4); // write the lower 4-bits of the data}

void lcd_write_instruction_4d(uint8_t theInstruction){ lcd_RS_port &= ~(1<<lcd_RS_bit); // select the Instruction Register (RS low) lcd_E_port &= ~(1<<lcd_E_bit); // make sure E is initially low lcd_write_4(theInstruction); // write the upper 4-bits of the data lcd_write_4(theInstruction << 4); // write the lower 4-bits of the data}

void lcd_write_4(uint8_t theByte){ lcd_D7_port &= ~(1<<lcd_D7_bit); // assume that data is '0' if (theByte & 1<<7) lcd_D7_port |= (1<<lcd_D7_bit); // make data = '1' if necessary

lcd_D6_port &= ~(1<<lcd_D6_bit); // repeat for each data bit if (theByte & 1<<6) lcd_D6_port |= (1<<lcd_D6_bit);

lcd_D5_port &= ~(1<<lcd_D5_bit); if (theByte & 1<<5) lcd_D5_port |= (1<<lcd_D5_bit);

lcd_D4_port &= ~(1<<lcd_D4_bit); if (theByte & 1<<4) lcd_D4_port |= (1<<lcd_D4_bit);

// write the data

// 'Address set-up time' (40 nS) lcd_E_port |= (1<<lcd_E_bit); // Enable pin high _delay_us(1); // implement 'Data set-up time' (80 nS) and 'Enable pulse width' (230 nS) lcd_E_port &= ~(1<<lcd_E_bit); // Enable pin low _delay_us(1); // implement 'Data hold time' (10 nS) and 'Enable cycle time' (500 nS)

Page 5: Motor led RGB con atmega328

PORT_OFF (PORTB,6); //**APAGA FOCO****

} }

while(1); return 0;}

/******************************* End of Main Program Code ******************/

/*============================== 4-bit LCD Functions ======================*//* Name: lcd_init_4d Purpose: initialize the LCD module for a 4-bit data interface Entry: equates (LCD instructions) set up for the desired operation Exit: no parameters Notes: uses time delays rather than checking the busy flag*/

}

Programación En Arduino Del Sensor De Temperatura.

//***SENSOR TEMPERATURA*******************

#include <LiquidCrystal.h>

int foco=13;int motor=12;

int var_pot = A2;int var_temp = A1;char comp;float pot = 0;float tem = 0; float var1 = 1;float var2=0;

LiquidCrystal lcd(7, 6, 5, 4, 3, 2); void setup(){ pinMode(foco, OUTPUT); pinMode(motor, OUTPUT);

lcd.begin(16, 2); Serial.begin(9600); ///LEER DATOS....TRANS*****************

} void loop () { pot = analogRead(var_pot); tem = analogRead(var_temp); var1=(5*tem)/1024; var2=(5*pot)/1024;

comp=Serial.read(); switch (comp){case'A':digitalWrite(foco,LOW);digitalWrite(motor,LOW);

break;case'M'://digitalWrite(led,LOW);digitalWrite(motor,HIGH);if (pot<tem) { digitalWrite(foco,HIGH); //digitalWrite(motor,LOW); } else { digitalWrite(foco,LOW); //digitalWrite(motor,HIGH); }break;case 'F':digitalWrite(foco,HIGH);//digitalWrite(motor,LOW);

if (pot<tem) { //digitalWrite(led,HIGH); digitalWrite(motor,LOW); } else { //digitalWrite(led,LOW); digitalWrite(motor,HIGH); }break;

}

lcd.setCursor(0,0);lcd.print("VOL_REF ");lcd.print("VOL_TEMP ");

lcd.setCursor(1,1);lcd.print((float)var1);

lcd.setCursor(9,9);lcd.print(var2); }

SIMULACION PROTEUS

Como Se Puede Apreciar En La Fig.6 Tenemos El Diagrama De Nuestro Circuito En Proteus Del Motor Led.

Figure 6

Page 6: Motor led RGB con atmega328

Figure 7

En La Fig.8 Tenemos El Sensor De Temperatura Programado En AVR Studio Mikroc.

Figure 8

Figure 9

Figure 10

Figure 11

Tenemos Nuestra SIMULACION En Arduino De Nuestro Sensor De Temperatura.

Figure 12

Page 7: Motor led RGB con atmega328

Figure 13

Figure 14

CIRCUITO ARMADO

Figure 15

Figure 16

Figure 17

Figure 18

Figure 19

Figure 20

Page 8: Motor led RGB con atmega328

Figure 21

Figure 22

II. CONCLUSIONES

La programación de nuestro motor led en ensamblador funciono correctamente conforme a lo requerido.

Para una mejor apreciación de nuestra palabra PARADA, tuvimos que calibrar bien el tiempo a 500ms.

Tuvimos que instalar unas librerías del LCD para poderlo utilizar, ya uqe sin ellas no funciona.

En la programación en Mikroc del sensor de temperatura tuvimos complicaciones ya que algunas librerías no nos funcionaban.

En la comunicación serial programado en Arduino hay que habilitar los puertos del RX y TX para poderlos utilizar.

III. RECOMMENDACIONES

Instalar los drivers del quemador del Atmega 328 en una PC de 32bits ya que en la de 64bits no funciona.

Se recomienda utilizar cuidadosamente el LCD debido a que si se hace un corto se quemaría.

Recomendamos simular antes de armar el circuito del filtro.

Ubicar correctamente el Pic Atmega 328 al momento de quemar para evitar la pérdida del mismo.

No sobrepasar el voltaje del Pic ya que si se lo hace se echaría a perder.

IV. BIOGRAFIA

HECTOR CAJILEMAEstudiante de la Escuela de Ingeniería en Electrónica y

Telecomunicaciones de la Universidad Nacional de Chimborazo (Riobamba), desde el año 2010-2011 graduado como Bachiller Técnico en Electrónica de Consumo en el Instituto Tecnológico Superior Carlos Cisneros.

JAVIER LÓPEZ Estudiante de la Escuela de Ingeniería en Electrónica y Telecomunicaciones de la Universidad Nacional de Chimborazo (Riobamba), desde el año 2011-2012 graduado como Bachiller Técnico en

Page 9: Motor led RGB con atmega328

Electrónica en el Instituto Tecnológico Superior Carlos Cisneros.

V. REFERENCIAS

www.youtube.com/watch?v=d_RL5g-bzGIproyectoaula-pic16f628a.blogspot.com/proyectoselectronicoos.blogspot.com/2013/01/matriz-de-leds-7x5.htmlhttps://es.wikipedia.org/wiki/Atmega328www.atmel.com/images/doc8161.pdfyori.mxl.uabc.mx/wp-content/plugins/download.../download.php?id=32http://www.mikroe.com/chapters/view/82/capitulo-4-ejemplos/#c4v12

VI. ANEXOS

DATASHEEF

PLACAS DEL MOTOR LED RGB

Page 10: Motor led RGB con atmega328