1 | void lcd_init(uint8_t dispAttr)
|
2 | {
|
3 | #if LCD_IO_MODE
|
4 | /*
|
5 | * Initialize LCD to 4 bit I/O mode
|
6 | */
|
7 |
|
8 | if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
|
9 | && ( &LCD_RS_PORT == &LCD_DATA0_PORT) && ( &LCD_RW_PORT == &LCD_DATA0_PORT) && (&LCD_E_PORT == &LCD_DATA0_PORT)
|
10 | && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3)
|
11 | && (LCD_RS_PIN == 4 ) && (LCD_RW_PIN == 5) && (LCD_E_PIN == 6 ) )
|
12 | {
|
13 | /* configure all port bits as output (all LCD lines on same port) */
|
14 | DDR(LCD_DATA0_PORT) |= 0x7F;
|
15 | }
|
16 | else if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
|
17 | && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )
|
18 | {
|
19 | /* configure all port bits as output (all LCD data lines on same port, but control lines on different ports) */
|
20 | DDR(LCD_DATA0_PORT) |= 0x0F;
|
21 | DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);
|
22 | DDR(LCD_RW_PORT) |= _BV(LCD_RW_PIN);
|
23 | DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
|
24 | }
|
25 | else
|
26 | {
|
27 | /* configure all port bits as output (LCD data and control lines on different ports */
|
28 | DDR(LCD_RS_PORT) |= _BV(LCD_RS_PIN);
|
29 | DDR(LCD_RW_PORT) |= _BV(LCD_RW_PIN);
|
30 | DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
|
31 | DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
|
32 | DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
|
33 | DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
|
34 | DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
|
35 | }
|
36 | delay(16000); /* wait 16ms or more after power-on */
|
37 |
|
38 | /* initial write to lcd is 8bit */
|
39 | LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN); // _BV(LCD_FUNCTION)>>4;
|
40 | LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN); // _BV(LCD_FUNCTION_8BIT)>>4;
|
41 | lcd_e_toggle();
|
42 | delay(4992); /* delay, busy flag can't be checked here */
|
43 |
|
44 | /* repeat last command */
|
45 | lcd_e_toggle();
|
46 | delay(64); /* delay, busy flag can't be checked here */
|
47 |
|
48 | /* repeat last command a third time */
|
49 | lcd_e_toggle();
|
50 | delay(64); /* delay, busy flag can't be checked here */
|
51 |
|
52 | /* now configure for 4bit mode */
|
53 | LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN); // LCD_FUNCTION_4BIT_1LINE>>4
|
54 | lcd_e_toggle();
|
55 | delay(64); /* some displays need this additional delay */
|
56 |
|
57 | /* from now the LCD only accepts 4 bit I/O, we can use lcd_command() */
|
58 | #else
|
59 | /*
|
60 | * Initialize LCD to 8 bit memory mapped mode
|
61 | */
|
62 |
|
63 | /* enable external SRAM (memory mapped lcd) and one wait state */
|
64 | MCUCR = _BV(SRE) | _BV(SRW);
|
65 |
|
66 | /* reset LCD */
|
67 | delay(16000); /* wait 16ms after power-on */
|
68 | lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */
|
69 | delay(4992); /* wait 5ms */
|
70 | lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */
|
71 | delay(64); /* wait 64us */
|
72 | lcd_write(LCD_FUNCTION_8BIT_1LINE,0); /* function set: 8bit interface */
|
73 | delay(64); /* wait 64us */
|
74 | #endif
|
75 |
|
76 | #if KS0073_4LINES_MODE
|
77 | /* Display with KS0073 controller requires special commands for enabling 4 line mode */
|
78 | lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_ON);
|
79 | lcd_command(KS0073_4LINES_MODE);
|
80 | lcd_command(KS0073_EXTENDED_FUNCTION_REGISTER_OFF);
|
81 | #else
|
82 | lcd_command(LCD_FUNCTION_DEFAULT); /* function set: display lines */
|
83 | #endif
|
84 | lcd_command(LCD_DISP_OFF); /* display off */
|
85 | lcd_clrscr(); /* display clear */
|
86 | lcd_command(LCD_MODE_DEFAULT); /* set entry mode */
|
87 | lcd_command(dispAttr); /* display/cursor control */
|
88 |
|
89 | }/* lcd_init */
|