/* 16x2 character LCD * .----------------. * | pin15 |- unused * | pin16 |- unused * | | * | pin01 GND|- GND * | pin02 +5v|- +5v * | pin03 Contrast|- 4.7k pot * | pin04 RS|- PORTB1 * | pin05 RW|- GND * | pin06 EN|- PORTB0 * | pin07 Data0|- GND * | pin08 Data1|- GND * | pin09 Data2|- GND * | pin10 Data3|- GND * | pin11 Data4|- PORTB4 * | pin12 Data5|- PORTB5 * | pin13 Data6|- PORTB6 * | pin14 Data7|- PORTB7 * '-----------------' * * 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. */ #include #include "lcd.h" #include "delay.c" static bit LCD_RS @ ((unsigned)&PORTB*8+1); // Register select static bit LCD_EN @ ((unsigned)&PORTB*8+0); // Enable #define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0)) /* Clear and home the LCD. */ void lcd_clear_and_home(void) { LCD_RS = 0; lcd_write_byte(0x01); delay_ms(2); } /* Go to the specified position. */ void lcd_goto(unsigned char pos) { LCD_RS = 0; lcd_write_byte(0x80+pos); } /* Write one byte to the LCD. */ void lcd_write_byte(unsigned char c) { PORTB = (PORTB & 0x0F) | (c & 0xF0); LCD_STROBE; PORTB = (PORTB & 0x0F) | (c << 4); LCD_STROBE; delay_us(40); } /* Write one char to the LCD. */ void lcd_write_char(char c) { LCD_RS = 1; // characters PORTB = (PORTB & 0x0F) | (c & 0xF0); LCD_STROBE; PORTB = (PORTB & 0x0F) | (c << 4); LCD_STROBE; delay_us(40); } /* 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++); } } /* Initialise the LCD. */ void lcd_initialize(void) { LCD_RS = 0; // write control bytes delay_ms(15); // power on delay PORTB = 0x30; // attention! LCD_STROBE; delay_ms(5); LCD_STROBE; delay_us(100); LCD_STROBE; delay_ms(5); PORTB = 0x20; // set 4 bit mode LCD_STROBE; delay_us(40); 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(); } /* Load the custom characters into cgram. * Also performs Clear and Home. */ void lcd_load_custom_characters(void) { LCD_RS = 0; lcd_write_byte(0b01000000); // cgram address #0 LCD_RS = 1; // 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(); } /* 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); }