/* * lcd.c * * 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): * * PORTB bits 0-3 are connected to the LCD data bits 4-7 (high nibble) * PORTB bit 4 is connected to the LCD RS input (register select) * PORTB bit 5 is connected to the R/!W * * PORTA bit 4 is connected to the LCD EN bit (enable) * PORTA bit 5 is connected to the LCD backlight * * To use these routines, set up the port I/O (TRISA, TRISB) then * call lcd_init(), then other routines as required. * * Modified from the HITEC C sample programs to work with * the Northmicro NM110 LCD board. */ #include #include "lcd.h" #include "delay.h" #define LCD_RS RB4 //control/data mode #define LCD_RW RB5 //read/!write #define LCD_EN RA4 //enable #define LCD_BL RA5 //backlight #define nop asm("nop") #define LCD_STROBE LCD_EN = 1; nop; LCD_EN = 0 #define LCD_PORTEN TRISB = 0; nop; LCD_RW = 0 //LCD Port fix as LCD shares with keypad /* write a byte to the LCD in 4 bit mode */ void lcd_write(unsigned char c) { PORTB = (PORTB & 0xF0) | (c >> 4); // send MSNibble LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); // send LSNibble LCD_STROBE; DelayUs(80); } /* Clear and home the LCD */ void lcd_clear(void) { LCD_PORTEN; LCD_RS = 0; lcd_write(0x1); DelayMs(2); } /* write a string of chars to the LCD */ void lcd_puts(const char * s) { LCD_PORTEN; LCD_RS = 1; // write characters while(*s) lcd_write(*s++); } /* write one character to the LCD */ void lcd_putch(char c) { LCD_PORTEN; LCD_RS = 1; // write characters PORTB = (PORTB & 0xF0) | (c >> 4); // send MSNibble LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); // send LSNibble LCD_STROBE; DelayUs(80); } /* Go to the specified position */ void lcd_goto(unsigned char pos) { LCD_PORTEN; LCD_RS = 0; lcd_write(0x80+pos); //80 is goto command } /* control backlight */ void lcd_bl(char status) { if(status) LCD_BL = 1; //turn backlight on else LCD_BL = 0; //turn backlight off } /* initialise the LCD - put into 4 bit mode */ void lcd_init(void) { LCD_EN = 0; // setup lcd enable for strobe LCD_PORTEN; // enable port and write mode LCD_RS = 0; // clr to write control bytes DelayMs(30); // power on delay lcd_write(0x30); DelayMs(5); lcd_write(0x30); DelayMs(5); lcd_write(0x30); DelayMs(5); lcd_write(0x20); DelayMs(5); lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font lcd_write(0x0c); // display on cursor off blink off lcd_clear(); lcd_write(0x06); // entry mode set increment mode/ entire shift off }