/* 16x2 character LCD * .----------------. * | pin15 |- unused * | pin16 |- unused * | | * | pin01 GND|- GND * | pin02 +5v|- +5v * | pin03 Contrast|- 4.7k pot * | pin04 RS|- PORTB4 * | pin05 RW|- GND * | pin06 EN|- PORTB5 * | pin07 Data0|- GND * | pin08 Data1|- GND * | pin09 Data2|- GND * | pin10 Data3|- GND * | pin11 Data4|- PORTB0 * | pin12 Data5|- PORTB1 * | pin13 Data6|- PORTB2 * | pin14 Data7|- PORTB3 * '-----------------' * * LCD interface. * This code will interface to a standard LCD controller like the Hitachi HD44780. * This code is for the standard 14 pin connector, and 4-bit mode is assumed. */ /* If you're changing the pinout of the code, these vars are the only changes you have to make. * Plus, you'll have to change lcd_write_byte(), where it explicitly puts 4 bits onto portb * Plus, you'll have to change the TRIS in your main program. */ static bit LCD_RS @ ((unsigned)&PORTB*8+4); // Register select static bit LCD_EN @ ((unsigned)&PORTB*8+5); // Enable /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// #define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0)) /********************************************************************************************** * Write one byte to the LCD. */ void lcd_write_byte(unsigned char c) { PORTB = (PORTB & 0xF0) | (c >> 4); LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); LCD_STROBE; delay_us(40); } /********************************************************************************************** * Clear and home the LCD. */ void lcd_clear_and_home(void) { LCD_RS = 0; // control lcd_write_byte(0x01); delay_ms(2); } /********************************************************************************************** * Go to the specified position. */ void lcd_goto(unsigned char pos) { LCD_RS = 0; // control lcd_write_byte(0x80+pos); } /********************************************************************************************** * Write one char to the LCD. */ void lcd_write_char(char c) { LCD_RS = 1; // characters lcd_write_byte(c); } /********************************************************************************************** * Write a string of chars to the LCD. */ void lcd_write_string(const char * s) { LCD_RS = 1; // characters while(*s) { lcd_write_byte(*s++); } } /********************************************************************************************** * Clear and home the LCD, * then write a string of chars to the LCD. */ void lcd_overwrite_string(const char * s) { lcd_clear_and_home(); LCD_RS = 1; // characters while(*s) { lcd_write_byte(*s++); } } /********************************************************************************************** * Write a string of chars to the LCD * starting at the beginning of the second line of the display. */ void lcd_write_string_at_second_line(const char * s) { lcd_goto(64); LCD_RS = 1; // characters while(*s) { lcd_write_byte(*s++); } } /********************************************************************************************** * Load the custom characters into cgram. * Also performs Clear and Home. */ void lcd_load_custom_characters(void) { LCD_RS = 0; // control lcd_write_byte(0b01000000); // cgram address #0 LCD_RS = 1; // characters // pattern is xxxbbbbb // arrow right lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000100); lcd_write_byte(0b00000010); lcd_write_byte(0b00011111); lcd_write_byte(0b00000010); lcd_write_byte(0b00000100); // arrow left lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000100); lcd_write_byte(0b00001000); lcd_write_byte(0b00011111); lcd_write_byte(0b00001000); lcd_write_byte(0b00000100); // arrow up lcd_write_byte(0b00000100); lcd_write_byte(0b00001110); lcd_write_byte(0b00010101); lcd_write_byte(0b00000100); lcd_write_byte(0b00001100); lcd_write_byte(0b00011000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); // node lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00001110); lcd_write_byte(0b00010001); lcd_write_byte(0b00010001); lcd_write_byte(0b00010001); lcd_write_byte(0b00001110); // marked node lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00001110); lcd_write_byte(0b00010001); lcd_write_byte(0b00010101); lcd_write_byte(0b00010001); lcd_write_byte(0b00001110); // line lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_write_byte(0b00011111); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); // corner bottom left lcd_write_byte(0b00000100); lcd_write_byte(0b00000100); lcd_write_byte(0b00000100); lcd_write_byte(0b00000100); lcd_write_byte(0b00000110); lcd_write_byte(0b00000011); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); // corner bottom right lcd_write_byte(0b00000100); lcd_write_byte(0b00000100); lcd_write_byte(0b00000100); lcd_write_byte(0b00000100); lcd_write_byte(0b00001100); lcd_write_byte(0b00011000); lcd_write_byte(0b00000000); lcd_write_byte(0b00000000); lcd_clear_and_home(); } /********************************************************************************************** * Initialize the LCD. */ void lcd_initialize(void) { LCD_RS = 0; // control delay_us(100); lcd_write_byte(0x28); // 4 bit mode, 1/16 duty (2 lines), 5x8 dots lcd_write_byte(0x0C); // display on, blink off, cursor off //lcd_write_byte(0x0F); // display on, blink on, cursor on lcd_write_byte(0x06); // entry mode lcd_load_custom_characters(); } /********************************************************************************************** * Display a pattern of custom characters from cgram. * Also performs Clear and Home. */ void lcd_draw_logo(void) { // 0 = arrow right // 1 = arrow left // 2 = arrow up // 3 = node // 4 = marked node // 5 = line // 6 = corner bottom left // 7 = corner bottom right lcd_clear_and_home(); lcd_write_char(0); lcd_write_char(3); lcd_write_char(5); lcd_write_char(5); lcd_write_char(5); lcd_write_char(0); lcd_write_char(3); lcd_write_char(0); lcd_write_char(3); lcd_write_char(5); lcd_write_char(5); lcd_write_char(0); lcd_write_char(3); lcd_write_char(5); lcd_write_char(5); lcd_write_char(3); lcd_goto(64); lcd_write_char(' '); lcd_write_char(6); lcd_write_char(5); lcd_write_char(0); lcd_write_char(3); lcd_write_char(5); lcd_write_char(2); lcd_write_char(' '); lcd_write_char(6); lcd_write_char(0); lcd_write_char(4); lcd_write_char(1); lcd_write_char(7); lcd_write_char(4); lcd_write_char(1); lcd_write_char(7); }