494
// Function prototypes void initializeLCD(void); void clearLCD(void); void sendCommandToLCD(unsigned char command); void sendDataToLCD(unsigned char data); void printToLCD(const char *string); unsigned char moveCursorToPosition(unsigned char address); void initializeLCD(void) { // Set pins as output ENABLE_DIRECTION = 0; RS_DIRECTION = 0; LCD_BUS_DIRECTION = 0; // Write zero to pins and port ENABLE_LCD = 0; RS_LCD = 0; LCD_BUS = 0; __delay_ms(10); // 10 milliseconds delay // Initialization commands for LCD sendCommandToLCD(0x38); // Function set as given in datasheet sendCommandToLCD(0x0F); // Display ON; Cursor ON; Blink ON sendCommandToLCD(0x06); // Display shifting OFF clearLCD(); // Clear screen command } void clearLCD(void) { sendCommandToLCD(0x01); // Clear screen command __delay_ms(3); // Delay for cursor to return home (minimum 3ms) } void sendCommandToLCD(unsigned char command) { RS_LCD = 0; // Command RS must be low (0) LCD_BUS = command; // Write command to data bus of LCD ENABLE_LCD = 1; // Toggle Enable PIN to display data __delay_us(200); ENABLE_LCD = 0; __delay_us(200); } void sendDataToLCD(unsigned char data) { RS_LCD = 1; // Data RS must be high (1) LCD_BUS = data; // Write data to data bus of LCD ENABLE_LCD = 1; // Toggle Enable PIN to display data __delay_us(200); ENABLE_LCD = 0; __delay_us(200); } void printToLCD(const char *string) { while(*string) { sendDataToLCD(*string++); // Display data until string ends } } unsigned char moveCursorToPosition(unsigned char address) { // Valid addresses: 0x80 to 0xA8 for line one, 0xC0 to 0xE8 for line two if ((address >= 0x80 && address <= 0xA8) || (address >= 0xC0 && address <= 0xE8)) { sendCommandToLCD(address); return 1; } else { return 0; } }