--- /dev/null
+/****************************************************************************\r
+ Title : HD44780U LCD library\r
+ Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury\r
+ File: $Id: lcd.c,v 1.14.2.1 2006/01/29 12:16:41 peter Exp $\r
+ Software: AVR-GCC 3.3 \r
+ Target: any AVR device, memory mapped mode only for AT90S4414/8515/Mega\r
+\r
+ DESCRIPTION\r
+ Basic routines for interfacing a HD44780U-based text lcd display\r
+\r
+ Originally based on Volker Oth's lcd library,\r
+ changed lcd_init(), added additional constants for lcd_command(),\r
+ added 4-bit I/O mode, improved and optimized code.\r
+\r
+ Library can be operated in memory mapped mode (LCD_IO_MODE=0) or in \r
+ 4-bit IO port mode (LCD_IO_MODE=1). 8-bit IO port mode not supported.\r
+ \r
+ Memory mapped mode compatible with Kanda STK200, but supports also\r
+ generation of R/W signal through A8 address line.\r
+\r
+ USAGE\r
+ See the C include lcd.h file for a description of each function\r
+ \r
+*****************************************************************************/\r
+#include <inttypes.h>\r
+#include <avr/io.h>\r
+#include <avr/pgmspace.h>\r
+#include "lcd.h"\r
+\r
+\r
+\r
+/* \r
+** constants/macros \r
+*/\r
+#define DDR(x) (*(&x - 1)) /* address of data direction register of port x */\r
+#if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)\r
+ /* on ATmega64/128 PINF is on port 0x00 and not 0x60 */\r
+ #define PIN(x) ( &PORTF==&(x) ? _SFR_IO8(0x00) : (*(&x - 2)) )\r
+#else\r
+ #define PIN(x) (*(&x - 2)) /* address of input register of port x */\r
+#endif\r
+\r
+\r
+#if LCD_IO_MODE\r
+#define lcd_e_delay() __asm__ __volatile__( "rjmp 1f\n 1:" );\r
+#define lcd_e_high() LCD_E_PORT |= _BV(LCD_E_PIN);\r
+#define lcd_e_low() LCD_E_PORT &= ~_BV(LCD_E_PIN);\r
+#define lcd_e_toggle() toggle_e()\r
+#define lcd_rw_high() LCD_RW_PORT |= _BV(LCD_RW_PIN)\r
+#define lcd_rw_low() LCD_RW_PORT &= ~_BV(LCD_RW_PIN)\r
+#define lcd_rs_high() LCD_RS_PORT |= _BV(LCD_RS_PIN)\r
+#define lcd_rs_low() LCD_RS_PORT &= ~_BV(LCD_RS_PIN)\r
+#endif\r
+\r
+#if LCD_IO_MODE\r
+#if LCD_LINES==1\r
+#define LCD_FUNCTION_DEFAULT LCD_FUNCTION_4BIT_1LINE \r
+#else\r
+#define LCD_FUNCTION_DEFAULT LCD_FUNCTION_4BIT_2LINES \r
+#endif\r
+#else\r
+#if LCD_LINES==1\r
+#define LCD_FUNCTION_DEFAULT LCD_FUNCTION_8BIT_1LINE\r
+#else\r
+#define LCD_FUNCTION_DEFAULT LCD_FUNCTION_8BIT_2LINES\r
+#endif\r
+#endif\r
+\r
+#if LCD_CONTROLLER_KS0073\r
+#if LCD_LINES==4\r
+\r
+#define KS0073_EXTENDED_FUNCTION_REGISTER_ON 0x24 /* |0|010|0100 4-bit mode extension-bit RE = 1 */\r
+#define KS0073_EXTENDED_FUNCTION_REGISTER_OFF 0x20 /* |0|000|1001 4 lines mode */\r
+#define KS0073_4LINES_MODE 0x09 /* |0|001|0000 4-bit mode, extension-bit RE = 0 */\r
+\r
+#endif\r
+#endif\r
+\r
+/* \r
+** function prototypes \r
+*/\r
+#if LCD_IO_MODE\r
+static void toggle_e(void);\r
+#endif\r
+\r
+/*\r
+** local functions\r
+*/\r
+\r
+\r
+\r
+/*************************************************************************\r
+ delay loop for small accurate delays: 16-bit counter, 4 cycles/loop\r
+*************************************************************************/\r
+static inline void _delayFourCycles(unsigned int __count)\r
+{\r
+ if ( __count == 0 ) \r
+ __asm__ __volatile__( "rjmp 1f\n 1:" ); // 2 cycles\r
+ else\r
+ __asm__ __volatile__ (\r
+ "1: sbiw %0,1" "\n\t" \r
+ "brne 1b" // 4 cycles/loop\r
+ : "=w" (__count)\r
+ : "0" (__count)\r
+ );\r
+}\r
+\r
+\r
+/************************************************************************* \r
+delay for a minimum of <us> microseconds\r
+the number of loops is calculated at compile-time from MCU clock frequency\r
+*************************************************************************/\r
+#define delay(us) _delayFourCycles( ( ( 1*(XTAL/4000) )*us)/1000 )\r
+\r
+\r
+#if LCD_IO_MODE\r
+/* toggle Enable Pin to initiate write */\r
+static void toggle_e(void)\r
+{\r
+ lcd_e_high();\r
+ lcd_e_delay();\r
+ lcd_e_low();\r
+}\r
+#endif\r
+\r
+\r
+/*************************************************************************\r
+Low-level function to write byte to LCD controller\r
+Input: data byte to write to LCD\r
+ rs 1: write data \r
+ 0: write instruction\r
+Returns: none\r
+*************************************************************************/\r
+#if LCD_IO_MODE\r
+static void lcd_write(uint8_t data,uint8_t rs) \r
+{\r
+ unsigned char dataBits ;\r
+\r
+\r
+ if (rs) { /* write data (RS=1, RW=0) */\r
+ lcd_rs_high();\r
+ } else { /* write instruction (RS=0, RW=0) */\r
+ lcd_rs_low();\r
+ }\r
+ lcd_rw_low();\r
+\r
+ if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )\r
+ && (LCD_DATA0_PIN == 0) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )\r
+ {\r
+ /* configure data pins as output */\r
+ DDR(LCD_DATA0_PORT) |= 0x0F;\r
+\r
+ /* output high nibble first */\r
+ dataBits = LCD_DATA0_PORT & 0xF0;\r
+ LCD_DATA0_PORT = dataBits |((data>>4)&0x0F);\r
+ lcd_e_toggle();\r
+\r
+ /* output low nibble */\r
+ LCD_DATA0_PORT = dataBits | (data&0x0F);\r
+ lcd_e_toggle();\r
+\r
+ /* all data pins high (inactive) */\r
+ LCD_DATA0_PORT = dataBits | 0x0F;\r
+ }\r
+ else\r
+ {\r
+ /* configure data pins as output */\r
+ DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);\r
+ DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);\r
+ DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);\r
+ DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);\r
+ \r
+ /* output high nibble first */\r
+ LCD_DATA3_PORT &= ~_BV(LCD_DATA3_PIN);\r
+ LCD_DATA2_PORT &= ~_BV(LCD_DATA2_PIN);\r
+ LCD_DATA1_PORT &= ~_BV(LCD_DATA1_PIN);\r
+ LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);\r
+ if(data & 0x80) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);\r
+ if(data & 0x40) LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);\r
+ if(data & 0x20) LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);\r
+ if(data & 0x10) LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN); \r
+ lcd_e_toggle();\r
+ \r
+ /* output low nibble */\r
+ LCD_DATA3_PORT &= ~_BV(LCD_DATA3_PIN);\r
+ LCD_DATA2_PORT &= ~_BV(LCD_DATA2_PIN);\r
+ LCD_DATA1_PORT &= ~_BV(LCD_DATA1_PIN);\r
+ LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);\r
+ if(data & 0x08) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);\r
+ if(data & 0x04) LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);\r
+ if(data & 0x02) LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);\r
+ if(data & 0x01) LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);\r
+ lcd_e_toggle(); \r
+ \r
+ /* all data pins high (inactive) */\r
+ LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);\r
+ LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);\r
+ LCD_DATA2_PORT |= _BV(LCD_DATA2_PIN);\r
+ LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);\r
+ }\r
+}\r
+#else\r
+#define lcd_write(d,rs) if (rs) *(volatile uint8_t*)(LCD_IO_DATA) = d; else *(volatile uint8_t*)(LCD_IO_FUNCTION) = d;\r
+/* rs==0 -> write instruction to LCD_IO_FUNCTION */\r
+/* rs==1 -> write data to LCD_IO_DATA */\r
+#endif\r
+\r
+\r
+/*************************************************************************\r
+Low-level function to read byte from LCD controller\r
+Input: rs 1: read data \r
+ 0: read busy flag / address counter\r
+Returns: byte read from LCD controller\r
+*************************************************************************/\r
+#if LCD_IO_MODE\r
+static uint8_t lcd_read(uint8_t rs) \r
+{\r
+ uint8_t data;\r
+ \r
+ \r
+ if (rs)\r
+ lcd_rs_high(); /* RS=1: read data */\r
+ else\r
+ lcd_rs_low(); /* RS=0: read busy flag */\r
+ lcd_rw_high(); /* RW=1 read mode */\r
+ \r
+ if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )\r
+ && ( LCD_DATA0_PIN == 0 )&& (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )\r
+ {\r
+ DDR(LCD_DATA0_PORT) &= 0xF0; /* configure data pins as input */\r
+ \r
+ lcd_e_high();\r
+ lcd_e_delay(); \r
+ data = PIN(LCD_DATA0_PORT) << 4; /* read high nibble first */\r
+ lcd_e_low();\r
+ \r
+ lcd_e_delay(); /* Enable 500ns low */\r
+ \r
+ lcd_e_high();\r
+ lcd_e_delay();\r
+ data |= PIN(LCD_DATA0_PORT)&0x0F; /* read low nibble */\r
+ lcd_e_low();\r
+ }\r
+ else\r
+ {\r
+ /* configure data pins as input */\r
+ DDR(LCD_DATA0_PORT) &= ~_BV(LCD_DATA0_PIN);\r
+ DDR(LCD_DATA1_PORT) &= ~_BV(LCD_DATA1_PIN);\r
+ DDR(LCD_DATA2_PORT) &= ~_BV(LCD_DATA2_PIN);\r
+ DDR(LCD_DATA3_PORT) &= ~_BV(LCD_DATA3_PIN);\r
+ \r
+ /* read high nibble first */\r
+ lcd_e_high();\r
+ lcd_e_delay(); \r
+ data = 0;\r
+ if ( PIN(LCD_DATA0_PORT) & _BV(LCD_DATA0_PIN) ) data |= 0x10;\r
+ if ( PIN(LCD_DATA1_PORT) & _BV(LCD_DATA1_PIN) ) data |= 0x20;\r
+ if ( PIN(LCD_DATA2_PORT) & _BV(LCD_DATA2_PIN) ) data |= 0x40;\r
+ if ( PIN(LCD_DATA3_PORT) & _BV(LCD_DATA3_PIN) ) data |= 0x80;\r
+ lcd_e_low();\r
+\r
+ lcd_e_delay(); /* Enable 500ns low */\r
+ \r
+ /* read low nibble */ \r
+ lcd_e_high();\r
+ lcd_e_delay();\r
+ if ( PIN(LCD_DATA0_PORT) & _BV(LCD_DATA0_PIN) ) data |= 0x01;\r
+ if ( PIN(LCD_DATA1_PORT) & _BV(LCD_DATA1_PIN) ) data |= 0x02;\r
+ if ( PIN(LCD_DATA2_PORT) & _BV(LCD_DATA2_PIN) ) data |= 0x04;\r
+ if ( PIN(LCD_DATA3_PORT) & _BV(LCD_DATA3_PIN) ) data |= 0x08; \r
+ lcd_e_low();\r
+ }\r
+ return data;\r
+}\r
+#else\r
+#define lcd_read(rs) (rs) ? *(volatile uint8_t*)(LCD_IO_DATA+LCD_IO_READ) : *(volatile uint8_t*)(LCD_IO_FUNCTION+LCD_IO_READ)\r
+/* rs==0 -> read instruction from LCD_IO_FUNCTION */\r
+/* rs==1 -> read data from LCD_IO_DATA */\r
+#endif\r
+\r
+\r
+/*************************************************************************\r
+loops while lcd is busy, returns address counter\r
+*************************************************************************/\r
+static uint8_t lcd_waitbusy(void)\r
+\r
+{\r
+ register uint8_t c;\r
+ \r
+ /* wait until busy flag is cleared */\r
+ while ( (c=lcd_read(0)) & (1<<LCD_BUSY)) {}\r
+ \r
+ /* the address counter is updated 4us after the busy flag is cleared */\r
+ delay(2);\r
+\r
+ /* now read the address counter */\r
+ return (lcd_read(0)); // return address counter\r
+ \r
+}/* lcd_waitbusy */\r
+\r
+\r
+/*************************************************************************\r
+Move cursor to the start of next line or to the first line if the cursor \r
+is already on the last line.\r
+*************************************************************************/\r
+static inline void lcd_newline(uint8_t pos)\r
+{\r
+ register uint8_t addressCounter;\r
+\r
+\r
+#if LCD_LINES==1\r
+ addressCounter = 0;\r
+#endif\r
+#if LCD_LINES==2\r
+ if ( pos < (LCD_START_LINE2) )\r
+ addressCounter = LCD_START_LINE2;\r
+ else\r
+ addressCounter = LCD_START_LINE1;\r
+#endif\r
+#if LCD_LINES==4\r
+#if KS0073_4LINES_MODE\r
+ if ( pos < LCD_START_LINE2 )\r
+ addressCounter = LCD_START_LINE2;\r
+ else if ( (pos >= LCD_START_LINE2) && (pos < LCD_START_LINE3) )\r
+ addressCounter = LCD_START_LINE3;\r
+ else if ( (pos >= LCD_START_LINE3) && (pos < LCD_START_LINE4) )\r
+ addressCounter = LCD_START_LINE4;\r
+ else \r
+ addressCounter = LCD_START_LINE1;\r
+#else\r
+ if ( pos < LCD_START_LINE3 )\r
+ addressCounter = LCD_START_LINE2;\r
+ else if ( (pos >= LCD_START_LINE2) && (pos < LCD_START_LINE4) )\r
+ addressCounter = LCD_START_LINE3;\r
+ else if ( (pos >= LCD_START_LINE3) && (pos < LCD_START_LINE2) )\r
+ addressCounter = LCD_START_LINE4;\r
+ else \r
+ addressCounter = LCD_START_LINE1;\r
+#endif\r
+#endif\r
+ lcd_command((1<<LCD_DDRAM)+addressCounter);\r
+\r
+}/* lcd_newline */\r
+\r
+\r
+/*\r
+** PUBLIC FUNCTIONS \r
+*/\r
+\r
+/*************************************************************************\r
+Send LCD controller instruction command\r
+Input: instruction to send to LCD controller, see HD44780 data sheet\r
+Returns: none\r
+*************************************************************************/\r
+void lcd_command(uint8_t cmd)\r
+{\r
+ lcd_waitbusy();\r
+ lcd_write(cmd,0);\r
+}\r
+\r
+\r
+/*************************************************************************\r
+Send data byte to LCD controller \r
+Input: data to send to LCD controller, see HD44780 data sheet\r
+Returns: none\r
+*************************************************************************/\r
+void lcd_data(uint8_t data)\r
+{\r
+ lcd_waitbusy();\r
+ lcd_write(data,1);\r
+}\r
+\r
+\r
+\r
+/*************************************************************************\r
+Set cursor to specified position\r
+Input: x horizontal position (0: left most position)\r
+ y vertical position (0: first line)\r
+Returns: none\r
+*************************************************************************/\r
+void lcd_gotoxy(uint8_t x, uint8_t y)\r
+{\r
+#if LCD_LINES==1\r
+ lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);\r
+#endif\r
+#if LCD_LINES==2\r
+ if ( y==0 ) \r
+ lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);\r
+ else\r
+ lcd_command((1<<LCD_DDRAM)+LCD_START_LINE2+x);\r
+#endif\r
+#if LCD_LINES==4\r
+ if ( y==0 )\r
+ lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);\r
+ else if ( y==1)\r
+ lcd_command((1<<LCD_DDRAM)+LCD_START_LINE2+x);\r
+ else if ( y==2)\r
+ lcd_command((1<<LCD_DDRAM)+LCD_START_LINE3+x);\r
+ else /* y==3 */\r
+ lcd_command((1<<LCD_DDRAM)+LCD_START_LINE4+x);\r
+#endif\r
+\r
+}/* lcd_gotoxy */\r
+\r
+\r
+/*************************************************************************\r
+*************************************************************************/\r
+int lcd_getxy(void)\r
+{\r
+ return lcd_waitbusy();\r
+}\r
+\r
+\r
+/*************************************************************************\r
+Clear display and set cursor to home position\r
+*************************************************************************/\r
+void lcd_clrscr(void)\r
+{\r
+ lcd_command(1<<LCD_CLR);\r
+}\r
+\r
+\r
+/*************************************************************************\r
+Set cursor to home position\r
+*************************************************************************/\r
+void lcd_home(void)\r
+{\r
+ lcd_command(1<<LCD_HOME);\r
+}\r
+\r
+\r
+/*************************************************************************\r
+Display character at current cursor position \r
+Input: character to be displayed \r
+Returns: none\r
+*************************************************************************/\r
+void lcd_putc(char c)\r
+{\r
+ uint8_t pos;\r
+\r
+\r
+ pos = lcd_waitbusy(); // read busy-flag and address counter\r
+ if (c=='\n')\r
+ {\r
+ lcd_newline(pos);\r
+ }\r
+ else\r
+ {\r
+#if LCD_WRAP_LINES==1\r
+#if LCD_LINES==1\r
+ if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {\r
+ lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);\r
+ }\r
+#elif LCD_LINES==2\r
+ if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {\r
+ lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2,0); \r
+ }else if ( pos == LCD_START_LINE2+LCD_DISP_LENGTH ){\r
+ lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);\r
+ }\r
+#elif LCD_LINES==4\r
+ if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {\r
+ lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2,0); \r
+ }else if ( pos == LCD_START_LINE2+LCD_DISP_LENGTH ) {\r
+ lcd_write((1<<LCD_DDRAM)+LCD_START_LINE3,0);\r
+ }else if ( pos == LCD_START_LINE3+LCD_DISP_LENGTH ) {\r
+ lcd_write((1<<LCD_DDRAM)+LCD_START_LINE4,0);\r
+ }else if ( pos == LCD_START_LINE4+LCD_DISP_LENGTH ) {\r
+ lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);\r
+ }\r
+#endif\r
+ lcd_waitbusy();\r
+#endif\r
+ lcd_write(c, 1);\r
+ }\r
+\r
+}/* lcd_putc */\r
+\r
+\r
+/*************************************************************************\r
+Display string without auto linefeed \r
+Input: string to be displayed\r
+Returns: none\r
+*************************************************************************/\r
+void lcd_puts(const char *s)\r
+/* print string on lcd (no auto linefeed) */\r
+{\r
+ register char c;\r
+\r
+ while ( (c = *s++) ) {\r
+ lcd_putc(c);\r
+ }\r
+\r
+}/* lcd_puts */\r
+\r
+\r
+/*************************************************************************\r
+Display string from program memory without auto linefeed \r
+Input: string from program memory be be displayed \r
+Returns: none\r
+*************************************************************************/\r
+void lcd_puts_p(const char *progmem_s)\r
+/* print string from program memory on lcd (no auto linefeed) */\r
+{\r
+ register char c;\r
+\r
+ while ( (c = pgm_read_byte(progmem_s++)) ) {\r
+ lcd_putc(c);\r
+ }\r
+\r
+}/* lcd_puts_p */\r
+\r
+\r
+/*************************************************************************\r
+Initialize display and select type of cursor \r
+Input: dispAttr LCD_DISP_OFF display off\r
+ LCD_DISP_ON display on, cursor off\r
+ LCD_DISP_ON_CURSOR display on, cursor on\r
+ LCD_DISP_CURSOR_BLINK display on, cursor on flashing\r
+Returns: none\r
+*************************************************************************/\r
+void lcd_init(uint8_t dispAttr)\r
+{\r
+#if LCD_IO_MODE\r
+ /*\r
+ * Initialize LCD to 4 bit I/O mode\r
+ */\r
+ \r
+ if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )\r
+ && ( &LCD_RS_PORT == &LCD_DATA0_PORT) && ( &LCD_RW_PORT == &LCD_DATA0_PORT) && (&LCD_E_PORT == &LCD_DATA0_PORT)\r
+ && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) \r
+ && (LCD_RS_PIN == 4 ) && (LCD_RW_PIN == 5) && (LCD_E_PIN == 6 ) )\r
+ {\r
+ /* configure all port bits as output (all LCD lines on same port) */\r
+ DDR(LCD_DATA0_PORT) |= 0x7F;\r
+ }\r
+ else if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )\r
+ && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )\r
+ {\r
+ /* configure all port bits as output (all LCD data lines on same port, but control lines on different ports) */\r
+ DDR(LCD_DATA0_PORT) |= 0x0F;\r
+ DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);\r
+ DDR(LCD_RW_PORT) |= _BV(LCD_RW_PIN);\r
+ DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);\r
+ }\r
+ else\r
+ {\r
+ /* configure all port bits as output (LCD data and control lines on different ports */\r
+ DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);\r
+ DDR(LCD_RW_PORT) |= _BV(LCD_RW_PIN);\r
+ DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);\r
+ DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);\r
+ DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);\r
+ DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);\r
+ DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);\r
+ }\r
+ delay(16000); /* wait 16ms or more after power-on */\r
+ \r
+ /* initial write to lcd is 8bit */\r
+ LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN); // _BV(LCD_FUNCTION)>>4;\r
+ LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN); // _BV(LCD_FUNCTION_8BIT)>>4;\r
+ lcd_e_toggle();\r
+ delay(4992); /* delay, busy flag can't be checked here */\r
+ \r
+ /* repeat last command */ \r
+ lcd_e_toggle(); \r
+ delay(64); /* delay, busy flag can't be checked here */\r
+ \r
+ /* repeat last command a third time */\r
+ lcd_e_toggle(); \r
+ delay(64); /* delay, busy flag can't be checked here */\r
+\r
+ /* now configure for 4bit mode */\r
+ LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN); // LCD_FUNCTION_4BIT_1LINE>>4\r
+ lcd_e_toggle();\r
+ delay(64); /* some displays need this additional delay */\r
+ \r
+ /* from now the LCD only accepts 4 bit I/O, we can use lcd_command() */ \r
+#else\r
+ /*\r
+ * Initialize LCD to 8 bit memory mapped mode\r
+ */\r
+ \r
+ /* enable external SRAM (memory mapped lcd) and one wait state */ \r
+ MCUCR = _BV(SRE) | _BV(SRW);\r
+\r
+ /* reset LCD */\r
+ delay(16000); /* wait 16ms after power-on */\r
+ lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */ \r
+ delay(4992); /* wait 5ms */\r
+ lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */ \r
+ delay(64); /* wait 64us */\r
+ lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */ \r
+ delay(64); /* wait 64us */\r
+#endif\r
+\r
+#if KS0073_4LINES_MODE\r
+ /* Display with KS0073 controller requires special commands for enabling 4 line mode */\r
+ lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_ON);\r
+ lcd_command(KS0073_4LINES_MODE);\r
+ lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_OFF);\r
+#else\r
+ lcd_command(LCD_FUNCTION_DEFAULT); /* function set: display lines */\r
+#endif\r
+ lcd_command(LCD_DISP_OFF); /* display off */\r
+ lcd_clrscr(); /* display clear */ \r
+ lcd_command(LCD_MODE_DEFAULT); /* set entry mode */\r
+ lcd_command(dispAttr); /* display/cursor control */\r
+\r
+}/* lcd_init */\r