Está en la página 1de 11

PIC Interfaces Agregando un Mdulo LCD

Autor: Carlos A. Narvez V. Ingeniero Electricista Universidad de Oriente Email: cnarvaez@bolivar.udo.edu.ve

Propsito
En los diseos con Microcontroladores es muy frecuente la necesidad de visualizar algn mensaje durante la ejecucin de un programa. Normalmente se utilizan los display de siete segmentos pero estos, adems de no mostrar caracteres ASCII tienen un elevado consumo de energa y agregan ciertas complicaciones cuando se requiere multiplexaje. Los mdulos LCD solucionan estos problemas y agregan una presentacin excelente al proyecto terminado, adems no se incurre en costos excesivos debido a la tendencia en la disminucin del precio de estos mdulos.

Caractersticas
Este trabajo se basa en el mdulo LCD de Optrex Corporation DMC16249UB que utiliza el controlador Hitachi HD44780. Este mdulo puede desplegar caracteres de 5x8 dots en un formato de 16 caracteres x 2 lneas y puede trabajar en modo de 4 u 8 bits. En el modo de 4 bits, se debe leer o escribir dos veces, en la primera se obtiene o enva el nibble alto (MSB) y en la segunda el nibble bajo (LSB) del dato o instruccin. La tabla siguiente muestra la funcin de cada pin de este modulo. Pin
1 2 3 4 5 6 7 8 9 10 11 12 13 14

Smbolo
VSS VCC VEE RS R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

Nivel
H/L H/L H/L H/L H/L H/L H/L H/L H/L H/L H/L

Funcin
Fuente de poder (0v, GND) Fuente de poder (5v logic) Fuente de poder LCD Drive Seal RegisterSelect Read/write Seal Select H:Read L: Write Seal Enable (no pull-up resister) Data Bus Line/ Sin conexin en modo 4bit Data Bus Line/ Sin conexin en modo 4bit Data Bus Line/ Sin conexin en modo 4bit Data Bus Line/ Sin conexin en modo 4bit Data Bus Line Data Bus Line Data Bus Line Data Bus Line

Los pines de conexin de estos mdulos incluyen un bus de datos de 8 bits, un pin de habilitacin (E), un pin de seleccin, que indica que el dato es una instruccin o una carcter del mensaje (RS) y un pin que indica si se va a escribir o leer en el mdulo (R/W). Del bus de datos slo se utiliza la mitad (4) cuando se trabaja en modo de 4 bits

Carlos A. Narvez V. 2003

PIC Interfaces
Instrucciones
Para los efectos de este trabajo, slo indagaremos las instrucciones ms comunes del mdulo. Para una informacin completa los remitimos al documento HD44780U Dot Matrix Liquid Crystal Display Controller/Driver de Hitachi.

Instruccin Clear Display Return Home Entry Mode Set Display on/off Funtion Set Set DDRAM ADDR Write data Read data

RS 0 0 0 0 0 0 1 1

R/W 0 0 0 0 0 0 0 1

DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 -0 0 0 0 0 1 I/D S 0 0 0 0 1 D C B 0 0 1 DL N F --1 Address Data Data

Clear Display: Blanquea el display escribiendo el carcter espacio 20H en todas las direcciones de la DDRAM, pone a cero el contador de direcciones y retorna el display a su estado inicial. Return Home: Coloca el contador de direcciones a cero, retorna el display a su estado inicial, el contenido de la DDRAM no cambia y coloca el cursor en la posicin ms a la izquierda de la lnea uno. Entry Mode Set: Si I/D = 1, incrementa el contador de direcciones DDRAM en uno cuando un carcter es escrito o leido desde la DDRAM. Si I/D = 0, entonces decrementa el contador de direcciones en uno cuando se da la misma condicin anterior. Display on/off: El display esta encendido cuando D = 1 y apagado cuando D = 0. Cuando esta apagado los datos en la DDRAM permanecen y pueden desplegarse instantaneamente poniendo D=1. El cursor es mostrado cuando C = 1 y desaparece cuando C = 0. Aun cuando el cursor desaparezca, la funcin de I/D y otras especificaciones no cambian. El carcter indicado por el cursor parpadea cuando B= 1. Set DDRAM ADDR: Coloca la direccin suministrada en el contador de direcciones. El dato puede entonces ser escrito o leido de la DDRAM. Cuando el display es de 1 lnea el rango de direcciones es de 00H a 4FH. Cuando el display es de 2 lneas el rango de direcciones es de 00H a 27H para la primera lnea y de 40H a 67H para la segunda lnea.

Carlos A. Narvez V. 2003

PIC Interfaces
Inicializacin
El siguiente diagrama de flujo muestra el proceso de inicializacin para el modo de 4 bits.

Funtion Set Modo 8 bits

Funtion Set Modo 8 bits

Funtion Set Modo 8 bits

Funtion Set Set a modo 4 bits

Funtion Set modo 4 bits, especifique el numero de lineas y fuente Display off

Display clear Entry mode set

Carlos A. Narvez V. 2003

PIC Interfaces
Hardware
A continuacin se muestra la conexin del LCD descrito trabajando en modo de 4 bits, con un PIC16F84. Observe que los pines DB0 a DB3 no se usan en este caso.

Contraste

10K

Vss Vcc VEE RS R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

+V 1k 100 +V S1 RESET

PIC16F84
RA2 RA1 RA3 RA0 RA4 OSC1 MCLR OSC2 Vss VDD RB0 RB7 RB1 RB6 RB2 RB5 RB3 RB4

LCD

15pF 4MHZ

15pF

Carlos A. Narvez V. 2003

PIC Interfaces
Software
/***************************************************************** */ /* */ /* LcdTest.c */ /* Implementando una interfaz LCD */ /* para el PIC16F84a */ /* */ /* Original: J. Winpenny */ /* Adaptacin: Carlos Narvaez 05/05/2003 */ /* */ /* */ /*---------------------------------------------------------------- */ /* LCD : OPTREX Corporation DMC16249UB */ /* LCD tipo: HD44780 */ /* Compilador: CSC PCM C compiler */ /* */ /* Interface : SEL = Port A bit 2 */ /* WR = Port A bit 1 */ /* RS = Port A bit 0 */ /* Data_7 = Port B bit 7 */ /* Data_6 = Port B bit 6 */ /* Data_5 = Port B bit 5 */ /* Data_4 = Port B bit 4 */ /* */ /* Esta version es para el modo de operacin 4 bit */ /* */ /* */ /***************************************************************** */ #include <16f84a.h> #use delay (clock=4000000) #fuse XL,NOWDT, NOPROTECT, PUT /***************************************************************** */ /* Prototipos */ /***************************************************************** */ void LCD_Setup(void); void LCD_FunctionMode(void); void LCD_DataMode(void); void LCD_Write_8_Bit(char); void LCD_Write_4_Bit(char); void LCD_Delay(void); void delay_s(int); //void WriteLCDString( const char *lcdptr ); /***************************************************************** */ /* Definiciones Generales */ /***************************************************************** */ #byte OPTION_REG = 0x81 /***************************************************************** */ /* Definiciones para la interface PIC-LCD */ /***************************************************************** */ #define LCD_SEL PIN_A2 /* Port A bit 2 ( Habilita LCD ) */ #define LCD_WR PIN_A1 /* Port A bit 1 ( Escribir = 0 ) */ #define LCD_RS PIN_A0 /* Port A bit 0 ( register Select ) */ #define LCD_DATA_4 PIN_B4 /* LCD BIT 4 */ #define LCD_DATA_5 PIN_B5 /* LCD BIT 5 */ #define LCD_DATA_6 PIN_B6 /* LCD BIT 6 */ #define LCD_DATA_7 PIN_B7 /* LCD BIT 7 */ /******************************************************************/

Carlos A. Narvez V. 2003

PIC Interfaces

/******************************************************************************* */ /* Comandos del LCD ( Segun LCD Data Sheet ) */ /***************************************************************** ****************/ /* */ #define clear_lcd 0x01 /* Clear Display */ #define return_home 0x02 /* Cursor to Home position */ #define entry_mode 0x06 /* Normal entry mode */ #define entry_mode_shift 0x07 /* - with shift */ #define system_set_8_bit 0x38 /* 8 bit data mode 2 line ( 5x7 font ) */ #define system_set_4_bit 0x28 /* 4 bit data mode 2 line ( 5x7 font ) */ #define display_on 0x0c /* Switch ON Display */ #define display_off 0x08 /* Cursor plus blink */ #define set_dd_line1 0x80 /* Line 1 position 1 */ #define set_dd_line2 0xC0 /* Line 2 position 1 */ #define set_dd_ram 0x80 /* Line 1 position 1 */ #define write_data 0x00 /* With RS = 1 */ #define cursor_on 0x0E /* Switch Cursor ON */ #define cursor_off 0x0C /* Switch Cursor OFF */ /* */ /*********************************************************************************/ /* Variables configuracion del PIC */ /*********************************************************************************/ #define TxMode 0x40 /* Set option reg TMR0 interupt */ /* source from counter. */ /* Max speed prescaler ( 1:2 ) */ /* ( Not needed for LCD ) */ #define PortAConfig 0x10 /* Configure port A ( 1 = input ) */ /* RA4/RTCC = Input / RA2 = Input */ #define PortBConfig 0x0B /* Configure port B */ /*********************************************************************************/ /* Programa Principal */ /*********************************************************************************/ void main(void) { char a; char b; /* Configuracin puertos PIC e Interrucciones */ OPTION_REG = TxMode; set_tris_a( PortAConfig); set_tris_b( PortBConfig ); output_low(LCD_SEL); disable_interrupts( GLOBAL ); /* End of Setup */ /* Set Option register. /* Setup ports */ */

/* Disable LCD */ /* Disable Global Interrupts */

Carlos A. Narvez V. 2003

PIC Interfaces

LCD_Setup(); LCD_Write_4_Bit('L'); LCD_Write_4_Bit('C'); LCD_Write_4_Bit('D'); LCD_Write_4_Bit(' '); LCD_Write_4_Bit('T'); LCD_Write_4_Bit('e'); LCD_Write_4_Bit('s'); LCD_Write_4_Bit('t'); delay_s(10); while( 1 ) { LCD_FunctionMode(); LCD_Write_4_Bit(clear_lcd); LCD_DataMode(); a = 'a'; for ( b = 0; b < 16; b++ ) { LCD_Write_4_Bit(a++); delay_ms(250); } LCD_FunctionMode(); LCD_Write_4_Bit(set_dd_line2); LCD_DataMode(); for ( b = 0; b < 10; b++ ) { LCD_Write_4_Bit(a++); delay_ms(250); } delay_s(5); } } /* END OF MAIN */

/* Setup the LCD device

*/

/* Display a test message */

/* Write "abcdefghijklmnop" on line 1 */

/* Goto line 2 */ /* Write "qrstuvwxyz" on line 2 */

Carlos A. Narvez V. 2003

PIC Interfaces

/***********************************/ /* Setup the lcd device */ /***********************************/ void LCD_Setup(void) { /* Reset the LCD delay_ms(30); LCD_FunctionMode(); LCD_Write_8_Bit( system_set_4_bit ); delay_ms(5); LCD_Write_8_Bit( system_set_4_bit ); delay_ms(5); LCD_Write_8_Bit( system_set_4_bit ); delay_ms(5); LCD_Write_4_Bit( delay_ms(2); LCD_Write_4_Bit( delay_ms(2); LCD_Write_4_Bit( delay_ms(2); LCD_Write_4_Bit( delay_ms(2); LCD_Write_4_Bit( delay_ms(2); LCD_DataMode(); } /***********************************/ /* LCD modo comandos */ /***********************************/ void LCD_FunctionMode(void) { output_low(LCD_RS); LCD_Delay(); } /***********************************/ /* LCD modo Data */ /***********************************/ void LCD_DataMode(void) { output_high(LCD_RS); LCD_Delay(); } system_set_4_bit ); display_off ); entry_mode ); display_on ); set_dd_ram ); /* Power up delay */ */

/* This sequence resets the LCD */

Carlos A. Narvez V. 2003

PIC Interfaces

/***********************************/ /* Escribe un byte en el LCD */ /* Modo 8 Bit */ /***********************************/ void LCD_Write_8_Bit(char d ) { output_low(LCD_WR); LCD_Delay(); if ( d & 0x80 ) output_high(LCD_DATA_7); else output_low(LCD_DATA_7); if ( d & 0x40 ) output_high(LCD_DATA_6); else output_low(LCD_DATA_6); if ( d & 0x20 ) output_high(LCD_DATA_5); else output_low(LCD_DATA_5); if ( d & 0x10 ) output_high(LCD_DATA_4); else output_low(LCD_DATA_4); LCD_Delay(); output_high(LCD_SEL); LCD_Delay(); output_low(LCD_SEL); } /***********************************/ /* Escribe un byte en el LCD */ /* Modo 4 Bit */ /***********************************/ void LCD_Write_4_Bit(char d ) { output_low(LCD_WR); LCD_Delay(); /* Write Mode */ /* modo Write /* Setup data */ */

/* Select LCD

*/

/* De-select LCD */

/* Output Higher 4 bits */ if ( d & 0x80 ) output_high(LCD_DATA_7); else output_low(LCD_DATA_7); if ( d & 0x40 ) output_high(LCD_DATA_6); else output_low(LCD_DATA_6);

Carlos A. Narvez V. 2003

PIC Interfaces

if ( d & 0x20 ) output_high(LCD_DATA_5); else output_low(LCD_DATA_5); if ( d & 0x10 ) output_high(LCD_DATA_4); else output_low(LCD_DATA_4); LCD_Delay(); output_high(LCD_SEL); LCD_Delay(); output_low(LCD_SEL); LCD_Delay(); d <<= 4; if ( d & 0x80 ) output_high(LCD_DATA_7); else output_low(LCD_DATA_7); if ( d & 0x40 ) output_high(LCD_DATA_6); else output_low(LCD_DATA_6); if ( d & 0x20 ) output_high(LCD_DATA_5); else output_low(LCD_DATA_5); if ( d & 0x10 ) output_high(LCD_DATA_4); else output_low(LCD_DATA_4); LCD_Delay(); output_high(LCD_SEL); LCD_Delay(); output_low(LCD_SEL); } /***********************************/ /* Retardo temporal para LCD */ /* Adjuste segun el LCD que use */ /***********************************/ void LCD_Delay(void) { delay_ms(1); }

/* Clock in the data

*/

/* Output Lower 4 bits

*/

/* Clock in the data

*/

Carlos A. Narvez V. 2003

PIC Interfaces
/***********************************/ /* Retardo segundos */ /***********************************/ void delay_s(int n) { for (;n!=0; n--) delay_ms(1000); }

Carlos A. Narvez V. 2003

También podría gustarte