/* START LIBRARY DESCRIPTION *********************************************** * Rabbit_RCM3720 HD44780LCD.LIB V1.0 http://www.frank4dd.com/howto * * * * Written for a 16x2 HD44780 compatible LCD display on a Rabbit RCM3720 * * Ethernet Development Kit. Tested using all 8 datalines going to port * * A while RS, E and RW are connected to Port B on B2, B3 and B4. Written * * using Dynamic C Version 9.21. * * Frank4dd, @2008-11-09 * * public[at]frank4dd[dot]com * * END DESCRIPTION *********************************************************/ /*** BeginHeader SetPortAOut, MsDelay, pause, ByteSplit, LcdWrite, Lcd_Config, Lcd_Clear, Lcd_solidCursor_On, Lcd_noCursor_On, Lcd_blinkCursor_On, Lcd_Off, Lcd_Cursor_Home, AsciiCharLookup, LcdPutChar, LcdWriteStr, LcdLineClear */ void SetPortAOut(); void MsDelay(unsigned long milliSeconds); void pause(unsigned long seconds); void LcdInit(); void ByteSplit(char byte, int bit[8]); void LcdWrite(int mode, char hex); void Lcd_Config(); void Lcd_Clear(); void Lcd_solidCursor_On(); void Lcd_noCursor_On(); void Lcd_blinkCursor_On(); void Lcd_Cursor_Home(); void Lcd_Off(); char * AsciiCharLookup(char syschar); void LcdPutChar(char displayChar); void LcdWriteStr(int line, char s[]); void LcdLineClear(int line); /*** EndHeader */ /* if interface mode is undefined, define it as HD44780_8bit */ #ifndef INTERFACE #define INTERFACE 8 #endif /* define on which port the data lines D0-D7 or D4-D7 are connected to */ #if INTERFACE == 8 #define WrDataPort(x) WrPortI(PADR, &PADRShadow, x) // 8 lines on port A #elif INTERFACE == 4 #define WrDataPortD4(x) BitWrPortI(PADR, &PADRShadow, x, 4) // LCD D4 on port A-4 #define WrDataPortD5(x) BitWrPortI(PADR, &PADRShadow, x, 5) // LCD D5 on port A-5 #define WrDataPortD6(x) BitWrPortI(PADR, &PADRShadow, x, 6) // LCD D6 on port A-6 #define WrDataPortD7(x) BitWrPortI(PADR, &PADRShadow, x, 7) // LCD D7 on port A-7 #endif /* define on which ports the 3 control lines RS, E and RW are connected to */ #define WrCtl_RS(x) BitWrPortI(PBDR, &PBDRShadow, x, 2) // LCD RS on port B-2 #define WrCtl_EN(x) BitWrPortI(PBDR, &PBDRShadow, x, 3) // LCD En on port B-3 #define WrCtl_RW(x) BitWrPortI(PBDR, &PBDRShadow, x, 4) // LCD RW on port B-4 /* define display characteristics */ #define DISPLAYSIZE 16 // The length of the LCD display #define LCDREADTIME 1 // The LCD needs some time to read #define NEXTWRWAIT 1 // The time before another write /* port A is special and needs to be defined as output port */ void SetPortAOut() { WrPortI(SPCR, NULL, 0x84); // Set Rabbit port A to output WrPortI(PADR, &PADRShadow, 0x0); // Zero out all bits of port A } /* START FUNCTION DESCRIPTION ********************** MsDelay SYNTAX: void MsDelay(unsigned long milliSeconds); DESCRIPTION: Create a delay of x milliseconds. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void MsDelay(unsigned long milliSeconds) { unsigned long ul0; ul0 = MS_TIMER; // get the current timer value while(MS_TIMER < ul0 + milliSeconds); } /* START FUNCTION DESCRIPTION ********************** pause SYNTAX: void pause(unsigned long Seconds); DESCRIPTION: Create a delay of x seconds. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void pause(unsigned long seconds) { MsDelay(seconds*1000); // multiply sec with 1000 for ms } #if INTERFACE == 4 void ByteSplit(char byte, int bit[8]) { int i, j; j=0; for(i=128; i>0; i=i/2) { if ((byte & i) != 0) bit[j] = 1; if ((byte & i) == 0) bit[j] = 0; if (j == 7) break; else j++; } } #endif /* START FUNCTION DESCRIPTION ********************** LcdWrite SYNTAX: void LcdWrite(int mode, char hex); DESCRIPTION: Write data or a command to the LCD display. PARAMETER1: mode - 0 = command or 1 = data. PARAMETER2: hex - The hex code of the command or char. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void LcdWrite(int mode, char hex) { #if INTERFACE == 4 int bits[8]; /* In 4bit mode we split the byte into its bits, then */ /* we are going to send the first and second half of it */ ByteSplit(hex, bits); #endif WrCtl_RS(mode); // Set RS to command mode WrCtl_RW(0); // Set RW to write mode #if INTERFACE == 4 WrDataPortD4(bits[3]); // Set LCD line D4 WrDataPortD5(bits[2]); // Set LCD line D5 WrDataPortD6(bits[1]); // Set LCD line D6 WrDataPortD7(bits[0]); // Set LCD line D7 MsDelay(LCDREADTIME); WrCtl_EN(1); // Start sending data upper 4 bit MsDelay(LCDREADTIME); // Wait for LCD to read WrCtl_EN(0); // Finish transmission upper 4 bit MsDelay(NEXTWRWAIT); WrDataPortD4(bits[7]); // Set LCD line D4 WrDataPortD5(bits[6]); // Set LCD line D5 WrDataPortD6(bits[5]); // Set LCD line D6 WrDataPortD7(bits[4]); // Set LCD line D7 #elif INTERFACE == 8 WrDataPort(hex); // Set LCD line D0-D7 #endif MsDelay(LCDREADTIME); WrCtl_EN(1); // Start sending data MsDelay(LCDREADTIME); // Wait for LCD to read WrCtl_EN(0); // Finish transmission MsDelay(NEXTWRWAIT); } void LcdInit() { #if INTERFACE == 4 WrCtl_RS(0); // Set RS to command mode WrCtl_RW(0); // Set RW to write mode WrDataPortD4(1); // Set LCD line D4 WrDataPortD5(1); // Set LCD line D5 WrDataPortD6(0); // Set LCD line D6 WrDataPortD7(0); // Set LCD line D7 MsDelay(LCDREADTIME); WrCtl_EN(1); // Start sending data upper 4 bit MsDelay(LCDREADTIME); // Wait for LCD to read WrCtl_EN(0); // Finish transmission upper 4 bit MsDelay(5); WrDataPortD4(1); // Set LCD line D4 WrDataPortD5(1); // Set LCD line D5 WrDataPortD6(0); // Set LCD line D6 WrDataPortD7(0); // Set LCD line D7 MsDelay(LCDREADTIME); WrCtl_EN(1); // Start sending data upper 4 bit MsDelay(LCDREADTIME); // Wait for LCD to read WrCtl_EN(0); // Finish transmission upper 4 bit MsDelay(NEXTWRWAIT); WrDataPortD4(1); // Set LCD line D4 WrDataPortD5(1); // Set LCD line D5 WrDataPortD6(0); // Set LCD line D6 WrDataPortD7(0); // Set LCD line D7 MsDelay(LCDREADTIME); WrCtl_EN(1); // Start sending data upper 4 bit MsDelay(LCDREADTIME); // Wait for LCD to read WrCtl_EN(0); // Finish transmission upper 4 bit MsDelay(NEXTWRWAIT); WrDataPortD4(0); // Set LCD line D4 WrDataPortD5(1); // Set LCD line D5 WrDataPortD6(0); // Set LCD line D6 WrDataPortD7(0); // Set LCD line D7 MsDelay(LCDREADTIME); WrCtl_EN(1); // Start sending data upper 4 bit MsDelay(LCDREADTIME); // Wait for LCD to read WrCtl_EN(0); // Finish transmission upper 4 bit MsDelay(NEXTWRWAIT); LcdWrite(0, 0x28); // keep 4bit, set 2 lines, 5x7 font #elif INTERFACE == 8 LcdWrite(0, 0x30); // Send cmd 8bit MsDelay(4); LcdWrite(0, 0x30); // Send cmd 8bit LcdWrite(0, 0x30); // Send cmd 8bit LcdWrite(0, 0x38); // Send cmd 8bit, 2 lines, 5x7 font #endif } /* START FUNCTION DESCRIPTION ********************** Lcd_Config SYNTAX: Lcd_Config(); DESCRIPTION: Initialize and configure a HD44780 display. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void Lcd_Config() { LcdInit(); // LCD initialisation 4 or 8bit LcdWrite(0, 0x06); // Set Entry mode, increment move LcdWrite(0, 0x10); // Display and cursor shift LcdWrite(0, 0x0E); // Switch display and cursor on LcdWrite(0, 0x01); // Clear the LCD and jump to zero } /* START FUNCTION DESCRIPTION ********************** Lcd_Clear SYNTAX: void Lcd_Clear(); DESCRIPTION: Clear a HD44780 LCD and jump to zero. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void Lcd_Clear() { LcdWrite(0, 0x01); // Clear the LCD and jump to zero } void Lcd_solidCursor_On() { // Switch the LCD on LcdWrite(0, 0x0E); // Cursor is a solid underline } void Lcd_noCursor_On() { LcdWrite(0, 0x0C); // Switch the LCD on (no cursor) } void Lcd_blinkCursor_On() { // Switch the LCD on LcdWrite(0, 0x0F); // Cursor is a blinking square } void Lcd_Cursor_Home() { LcdWrite(0, 0x03); // Cursor back to first position } /* START FUNCTION DESCRIPTION ********************** Lcd_Off SYNTAX: void Lcd_Off(); DESCRIPTION: Switch the HD44780 LCD Display off. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void Lcd_Off() { LcdWrite(0, 0x08); // Switch the LCD display off } #define ASCIITABLESIZE 96 char * AsciiCharLookup(char syschar) { char * lcdCode; char charTable[ASCIITABLESIZE][2]; int i; charTable[0][0] = ' '; charTable[0][1] = 0x20; charTable[1][0] = '!'; charTable[1][1] = 0x21; charTable[2][0] = '"'; charTable[2][1] = 0x22; charTable[3][0] = '#'; charTable[3][1] = 0x23; charTable[4][0] = '$'; charTable[4][1] = 0x24; charTable[5][0] = '%'; charTable[5][1] = 0x25; charTable[6][0] = '&'; charTable[6][1] = 0x26; charTable[7][0] = '\''; charTable[7][1] = 0x27; charTable[8][0] = '('; charTable[8][1] = 0x28; charTable[9][0] = ')'; charTable[9][1] = 0x29; charTable[10][0] = '*'; charTable[10][1] = 0x2A; charTable[11][0] = '+'; charTable[11][1] = 0x2B; charTable[12][0] = ','; charTable[12][1] = 0x2C; charTable[13][0] = '-'; charTable[13][1] = 0x2D; charTable[14][0] = '.'; charTable[14][1] = 0x2E; charTable[15][0] = '/'; charTable[15][1] = 0x2F; charTable[16][0] = '0'; charTable[16][1] = 0x30; charTable[17][0] = '1'; charTable[17][1] = 0x31; charTable[18][0] = '2'; charTable[18][1] = 0x32; charTable[19][0] = '3'; charTable[19][1] = 0x33; charTable[20][0] = '4'; charTable[20][1] = 0x34; charTable[21][0] = '5'; charTable[21][1] = 0x35; charTable[22][0] = '6'; charTable[22][1] = 0x36; charTable[23][0] = '7'; charTable[23][1] = 0x37; charTable[24][0] = '8'; charTable[24][1] = 0x38; charTable[25][0] = '9'; charTable[25][1] = 0x39; charTable[26][0] = ':'; charTable[26][1] = 0x3A; charTable[27][0] = ';'; charTable[27][1] = 0x3B; charTable[28][0] = '<'; charTable[28][1] = 0x3C; charTable[29][0] = '='; charTable[29][1] = 0x3D; charTable[30][0] = '>'; charTable[30][1] = 0x3E; charTable[31][0] = '?'; charTable[31][1] = 0x3F; charTable[32][0] = '@'; charTable[32][1] = 0x40; charTable[33][0] = 'A'; charTable[33][1] = 0x41; charTable[34][0] = 'B'; charTable[34][1] = 0x42; charTable[35][0] = 'C'; charTable[35][1] = 0x43; charTable[36][0] = 'D'; charTable[36][1] = 0x44; charTable[37][0] = 'E'; charTable[37][1] = 0x45; charTable[38][0] = 'F'; charTable[38][1] = 0x46; charTable[39][0] = 'G'; charTable[39][1] = 0x47; charTable[40][0] = 'H'; charTable[40][1] = 0x48; charTable[41][0] = 'I'; charTable[41][1] = 0x49; charTable[42][0] = 'J'; charTable[42][1] = 0x4A; charTable[43][0] = 'K'; charTable[43][1] = 0x4B; charTable[44][0] = 'L'; charTable[44][1] = 0x4C; charTable[45][0] = 'M'; charTable[45][1] = 0x4D; charTable[46][0] = 'N'; charTable[46][1] = 0x4E; charTable[47][0] = 'O'; charTable[47][1] = 0x4F; charTable[48][0] = 'P'; charTable[48][1] = 0x50; charTable[49][0] = 'Q'; charTable[49][1] = 0x51; charTable[50][0] = 'R'; charTable[50][1] = 0x52; charTable[51][0] = 'S'; charTable[51][1] = 0x53; charTable[52][0] = 'T'; charTable[52][1] = 0x54; charTable[53][0] = 'U'; charTable[53][1] = 0x55; charTable[54][0] = 'V'; charTable[54][1] = 0x56; charTable[55][0] = 'W'; charTable[55][1] = 0x57; charTable[56][0] = 'X'; charTable[56][1] = 0x58; charTable[57][0] = 'Y'; charTable[57][1] = 0x59; charTable[58][0] = 'Z'; charTable[58][1] = 0x5A; charTable[59][0] = '['; charTable[59][1] = 0x5B; charTable[60][0] = ' '; charTable[60][1] = 0x5C; // The Yen currency symbol charTable[61][0] = ']'; charTable[61][1] = 0x5D; charTable[62][0] = '^'; charTable[62][1] = 0x5E; charTable[63][0] = '_'; charTable[63][1] = 0x5F; charTable[64][0] = '\\'; charTable[64][1] = 0x60; charTable[65][0] = 'a'; charTable[65][1] = 0x61; charTable[66][0] = 'b'; charTable[66][1] = 0x62; charTable[67][0] = 'c'; charTable[67][1] = 0x63; charTable[68][0] = 'd'; charTable[68][1] = 0x64; charTable[69][0] = 'e'; charTable[69][1] = 0x65; charTable[70][0] = 'f'; charTable[70][1] = 0x66; charTable[71][0] = 'g'; charTable[71][1] = 0x67; charTable[72][0] = 'h'; charTable[72][1] = 0x68; charTable[73][0] = 'i'; charTable[73][1] = 0x69; charTable[74][0] = 'j'; charTable[74][1] = 0x6A; charTable[75][0] = 'k'; charTable[75][1] = 0x6B; charTable[76][0] = 'l'; charTable[76][1] = 0x6C; charTable[77][0] = 'm'; charTable[77][1] = 0x6D; charTable[78][0] = 'n'; charTable[78][1] = 0x6E; charTable[79][0] = 'o'; charTable[79][1] = 0x6F; charTable[80][0] = 'p'; charTable[80][1] = 0x70; charTable[81][0] = 'q'; charTable[81][1] = 0x71; charTable[82][0] = 'r'; charTable[82][1] = 0x72; charTable[83][0] = 's'; charTable[83][1] = 0x73; charTable[84][0] = 't'; charTable[84][1] = 0x74; charTable[85][0] = 'u'; charTable[85][1] = 0x75; charTable[86][0] = 'v'; charTable[86][1] = 0x76; charTable[87][0] = 'w'; charTable[87][1] = 0x77; charTable[88][0] = 'x'; charTable[88][1] = 0x78; charTable[89][0] = 'y'; charTable[89][1] = 0x79; charTable[90][0] = 'z'; charTable[90][1] = 0x7A; charTable[91][0] = '{'; charTable[91][1] = 0x7B; charTable[92][0] = '|'; charTable[92][1] = 0x7C; charTable[93][0] = '}'; charTable[93][1] = 0x7D; charTable[94][0] = ' '; charTable[94][1] = 0x7E; // arrow pointing right charTable[95][0] = ' '; charTable[95][1] = 0x7F; // arrow pointing left lcdCode = NULL; for(i=0;i SYNTAX: void LcdPutChar(char displayChar); DESCRIPTION: Write a char to a HD44780 LCD Display. PARAMETER1: displayChar - The ASCII char to write. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void LcdPutChar(char displayChar) { char * lcdChar; lcdChar = AsciiCharLookup(displayChar); if(lcdChar==NULL) return; LcdWrite(1, *lcdChar); // Write character byte to LCD } /* START FUNCTION DESCRIPTION ********************** LcdWriteStr SYNTAX: void LcdWriteStr(int line, char s[]); DESCRIPTION: Write a string to a HD44780 LCD Display. PARAMETER1: line - The display line to write to. PARAMETER2: s[] - The string to write to the line. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void LcdWriteStr(int line, char s[]) { int i; if (line == 1) LcdWrite(0, 0x80); // Set display address for line 1 if (line == 2) LcdWrite(0, 0xA8); // Set display address for line 2 for(i=0;i SYNTAX: void LcdLineClear(int line); DESCRIPTION: Clear a line of a HD44780 LCD Display. PARAMETER1: line - The display line to write to. RETURN VALUE: None KEY WORDS: HD44780 LCD END DESCRIPTION ***********************************/ void LcdLineClear(int line) { int i; if (line == 1) LcdWrite(0, 0x80); // Set display address for line 1 if (line == 2) LcdWrite(0, 0xA8); // Set display address for line 2 for(i=0;i