/* $Id: openxlcd.c,v 1.1 2003/12/09 22:52:09 GrosbaJ Exp $ */ #include #include "xlcd.h" #ifdef PICDEM_LCD #include #endif /******************************************************************** * Function Name: OpenXLCD * * Return Value: void * * Parameters: lcdtype: sets the type of LCD (lines) * * Description: This routine configures the LCD. Based on * * the Hitachi HD44780 LCD controller. The * * routine will configure the I/O pins of the * * microcontroller, setup the LCD for 4- or * * 8-bit mode and clear the display. The user * * must provide three delay routines: * * DelayFor18TCY() provides a 18 Tcy delay * * DelayPORXLCD() provides at least 15ms delay * * DelayXLCD() provides at least 5ms delay * * Arlotto 01/05 : because picdem uses PORTA as cmd port * * we must configure analog lines to digital ! * * Add _H_USER putc function for XLCD * * Arlotto 03/06 : Better init for picdem lcd bce normal init * * sometimes crashes at reset * ********************************************************************/ #ifdef PICDEM_LCD static void __lcd_write(unsigned char c); #define LCD_STROBE ((E_PIN = 1),(E_PIN=0)) void OpenXLCD(unsigned char lcdtype) { TRISD &= 0xF0; ADCON1 = 0x0E ; // Ref are always VDD/VSS for picdem2 TRISA &= 0xF1 ; RW_PIN = 0 ; RS_PIN = 0; // write control bytes Delay1KTCYx(15); // power on delay PORTD = 0x3; // attention! LCD_STROBE; Delay1KTCYx(5); LCD_STROBE; Delay10TCYx(10); LCD_STROBE; Delay1KTCYx(5); PORTD = 0x2; // set 4 bit mode LCD_STROBE; Delay10TCYx(4); __lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font __lcd_write(0x08); // display off __lcd_write(0x0F); // display on, blink curson on __lcd_write(0x06); // entry mode __lcd_write(0x01); // entry mode Delay1KTCYx(15); } static void __lcd_write(unsigned char c) { PORTD = (PORTD & 0xF0) | (c >> 4); LCD_STROBE; PORTD = (PORTD & 0xF0) | (c & 0x0F); LCD_STROBE; Delay10TCYx(12); } #else void OpenXLCD(unsigned char lcdtype) { #ifdef PICDEM_LCD ADCON1 = ADCON1bits.ADFM ? 0x8E : 0x0E ; #endif // The data bits must be either a 8-bit port or the upper or // lower 4-bits of a port. These pins are made into inputs #ifdef BIT8 // 8-bit mode, use whole port DATA_PORT = 0; TRIS_DATA_PORT = 0xff; #else // 4-bit mode #ifdef UPPER // Upper 4-bits of the port DATA_PORT &= 0x0f; TRIS_DATA_PORT |= 0xf0; #else // Lower 4-bits of the port DATA_PORT &= 0xf0; TRIS_DATA_PORT |= 0x0f; #endif #endif TRIS_RW = 0; // All control signals made outputs TRIS_RS = 0; TRIS_E = 0; RW_PIN = 0; // R/W pin made low RS_PIN = 0; // Register select pin made low E_PIN = 0; // Clock pin made low // Delay for 15ms to allow for LCD Power on reset DelayPORXLCD(); // Setup interface to LCD #ifdef BIT8 // 8-bit mode interface TRIS_DATA_PORT = 0; // Data port output DATA_PORT = 0b00110000; // Function set cmd(8-bit interface) #else // 4-bit mode interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= 0b00100000; // Function set cmd(4-bit interface) #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= 0b00000010; // Function set cmd(4-bit interface) #endif #endif E_PIN = 1; // Clock the cmd in DelayFor18TCY(); E_PIN = 0; // Delay for at least 4.1ms DelayXLCD(); // Setup interface to LCD #ifdef BIT8 // 8-bit interface DATA_PORT = 0b00110000; // Function set cmd(8-bit interface) #else // 4-bit interface #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Function set cmd(4-bit interface) DATA_PORT |= 0b00100000; #else // Lower nibble interface DATA_PORT &= 0xf0; // Function set cmd(4-bit interface) DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; // Clock the cmd in DelayFor18TCY(); E_PIN = 0; // Delay for at least 100us DelayXLCD(); // Setup interface to LCD #ifdef BIT8 // 8-bit interface DATA_PORT = 0b00110000; // Function set cmd(8-bit interface) #else // 4-bit interface #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Function set cmd(4-bit interface) DATA_PORT |= 0b00100000; #else // Lower nibble interface DATA_PORT &= 0xf0; // Function set cmd(4-bit interface) DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; // Clock cmd in DelayFor18TCY(); E_PIN = 0; #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0xff; // Make data port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; // Make data nibble input #else // Lower nibble interface TRIS_DATA_PORT |= 0x0f; // Make data nibble input #endif #endif // Set data interface width, # lines, font while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(lcdtype); // Function set cmd // Turn the display on then off while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF); // Display OFF/Blink OFF while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON); // Display ON/Blink ON // Clear display while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(0x01); // Clear display // Set entry mode inc, no shift while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode // Set DD Ram address to 0 while(BusyXLCD()); // Wait if LCD busy SetDDRamAddr(0); // Set Display data ram address to 0 return; } #endif #ifdef _H_USER_IS_XLCD int _user_putc (char c) { WriteDataXLCD(c); return c ; } #endif