Informe _8 Laboratorio de Microcontroladores

18
17-1-2014 LCD y Teclado Laboratorio de Microcontroladores Integrantes Alexander Edward Merejildo Tomalá Marcela Pérez Pereira ESCUELA SUPERIOR POLITÉCNICA DEL LITORAL Facultad de Ingeniería en Electricidad y Computación Paralelo 8 2013-2 do Término

Transcript of Informe _8 Laboratorio de Microcontroladores

Page 1: Informe _8 Laboratorio de Microcontroladores

Alexander Edward Merejildo TomaláMarcela Pérez Pereira

Integrant

ESCUELA SUPERIOR POLITÉCNICA DEL LITORAL

17-1-2014

Facultad de Ingeniería en Electricidad y Computación

8

Parale

2013-2do Término

Page 2: Informe _8 Laboratorio de Microcontroladores

1. Enunciado de la practica

Manejo de LCDRealiza un programa que muestra en el lcd "mikroElektronika", "EasyPIC5" y este mensajes es desplazado de izquierda a derecha.

Manejo del teclado 4x4Realiza un programa que muestra en el lcd la tecla presionada en el keypad 4x4 y el número de veces que fue presionada.

2. Diagrama de Bloques

Manejo de LCD

Manejo del teclado 4x4

3. Diagrama de Flujo funcional del Programa principal y de las subrutinas

Manejo de LCD

LCDPIC 16F887

LCDPIC 16F887TECLADO

Page 3: Informe _8 Laboratorio de Microcontroladores

Manejo del teclado 4x4

Page 4: Informe _8 Laboratorio de Microcontroladores

4. Descripción del algoritmo o estrategia utilizada.

Manejo de LCD1. Declaramos las variables descritas en la librería

del software que son para el uso de la LCD, las cuales conectan el puerto B con la LCD.

2. Declaramos las variables tipo texto, txt1, txt2, txt3, txt4 que van a ser usadas para mostrarlas en la LCD a través del puertoB, asi como la variable i, usada en un lazo for.

3. Seteamos el puerto B. 4. Inicializamos, limpiamos y apagamos el cursor del

LCD mediante los comandos correspondientes.5. Escribimos lo que está en text3 en la fila 1,

columna 6.6. Escribimos lo que está en text4 en la fila 2,

columna 6.7. Hacemos un retardo de 2 seg.8. Limpiamos la LCD.

Page 5: Informe _8 Laboratorio de Microcontroladores

9. Hacemos un lazo for, desde i=0 hasta i=16 incrementando i en pasos de 1, en el cual desplazamos hacia la izquierda lo que está escrito en el LCD.

10. Terminado el lazo anterior, hacemos otro lazo for, desde i=0 hasta i=16 incrementando i en pasos de 1, en el cual desplazamos hacia la derecha lo que está escrito en la LCD.

11. Terminado el lazo anterior, regresamos al paso i) del algoritmo.

Manejo del teclado 4x4

1. Declaramos las diferentes variables que usaremos en nuestro código, asignándoles un respectivo valor inicial a cada una, según el estado en el que las necesitemos.

2. Asignamos como vamos a conectar los pines de la LCD con los pines de salida del puerto que usaremos como salidas digitales en nuestro PIC.

3. Luego mediante las funciones creadas, se setea el estado inicial de la LCD y se pone el cursor para proceder a la escritura en la LCD.

4. Con el valor de Kp inicializado en cero lo que haremos es llamar a la función Keypad_Key_Press() en la cual al presionar un botón de nuestro teclado este dato es transformado a su correspondiente código ASCII y este es valor que podremos apreciar en nuestra LCD.

5. Luego hacemos uso de una de las variables declaradas “oldstate” a la cual se le asignará el valor anterior de Kp, para que este sea comparado con el valor actual de Kp, si el valor actual y el anterior son iguales se le asigna 1 a la variable cnt, caso contrario el valor de cnt se irá incrementando 1 a 1

6. Cuando el valor de la variable cnt es igual a 255 se vuelve a encerar la variable cnt y así el código se queda dentro de un lazo repetitivo e infinito.

5. Listado del programa fuente en lenguaje C

Manejo de LCD

Page 6: Informe _8 Laboratorio de Microcontroladores

// LCD module connectionssbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;// End LCD module connections

char txt1[] = "mikroElektronika"; char txt2[] = "EasyPIC5";char txt3[] = "Lcd4bit";char txt4[] = "example";

char i; // Loop variable

void Move_Delay() { // Function used for text moving Delay_ms(500); // You can change the moving speed here}

void main(){ TRISB = 0; PORTB = 0xFF; TRISB = 0xff; ANSEL = 0; // Configure AN pins as digital I/O ANSELH = 0; Lcd_Init(); // Initialize LCD

Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1,6,txt3); // Write text in first row

Lcd_Out(2,6,txt4); // Write text in second row Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); // Clear display

Lcd_Out(1,1,txt1); // Write text in first row Lcd_Out(2,5,txt2); // Write text in second row

Delay_ms(2000);

while(1) { // Endless loop// Moving text

for(i=0; i<16; i++) { // Move text to the right 4 times Lcd_Cmd(_LCD_SHIFT_RIGHT); Move_Delay(); }

for(i=0; i<16; i++) { // Move text to the left 7 times Lcd_Cmd(_LCD_SHIFT_LEFT); Move_Delay(); }

}}Manejo del teclado 4x4// Keypad module connectionschar keypadPort at PORTD;// End Keypad module connections

Page 7: Informe _8 Laboratorio de Microcontroladores

// LCD module connectionssbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;// End LCD module connections

void main() { cnt = 0; // Reset counter Keypad_Init(); // Initialize Keypad Lcd_Init(); // Initialize Lcd Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1, 1, "1"); Lcd_Out(1, 1, "Key :"); // Write message text on Lcd Lcd_Out(2, 1, "Times:");

do { kp = 0; // Reset key code variable

// Wait for key to be pressed and released do //kp = Keypad_Key_Press(); // Store key code in kp variable kp = Keypad_Key_Click(); // Store key code in kp variable while (!kp); // Prepare value for output, transform key to it's ASCII value switch (kp) { //case 10: kp = 42; break; // '*' // Uncomment this block for keypad4x3 //case 11: kp = 48; break; // '0' //case 12: kp = 35; break; // '#' //default: kp += 48;

case 1: kp = 49; break; // 1 // Uncomment this block for keypad4x4 case 2: kp = 50; break; // 2 case 3: kp = 51; break; // 3 case 4: kp = 65; break; // A case 5: kp = 52; break; // 4 case 6: kp = 53; break; // 5 case 7: kp = 54; break; // 6 case 8: kp = 66; break; // B case 9: kp = 55; break; // 7 case 10: kp = 56; break; // 8 case 11: kp = 57; break; // 9 case 12: kp = 67; break; // C case 13: kp = 42; break; // * case 14: kp = 48; break; // 0 case 15: kp = 35; break; // # case 16: kp = 68; break; // D

}

if (kp != oldstate) { // Pressed key differs from previous cnt = 1; oldstate = kp; } else { // Pressed key is same as previous cnt++;

Page 8: Informe _8 Laboratorio de Microcontroladores

}

Lcd_Chr(1, 10, kp); // Print key ASCII value on Lcd

if (cnt == 255) { // If counter varialble overflow cnt = 0; Lcd_Out(2, 10, " "); }

WordToStr(cnt, txt); // Transform counter value to string Lcd_Out(2, 10, txt); // Display counter value on Lcd } while (1);}

Manejo del teclado 4x4

unsigned short kp, cnt, oldstate = 0;char txt[6];

// Keypad module connectionschar keypadPort at PORTD;// End Keypad module connections

// LCD module connectionssbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;// End LCD module connections

void main() { cnt = 0; // Reset counter Keypad_Init(); // Initialize Keypad Lcd_Init(); // Initialize Lcd Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1, 1, "1"); Lcd_Out(1, 1, "Key :"); // Write message text on Lcd Lcd_Out(2, 1, "Times:");

do { kp = 0; // Reset key code variable

// Wait for key to be pressed and released do //kp = Keypad_Key_Press(); // Store key code in kp variable kp = Keypad_Key_Click(); // Store key code in kp variable while (!kp); // Prepare value for output, transform key to it's ASCII value switch (kp) { //case 10: kp = 42; break; // '*' // Uncomment this block for keypad4x3 //case 11: kp = 48; break; // '0' //case 12: kp = 35; break; // '#' //default: kp += 48;

case 1: kp = 49; break; // 1 // Uncomment this block for keypad4x4 case 2: kp = 50; break; // 2

Page 9: Informe _8 Laboratorio de Microcontroladores

case 3: kp = 51; break; // 3 case 4: kp = 65; break; // A case 5: kp = 52; break; // 4 case 6: kp = 53; break; // 5 case 7: kp = 54; break; // 6 case 8: kp = 66; break; // B case 9: kp = 55; break; // 7 case 10: kp = 56; break; // 8 case 11: kp = 57; break; // 9 case 12: kp = 67; break; // C case 13: kp = 42; break; // * case 14: kp = 48; break; // 0 case 15: kp = 35; break; // # case 16: kp = 68; break; // D

}

if (kp != oldstate) { // Pressed key differs from previous cnt = 1; oldstate = kp; } else { // Pressed key is same as previous cnt++; }

Lcd_Chr(1, 10, kp); // Print key ASCII value on Lcd

if (cnt == 255) { // If counter varialble overflow cnt = 0; Lcd_Out(2, 10, " "); }

WordToStr(cnt, txt); // Transform counter value to string Lcd_Out(2, 10, txt); // Display counter value on Lcd } while (1);}

6. Copia impresa del circuito armado en PROTEUS

Page 10: Informe _8 Laboratorio de Microcontroladores

Manejo del teclado 4x4

RE3/MCLR/VPP1

RA1/AN1/C12IN1-3

RA2/AN2/VREF-/CVREF/C2IN+4

RA4/T0CKI/C1OUT6

RA5/AN4/SS/C2OUT7

RB0/AN12/INT33

RB1/AN10/C12IN3-34

RB2/AN835

RA7/OSC1/CLKIN13

RA6/OSC2/CLKOUT14

RD5/P1B28

RD6/P1C29

RD7/P1D30

RC4/SDI/SDA23

RC5/SDO24

RC3/SCK/SCL18

RC2/P1A/CCP117

RC1/T1OSI/CCP216

RC0/T1OSO/T1CKI15

RB7/ICSPDAT40

RB6/ICSPCLK39

RB5/AN13/T1G38

RB4/AN1137

RD322

RD221

RD120

RD019

RC7/RX/DT26

RC6/TX/CK25

RE2/AN710

RE1/AN69

RE0/AN58

RA3/AN3/VREF+/C1IN+5

RD427

RB3/AN9/PGM/C12IN2-36

RA0/AN0/ULPWU/C12IN0-2

U1

PIC16F887

D7

14D6

13D5

12D4

11D3

10D2

9D1

8D0

7

E6

RW

5RS

4

VSS

1

VDD

2

VEE

3

LCD1LM016L

4567

RSE

0 1 2 3 4 5 6 7

RS

GND E

d1d2d3d4abcd

GND

1 2 3

4 5 6

7 8 9

0 #

1 2 3

A

B

C

D

a

b

c

d

d1 d2 d3Manejo de LCD

RE3/MCLR/VPP1

RA1/AN1/C12IN1-3

RA2/AN2/VREF-/CVREF/C2IN+4

RA4/T0CKI/C1OUT6

RA5/AN4/SS/C2OUT7

RB0/AN12/INT33

RB1/AN10/C12IN3-34

RB2/AN835

RA7/OSC1/CLKIN13

RA6/OSC2/CLKOUT14

RD5/P1B28

RD6/P1C29

RD7/P1D30

RC4/SDI/SDA23

RC5/SDO24

RC3/SCK/SCL18

RC2/P1A/CCP117

RC1/T1OSI/CCP216

RC0/T1OSO/T1CKI15

RB7/ICSPDAT40

RB6/ICSPCLK39

RB5/AN13/T1G38

RB4/AN1137

RD322

RD221

RD120

RD019

RC7/RX/DT26

RC6/TX/CK25

RE2/AN710

RE1/AN69

RE0/AN58

RA3/AN3/VREF+/C1IN+5

RD427

RB3/AN9/PGM/C12IN2-36

RA0/AN0/ULPWU/C12IN0-2

U1

PIC16F887

D7

14D6

13D5

12D4

11D3

10D2

9D1

8D0

7

E6

RW

5RS

4

VSS

1

VDD

2

VEE

3

LCD1LM016L

4567

RSE

0 1 2 3 4 5 6 7

RS

GND E

7. Conclusiones

Page 11: Informe _8 Laboratorio de Microcontroladores

1. Se observa las ventajas de utilizar el lenguaje c como base para programar PIC como es la utilización de subrutinas para el manejo del lcd y teclado.

2. Finalmente en la simulación en proteus se pudo observar como la LCD funcionaba de acuerdo al código hecho en el lenguaje C, el mismo que lo podíamos modificar a nuestro gusto para de esa manera mostrar otras cosas en la LCD, como los desplazamiento de la LCD que hacen creer al usuario que lo que se mueve es los que se muestra dentro del codigo, o la variacion en la posicion de cualquier texto en forma invertida.

8. Recomendaciones

1. Realizar las adecuadas polarizaciones del lcd para su correcto funcionamiento como por ejemplo el brillo.

2. Los puertos deseados como salidas digitales, primero el Tris y luego el puerto correspondiente, colocar el texto de manera ordenada y utilizar el delay acorde a la necesidad de retardo.

3. Tener en cuenta la tabla de conversiones a utilizar, en este caso se tendrá la tabla de conversiones a ASCII q luego el valor se mostrara por el lcd. Tener en cuenta el valor del delay que se utilizara para los retardos.

Page 12: Informe _8 Laboratorio de Microcontroladores

ANEXO A LA PRÁCTICA

1) Al ejercicio anterior agregue un botón en RD0 y cada vez que se presione el botón se mostrará en la pantalla LCD cuantas veces ha sido presionado el botón, es decir que se cuente cuantos pulsos van ingresando por RD0.

/* * Nombre del Proyecto: PULSADOR CONTADOR P8a_lcd.c * Nombre del Autor: (c) Mikroelektronika, 2009. * Description: (Explicación del ejercicio) * Test configuration: MCU: PIC16F887 Oscillator: HS, 08.0000 MHz SW: mikroC PRO for PIC

* NOTES:

*/

// PULSADOR CONTADOR// LCD module connectionssbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;// End LCD module connections

char txt1[] = "mikroElektronika";char txt2[] = "EasyPIC5";char txt3[] = "Lcd4bit";char txt4[] = "example";

char i; // Loop variable

void Move_Delay() { // Function used for text moving Delay_ms(100); // You can change the moving speed here}

void main(){char cnt=0;char texto[10]={0}; TRISB = 0; PORTB = 0xFF; TRISB = 0xff; TRISD=0xFF; ANSEL = 0; // Configure AN pins as digital I/O ANSELH = 0; Lcd_Init(); // Initialize LCD

Page 13: Informe _8 Laboratorio de Microcontroladores

Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1,6,txt3); // Write text in first row

Lcd_Out(2,6,txt4); // Write text in second row Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); // Clear display

Lcd_Out(1,1,txt1); // Write text in first row Lcd_Out(2,5,txt2); // Write text in second row

Delay_ms(2000);

while(1) { // Endless loop// Moving text

for(i=0; i<16; i++) { // Move text to the right 4 times Lcd_Cmd(_LCD_SHIFT_RIGHT); Move_Delay(); }

for(i=0; i<16; i++) { // Move text to the left 7 times Lcd_Cmd(_LCD_SHIFT_LEFT); Move_Delay(); } Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"Numero de Pulsos:"); IntToStr(cnt,texto); lcd_out(2,1,texto); while(1) { if(PORTD==1) { cnt++; Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"cnt:"); IntToStr(cnt,texto); lcd_out(2,1,texto); while(PORTD==1); } } }

2) Modifique el programa anterior para que se ingrese 4 caracteres desde el teclado 4x4 y se graben estos datos en la memoria Eeprom. En la primera línea de la pantalla LCD se muestra los caracteres en el mismo orden que ingresó el usuario y en la segunda línea se muestran los caracteres al revés. Por ejemplo:

1. 1234 Primera línea en el LCD

2. 4321 Segunda línea en el LCD

/* PARTE 2 CON MODIFICACION * Nombre del Proyecto: P8b_keyboard.c * Nombre del Autor: (c) Mikroelektronika, 2009.

Page 14: Informe _8 Laboratorio de Microcontroladores

* Description: (Explicación del ejercicio) * Test configuration: MCU: PIC16F887 Oscillator: HS, 08.0000 MHz SW: mikroC PRO for PIC

* NOTES:

*/

unsigned short kp, cnt, oldstate = 0;char txt[6];

// Keypad module connectionschar keypadPort at PORTD;// End Keypad module connections

// LCD module connectionssbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;// End LCD module connections

void main() {ANSEL = 0; // Configure AN pins as digital I/O ANSELH = 0;

cnt = 0; // Reset counter Keypad_Init(); // Initialize Keypad Lcd_Init(); // Initialize Lcd Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

do { kp = 0; // Reset key code variable

// Wait for key to be pressed and released do //kp = Keypad_Key_Press(); // Store key code in kp variable kp = Keypad_Key_Click(); // Store key code in kp variable while (!kp); // Prepare value for output, transform key to it's ASCII value switch (kp) { //case 10: kp = 42; break; // '*' // Uncomment this block for keypad4x3 //case 11: kp = 48; break; // '0' //case 12: kp = 35; break; // '#' //default: kp += 48;

Page 15: Informe _8 Laboratorio de Microcontroladores

case 1: kp = 49; break; // 1 // Uncomment this block for keypad4x4 case 2: kp = 50; break; // 2 case 3: kp = 51; break; // 3 case 4: kp = 65; break; // A case 5: kp = 52; break; // 4 case 6: kp = 53; break; // 5 case 7: kp = 54; break; // 6 case 8: kp = 66; break; // B case 9: kp = 55; break; // 7 case 10: kp = 56; break; // 8 case 11: kp = 57; break; // 9 case 12: kp = 67; break; // C case 13: kp = 42; break; // * case 14: kp = 48; break; // 0 case 15: kp = 35; break; // # case 16: kp = 68; break; // D } Lcd_Chr(1,cnt+1, kp); // Print key ASCII value on Lcd Lcd_Chr(2,4-cnt, kp); cnt++; } while (cnt < 4) ;}

Page 16: Informe _8 Laboratorio de Microcontroladores