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);
111 static uint16_t ao_lcd_time = 3;
116 volatile uint16_t count;
118 for (count = 0; count < ao_lcd_time; count++)
123 ao_lcd_send_ins(uint8_t data)
125 // printf("send ins %02x\n", data);
126 // ao_lcd_wait_idle();
129 ao_lcd_send_nibble(0, data >> 4);
130 ao_lcd_send_nibble(0, data & 0xf);
134 ao_lcd_send_data(uint8_t data)
136 // printf ("send data %02x\n", data);
137 // ao_lcd_wait_idle();
139 ao_lcd_send_nibble(1, data >> 4);
140 ao_lcd_send_nibble(1, data & 0x0f);
144 ao_lcd_send_string(char *string)
148 while ((c = (uint8_t) *string++))
152 #define AO_LCD_POWER_CONTROL 0x54
155 ao_lcd_contrast_set(uint8_t contrast)
157 ao_lcd_send_ins(AO_LCD_POWER_CONTROL | ((contrast >> 4) & 0x3));
158 ao_lcd_send_ins(0x70 | (contrast & 0xf));
164 ao_lcd_send_ins(0x01);
167 ao_lcd_send_ins(0x04 | 0x02);
173 /* get to 4bit mode */
174 ao_lcd_send_nibble(0, 0x3);
175 ao_lcd_send_nibble(0, 0x3);
176 ao_lcd_send_nibble(0, 0x3);
177 ao_lcd_send_nibble(0, 0x2);
180 ao_lcd_send_ins(0x28);
181 /* function set, instruction table 1 */
182 ao_lcd_send_ins(0x29);
185 ao_lcd_send_ins(0x14);
187 /* Power/icon/contrast control*/
188 ao_lcd_send_ins(AO_LCD_POWER_CONTROL);
190 /* Follower control */
191 ao_lcd_send_ins(0x6d);
192 ao_delay(AO_MS_TO_TICKS(200));
195 ao_lcd_contrast_set(0x18);
198 ao_lcd_send_ins(0x08 | 0x04);
206 ao_lcd_contrast(void)
209 if (ao_cmd_status == ao_cmd_success) {
210 printf("setting contrast to %02x\n", ao_cmd_lex_i);
211 ao_lcd_contrast_set(ao_cmd_lex_i & 0x3f);
216 ao_cmd_hex_nibble(void)
218 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
219 return ao_cmd_lex_c - '0';
220 if ('a' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'f')
221 return ao_cmd_lex_c - ('a' - 10);
222 if ('A' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'F')
223 return ao_cmd_lex_c - ('A' - 10);
224 ao_cmd_status = ao_cmd_syntax_error;
235 if (ao_cmd_status != ao_cmd_success)
237 ao_lcd_send_ins(0x80 | (ao_cmd_lex_i ? 0x40 : 0x00));
239 while (ao_cmd_lex_c != '\n') {
243 c = ao_cmd_hex_nibble() << 4;
245 c |= ao_cmd_hex_nibble();
252 ao_lcd_send_data(' ');
258 ao_lcd_delay_set(void)
261 if (ao_cmd_status == ao_cmd_success) {
262 printf("setting LCD delay to %d\n", ao_cmd_lex_i);
263 ao_lcd_time = ao_cmd_lex_i;
267 __code struct ao_cmds ao_lcd_cmds[] = {
268 { ao_lcd_start, "S\0Start LCD" },
269 { ao_lcd_contrast, "C <contrast>\0Set LCD contrast" },
270 { ao_lcd_string, "s <line> <string>\0Send string to LCD" },
271 { ao_lcd_delay_set, "t <delay>\0Set LCD delay" },
278 DDRB = (1 << PIN_RS) | (1 << PIN_E) | (1 << PIN_RW);
280 ao_cmd_register(&ao_lcd_cmds[0]);