Sie sind auf Seite 1von 15

ESCUELA SUPERIOR POLITCNICA DEL LITORAL

17-1-2014

Facultad de Ingeniera en Electricidad y Computacin

LCD y Teclado
Laboratorio de Microcontroladores

Integrantes
Alexander Edward Merejildo Tomal Marcela Prez Pereira

Paralelo
8

2013-2do Trmino

1. Enunciado de la practica Manejo de LCD Realiza un programa que muestra en el lcd "mikroElektronika", "EasyPIC5" y este mensajes es desplazado de izquierda a derecha. Manejo del teclado 4x4 Realiza un programa que muestra en el lcd la tecla presionada en el keypad 4x4 y el nmero de veces que fue presionada. 2. Diagrama de Bloques Manejo de LCD PIC 16F887 Manejo del teclado 4x4 TECLADO PIC 16F887 LCD LCD

3. Diagrama de Flujo funcional del Programa principal y de las subrutinas Manejo de LCD
INICIO DECLARACIN DE VARIABLES PUERTO B COMO SALIDA PUESTO EN FF PUERTO B COMO ENTRADA LIMPIAR LCD APAGAR CURSOR ESCRIBIR TEXTO EN FILA 1 COLUMNA 6 ESCRIBIR TEXTO EN FILA 2 COLUMNA 6

NO

2 SEG
SI

LIMPIAR LCD ESCRIBIR TEXTO EN FILA 1 COLUMNA 1 ESCRIBIR TEXTO EN FILA 2 COLUMNA 5

NO

NO

2 SEG

LIMPIAR LCD MOVER TEXTO DEL LCD A LA IZQUIERDA CADA 5 SEG

i=0

i+1
SI

i=16

NO

i=0 MOVER TEXTO DEL LCD A LA DERECHA CADA 5 SEG i+1 i=16
NO

SI

Manejo del teclado 4x4


inicio

Seteo de Puertos Seteo de Parmetros

Seleccionar los mdulos de conexin en la LCD

Declaracin e Inicializacin de Variables no Kp=0 cnt=0

si

Keypad_Key_Press()

Oldstate=Kp

Kp!=oldstate si

no

si no cnt=1 cnt=cnt+1

cnt==255

4. Descripcin del algoritmo o estrategia utilizada. Manejo de LCD 1. Declaramos las variables descritas en la librera 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 travs 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. 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 cdigo, asignndoles un respectivo valor inicial a cada una, segn 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 funcin Keypad_Key_Press() en la cual al presionar un botn de nuestro teclado este dato es transformado a su correspondiente cdigo 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 cdigo se queda dentro de un lazo repetitivo e infinito.

5. Listado del programa fuente en lenguaje C Manejo de LCD


// LCD module connections sbit 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 char char char txt1[] txt2[] txt3[] txt4[] = = = = "mikroElektronika"; "EasyPIC5"; "Lcd4bit"; "example"; // Loop variable // Function used for text moving // You can change the moving speed here

char i; void Move_Delay() { Delay_ms(500); } void main(){ TRISB = 0; PORTB = 0xFF; TRISB = 0xff; ANSEL = 0; ANSELH = 0; Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,6,txt3); Lcd_Out(2,6,txt4); Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,txt1); Lcd_Out(2,5,txt2); Delay_ms(2000); while(1) { // Moving text for(i=0; i<16; i++) { Lcd_Cmd(_LCD_SHIFT_RIGHT); Move_Delay(); } for(i=0; i<16; i++) { Lcd_Cmd(_LCD_SHIFT_LEFT); Move_Delay(); }

// Configure AN pins as digital I/O // Initialize LCD // Clear display // Cursor off // Write text in first row // Write text in second row // Clear display // Write text in first row // Write text in second row

// Endless loop

// Move text to the right 4 times

// Move text to the left 7 times

} } Manejo del teclado 4x4 // Keypad module connections char keypadPort at PORTD; // End Keypad module connections // LCD module connections sbit 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; Keypad_Init(); Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1, 1, "1"); Lcd_Out(1, 1, "Key :"); Lcd_Out(2, 1, "Times:"); do { kp = 0;

// Reset counter // Initialize Keypad // Initialize Lcd // Clear display // Cursor off // Write message text on Lcd

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

if (kp != oldstate) { cnt = 1; oldstate = kp; } else { cnt++; } Lcd_Chr(1, 10, kp); if (cnt == 255) { cnt = 0; Lcd_Out(2, 10, " } WordToStr(cnt, txt); Lcd_Out(2, 10, txt); } while (1); }

// Pressed key differs from previous

// Pressed key is same as previous

// Print key ASCII value on Lcd // If counter varialble overflow ");

// Transform counter value to string // Display counter value on Lcd

Manejo del teclado 4x4


unsigned short kp, cnt, oldstate = 0; char txt[6]; // Keypad module connections char keypadPort at PORTD; // End Keypad module connections // LCD module connections sbit 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; Keypad_Init(); Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1, 1, "1"); Lcd_Out(1, 1, "Key :"); Lcd_Out(2, 1, "Times:"); do { kp = 0;

// Reset counter // Initialize Keypad // Initialize Lcd // Clear display // Cursor off // Write message text on Lcd

// 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; //case 12: kp = 35; break; //default: kp += 48; case case case case case case case case case case case case case case case case } if (kp != oldstate) { cnt = 1; oldstate = kp; } else { cnt++; } Lcd_Chr(1, 10, kp); if (cnt == 255) { cnt = 0; Lcd_Out(2, 10, " } WordToStr(cnt, txt); Lcd_Out(2, 10, txt); } while (1); } 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: kp kp kp kp kp kp kp kp kp kp kp kp kp kp kp kp = = = = = = = = = = = = = = = = 49; 50; 51; 65; 52; 53; 54; 66; 55; 56; 57; 67; 42; 48; 35; 68; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; break; // // // // // // // // // // // // // // // //

// '0' // '#'

1 2 3 A 4 5 6 B 7 8 9 C * 0 # D

// Uncomment this block for keypad4x4

// Pressed key differs from previous

// Pressed key is same as previous

// Print key ASCII value on Lcd // If counter varialble overflow ");

// Transform counter value to string // Display counter value on Lcd

6. Copia impresa del circuito armado en PROTEUS

Manejo del teclado 4x4

Manejo de LCD

7. Conclusiones

1. Se observa las ventajas de utilizar el lenguaje c como base para programar PIC como es la utilizacin de subrutinas para el manejo del lcd y teclado. 2. Finalmente en la simulacin en proteus se pudo observar como la LCD funcionaba de acuerdo al cdigo hecho en el lenguaje C, el mismo que lo podamos 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.

ANEXO A LA PRCTICA 1) Al ejercicio anterior agregue un botn en RD0 y cada vez que se presione el botn se mostrar en la pantalla LCD cuantas veces ha sido presionado el botn, 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: (Explicacin del ejercicio) * Test configuration: MCU: PIC16F887 Oscillator: HS, 08.0000 MHz SW: mikroC PRO for PIC * NOTES: */ // PULSADOR CONTADOR // LCD module connections sbit 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 char char char txt1[] txt2[] txt3[] txt4[] = = = = "mikroElektronika"; "EasyPIC5"; "Lcd4bit"; "example"; // Loop variable // Function used for text moving // You can change the moving speed here

char i; void Move_Delay() { Delay_ms(100); } void main(){ char cnt=0; char texto[10]={0}; TRISB = 0; PORTB = 0xFF; TRISB = 0xff; TRISD=0xFF; ANSEL = 0; ANSELH = 0; Lcd_Init(); Lcd_Cmd(_LCD_CLEAR);

// Configure AN pins as digital I/O // Initialize LCD // Clear display

Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,6,txt3); Lcd_Out(2,6,txt4); Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,txt1); Lcd_Out(2,5,txt2); Delay_ms(2000); while(1) { // Moving text for(i=0; i<16; i++) { Lcd_Cmd(_LCD_SHIFT_RIGHT); Move_Delay(); }

// Cursor off // Write text in first row // Write text in second row // Clear display // Write text in first row // Write text in second row

// Endless loop

// Move text to the right 4 times

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 lnea de la pantalla LCD se muestra los caracteres en el mismo orden que ingres el usuario y en la segunda lnea se muestran los caracteres al revs. Por ejemplo: 1. 2. 1234 Primera lnea en el LCD 4321 Segunda lnea en el LCD

/* PARTE 2 CON MODIFICACION * Nombre del Proyecto: P8b_keyboard.c * Nombre del Autor: (c) Mikroelektronika, 2009. * Description: (Explicacin 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 connections char keypadPort at PORTD; // End Keypad module connections // LCD module connections sbit 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; ANSELH = 0; cnt = 0; Keypad_Init(); Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF);

// Configure AN pins as digital I/O

// Reset counter // Initialize Keypad // Initialize Lcd // Clear display // 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; case 1: kp = 49; break; // 1 // Uncomment this block for keypad4x4

case case case case case case case case case case case case case case case }

2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:

kp kp kp kp kp kp kp kp kp kp kp kp kp kp kp

= = = = = = = = = = = = = = =

50; 51; 65; 52; 53; 54; 66; 55; 56; 57; 67; 42; 48; 35; 68;

break; break; break; break; break; break; break; break; break; break; break; break; break; break; break;

// // // // // // // // // // // // // // //

2 3 A 4 5 6 B 7 8 9 C * 0 # D // Print key ASCII value on Lcd

Lcd_Chr(1,cnt+1, kp); Lcd_Chr(2,4-cnt, kp); cnt++; } while (cnt < 4) ; }

Das könnte Ihnen auch gefallen