LCD Interface PGM
LCD Interface PGM
LCD Interface PGM
LCD interface example Uses routines from delay.c This code will interface to a standard LCD controller like the Hitachi HD44780. It uses it in 4 bit mode, with the hardware connected as follows (the standard 14 pin LCD connector is used): PORTD bits 0-3 are connected to the LCD data bits 4-7 (high nibble) PORTA bit 3 is connected to the LCD RS input (register select) PORTA bit 1 is connected to the LCD EN bit (enable) To use these routines, set up the port I/O (TRISA, TRISD) then call lcd_init(), then other routines as required.
#defineLCD_RS RD2 //#define LCD_RW RA2 #define LCD_EN RD3 #define LCD_DATA4 #define LCD_DATA5 #define LCD_DATA6 #define LCD_DATA7 //#define LCD_DATA RD4 RD5 RD6 RD7 PORTD
#defineLCD_STROBE()
((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */ void lcd_write(unsigned char c) { unsigned char f; f = c; DelayUs(40); if(c & 0x10) LCD_DATA4 = 1;
else LCD_DATA4 = 0; if(c & 0x20) LCD_DATA5 = 1; else LCD_DATA5 = 0; if(c & 0x40) LCD_DATA6 = 1; else LCD_DATA6 = 0; if(c & 0x80) LCD_DATA7 = 1; else LCD_DATA7 = 0; //LCD_DATA = ( c & 0xF0 ); LCD_STROBE(); if(c & 0x01) LCD_DATA4 = 1; else LCD_DATA4 = 0; if(c & 0x02) LCD_DATA5 = 1; else LCD_DATA5 = 0; if(c & 0x04) LCD_DATA6 = 1; else LCD_DATA6 = 0; if(c & 0x08) LCD_DATA7 = 1; else LCD_DATA7 = 0; //LCD_DATA = ( ( f << 4 ) & 0xF0 ); LCD_STROBE(); }
} /* * */
void lcd_clear(void) { LCD_RS = 0; lcd_write(0x1); DelayMs(2); } /* write a string of chars to the LCD */ void lcd_puts(const char * s) { LCD_RS = 1; // write characters while(*s) lcd_write(*s++); }
/* * Go to the specified position */ void lcd_1st_line(unsigned char pos) { LCD_RS = 0; lcd_write(0x80+pos); } void lcd_2nd_line(unsigned char pos) { LCD_RS = 0; lcd_write(0xC0+pos); }
/* initialise the LCD - put into 4 bit mode */ void lcd_init() { char init_value;
//
DelayMs(15); // wait 15mSec after power applied, LCD_DATA4 = 1; LCD_DATA5 = 1; LCD_DATA6 = 0; LCD_DATA7 = 0; LCD_STROBE(); DelayMs(5); LCD_STROBE(); DelayUs(200); LCD_STROBE(); DelayUs(200); LCD_DATA4 = 0; LCD_DATA5 = 1; LCD_DATA6 = 0; LCD_DATA7 = 0; LCD_STROBE(); lcd_write(0x28); // Set interface length lcd_write(0x0E); // Display On, Cursor On, Cursor Blink lcd_clear(); // Clear screen lcd_write(0x06); // Set entry Mode }