2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #define LCD_PORT PORTB
28 ao_lcd_set_bits(uint8_t bits)
31 printf("\tLCD data %x RS %d R/W %d E %d\n",
33 (bits & (1 << PIN_RS)) ? 1 : 0,
34 (bits & (1 << PIN_RW)) ? 1 : 0,
35 (bits & (1 << PIN_E)) ? 1 : 0);
40 if (bits & (1 << PIN_RW))
41 printf("\tLCD input %x\n", PINB);
46 ao_lcd_get_nibble(uint8_t rs)
48 uint8_t data = (rs ? (1 << PIN_RS) : 0) | (1 << PIN_RW);
51 DDRB = (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
52 ao_lcd_set_bits(data);
53 ao_lcd_set_bits(data | (1 << PIN_E));
55 ao_lcd_set_bits(data);
60 ao_lcd_get_status(void)
65 high = ao_lcd_get_nibble(0);
66 low = ao_lcd_get_nibble(0);
67 data = (high << 4) | low;
68 printf ("\tLCD status %02x\n", data);
78 high = ao_lcd_get_nibble(1);
79 low = ao_lcd_get_nibble(1);
80 data = (high << 4) | low;
81 printf ("\tLCD data %02x\n", data);
86 ao_lcd_wait_idle(void)
92 status = ao_lcd_get_status();
95 printf("idle timeout\n");
98 } while (0); /* status & 0x80); */
102 ao_lcd_send_nibble(uint8_t rs, uint8_t data)
104 data = (data & 0xf) | (rs ? (1 << PIN_RS) : 0);
105 DDRB = (0xf) | (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
106 ao_lcd_set_bits(data);
107 ao_lcd_set_bits(data | (1 << PIN_E));
108 ao_lcd_set_bits(data);
112 ao_lcd_send_ins(uint8_t data)
114 printf("send ins %02x\n", data);
116 ao_lcd_send_nibble(0, data >> 4);
117 ao_lcd_send_nibble(0, data & 0xf);
121 ao_lcd_send_data(uint8_t data)
123 printf ("send data %02x\n", data);
125 ao_lcd_send_nibble(1, data >> 4);
126 ao_lcd_send_nibble(1, data & 0x0f);
130 ao_lcd_send_string(char *string)
134 while ((c = (uint8_t) *string++))
138 #define AO_LCD_POWER_CONTROL 0x54
141 ao_lcd_contrast_set(uint8_t contrast)
143 ao_lcd_send_ins(AO_LCD_POWER_CONTROL | ((contrast >> 4) & 0x3));
144 ao_lcd_send_ins(0x70 | (contrast & 0xf));
150 ao_lcd_send_ins(0x01);
153 ao_lcd_send_ins(0x04 | 0x02);
159 /* get to 4bit mode */
160 ao_lcd_send_nibble(0, 0x3);
161 ao_lcd_send_nibble(0, 0x3);
162 ao_lcd_send_nibble(0, 0x3);
163 ao_lcd_send_nibble(0, 0x2);
166 ao_lcd_send_ins(0x28);
167 /* function set, instruction table 1 */
168 ao_lcd_send_ins(0x29);
171 ao_lcd_send_ins(0x14);
173 /* Power/icon/contrast control*/
174 ao_lcd_send_ins(AO_LCD_POWER_CONTROL);
176 /* Follower control */
177 ao_lcd_send_ins(0x6d);
178 ao_delay(AO_MS_TO_TICKS(200));
181 ao_lcd_contrast_set(0x18);
184 ao_lcd_send_ins(0x08 | 0x04);
192 ao_lcd_contrast(void)
195 if (ao_cmd_status == ao_cmd_success) {
196 printf("setting contrast to %02x\n", ao_cmd_lex_i);
197 ao_lcd_contrast_set(ao_cmd_lex_i & 0x3f);
206 while (ao_cmd_lex_c != '\n') {
207 ao_lcd_send_data(ao_cmd_lex_c);
212 __code struct ao_cmds ao_lcd_cmds[] = {
213 { ao_lcd_start, "S\0Start LCD" },
214 { ao_lcd_contrast, "C\0Set LCD contrast" },
215 { ao_lcd_string, "s\0Send string to LCD" },
222 DDRB = (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
224 ao_cmd_register(&ao_lcd_cmds[0]);