/********************************************************************** * LCD display driver for SC1602 module (HITACHI HD44780U controller) * 4 bit transfer mode * * connection: * bit0 LCD E * bit1 LCD RS * bit2:3 N/C * bit4:7 LCD Data **********************************************************************/ #include #include "lcd4.h" /* configure following definition */ #define _XTAL_FREQ 10000000 #define LCD_PORT PORTB /* set LCD port */ #define LCD_CONFIGREG TRISB /* configuration register */ #define LCD_E RB0 /* set E bit of I/O port */ #define LCD_RS RB1 /* set RS bit of I/O port */ #ifndef _XTAL_FREQ #error _XTAL_FREQ is not defined #endif #if _XTAL_FREQ > 18000000 /* if clock is faster than 18MHz */ #define STROBE (LCD_E=1,asm("nop"),LCD_E=0) #else #define STROBE (LCD_E=1,LCD_E=0) #endif /* LCD initialization */ void Init_LCD4(void){ LCD_CONFIGREG=0b00001100; __delay_ms(15); LCD_PORT=0x30; /* E=0, RS=0, command=0x30 */ STROBE; __delay_ms(5); /* > 4.1ms */ STROBE; __delay_us(128); /* >100us */ STROBE; /* set 4bit mode */ LCD_PORT=0x20; /* E=0, RS=0, command=0x20 */ STROBE; Putc_LCD4(0x28, LCD_CMD); /* display mode */ Putc_LCD4(0x0C, LCD_CMD); /* display on */ Clr_LCD4; Putc_LCD4(0x06, LCD_CMD); /* entry mode */ } /* write one byte character or command c: ASCII code or command rs: LCD_CMD or LCD_DATA */ void Putc_LCD4(char c, char rs){ __delay_us(52); /* >40us */ LCD_RS=rs; rs=LCD_PORT & 0x0f; LCD_PORT=((c & 0xf0)+rs); /* E=0, RS=rs, high addr */ STROBE; LCD_PORT=((c << 4)+rs); /* E=0, RS=rs, low addr */ STROBE; } /* write character string ptr: start pointer of character string which is NULL terminated. */ void Puts_LCD4(char *ptr){ while(*ptr){ Putc_LCD4(*ptr++, LCD_DATA); } } /* shift display to left n times */ void Shift_LN(int n){ int i; for (i=0;i