1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/*
* 4-bit HD44780 LCD support
*
* Copyright (C) 2007-2012 Michael Buesch <m@bues.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "lcd.h"
#include "util.h"
#include <avr/io.h>
#include <string.h>
#include <stdio.h>
#define LCD_NR_CHARS (LCD_NR_LINES * LCD_NR_COLUMNS)
#define LCD_BUFFER_SIZE LCD_NR_CHARS
static uint8_t lcd_buffer[LCD_BUFFER_SIZE];
uint8_t lcd_cursor_pos;
static const uint8_t PROGMEM sad_smiley[] = {
0x00, 0x0A, 0x00, 0x00, 0x0E, 0x11, 0x00, 0x00,
};
/** lcd_enable_pulse - Send an E-pulse */
static void lcd_enable_pulse(void)
{
LCD_PORT |= LCD_PIN_E;
__asm__ __volatile__("nop\n\t"
"nop\n\t"
: : : "memory");
LCD_PORT &= ~LCD_PIN_E;
}
/** lcd_write - Write one byte to the LCD. */
static void lcd_write(uint8_t data)
{
LCD_PORT = (LCD_PORT & ~(0xF << LCD_DATA_SHIFT)) |
(((data & 0xF0) >> 4) << LCD_DATA_SHIFT);
lcd_enable_pulse();
LCD_PORT = (LCD_PORT & ~(0xF << LCD_DATA_SHIFT)) |
((data & 0x0F) << LCD_DATA_SHIFT);
lcd_enable_pulse();
_delay_us(50);
}
/** lcd_data - Send data to the LCD. */
static void lcd_data(uint8_t data)
{
LCD_PORT |= LCD_PIN_RS;
lcd_write(data);
}
/** lcd_command - Send command to the LCD. */
static void lcd_command(uint8_t command)
{
LCD_PORT &= ~LCD_PIN_RS;
lcd_write(command);
}
/** lcd_cmd_clear - Clear LCD and return cursor to home position. */
static void lcd_cmd_clear(void)
{
lcd_command(0x01);
_delay_ms(2);
}
/** lcd_cmd_home - Move cursor to home position. */
static void lcd_cmd_home(void)
{
lcd_command(0x02);
_delay_ms(2);
}
/** lcd_cmd_entrymode - Set entry mode.
* @cursor_inc: 1 = increment cursor, 0 = decrement cursor.
* @display_shift: 1 = shift display, 0 = don't shift display.
*/
static void lcd_cmd_entrymode(uint8_t cursor_inc,
uint8_t display_shift)
{
lcd_command(0x04 |
(cursor_inc ? 0x02 : 0x00) |
(display_shift ? 0x01 : 0x00));
}
/** lcd_cmd_dispctl - Display control.
* @display_on: 1 = turn display on, 0 = turn display off.
* @cursor_on: 1 = turn cursor on, 0 = turn cursor off.
* @cursor_blink: 1 = blink cursor, 0 = don't blink cursor.
*/
void lcd_cmd_dispctl(uint8_t display_on,
uint8_t cursor_on,
uint8_t cursor_blink)
{
lcd_command(0x08 |
(display_on ? 0x04 : 0x00) |
(cursor_on ? 0x02 : 0x00) |
(cursor_blink ? 0x01 : 0x00));
}
/** lcd_cmd_shiftctl - Display shift control.
* @shift_display: 1 = move display, 0 = move cursor.
* @shift_right: 1 = right direction, 0 = left direction.
*/
static void lcd_cmd_shiftctl(uint8_t shift_display,
uint8_t shift_right)
{
lcd_command(0x10 |
(shift_display ? 0x08 : 0x00) |
(shift_right ? 0x04 : 0x00));
}
/** lcd_cmd_funcset - Set basic display function.
* @eight_bit: 1 = 8bit mode, 0 = 4bit mode.
* @two_lines: 1 = two rows, 0 = one row.
* @font_5x10: 1 = 5x10 font, 0 = 5x8 font.
*/
static void lcd_cmd_funcset(uint8_t eight_bit,
uint8_t two_lines,
uint8_t font_5x10)
{
lcd_command(0x20 |
(eight_bit ? 0x10 : 0x00) |
(two_lines ? 0x08 : 0x00) |
(font_5x10 ? 0x04 : 0x00));
}
/** lcd_cmd_cgram_addr_set - Move CGRAM address pointer.
* @address: The address to move to.
*/
static void lcd_cmd_cgram_addr_set(uint8_t address)
{
lcd_command(0x40 | (address & 0x3F));
}
/** lcd_cmd_cursor - Move cursor (DDRAM address).
* @line: Line number. 0 - 1.
* @column: Column number. 0 - 15.
*/
void lcd_cmd_cursor(uint8_t line, uint8_t column)
{
lcd_command(0x80 |
((line & (LCD_NR_LINES - 1)) << 6) |
(column & (LCD_NR_COLUMNS - 1)));
}
/** lcd_clear_buffer - Clear the software buffer. */
void lcd_clear_buffer(void)
{
memset(lcd_buffer, ' ', LCD_BUFFER_SIZE);
lcd_cursor_pos = 0;
}
/** lcd_commit - Write the software buffer to the display. */
void lcd_commit(void)
{
uint8_t line, col;
const uint8_t *buf = lcd_buffer;
for (line = 0; line < LCD_NR_LINES; line++) {
lcd_cmd_cursor(line, 0);
for (col = 0; col < LCD_NR_COLUMNS; col++)
lcd_data(*buf++);
}
lcd_cmd_cursor(lcd_getline(), lcd_getcolumn());
}
/** lcd_put_char - Put one character into software buffer. */
void lcd_put_char(char c)
{
uint8_t line, column;
if (c == '\r') {
lcd_cursor(lcd_getline(), 0);
} else if (c == '\n') {
line = lcd_getline() + 1;
lcd_cursor(line & (LCD_NR_LINES - 1), 0);
} else {
lcd_buffer[lcd_cursor_pos] = c;
column = (lcd_getcolumn() + 1) & (LCD_NR_COLUMNS - 1);
lcd_cursor(lcd_getline(), column);
}
}
static int lcd_stream_putchar(char c, FILE *unused)
{
lcd_put_char(c);
return 0;
}
static FILE lcd_fstream = FDEV_SETUP_STREAM(lcd_stream_putchar, NULL,
_FDEV_SETUP_WRITE);
void _lcd_printf(const char PROGPTR *_fmt, ...)
{
char fmt[64];
va_list args;
strlcpy_P(fmt, _fmt, sizeof(fmt));
va_start(args, _fmt);
vfprintf(&lcd_fstream, fmt, args);
va_end(args);
}
void lcd_put_pstr(const char PROGPTR *str)
{
uint8_t c;
for ( ; ; str++) {
c = pgm_read_byte(str);
if (c == '\0')
break;
lcd_put_char(c);
}
}
void lcd_put_mstr(const char *str)
{
uint8_t c;
for ( ; ; str++) {
c = *str;
if (c == '\0')
break;
lcd_put_char(c);
}
}
void lcd_shift(int8_t count)
{
int8_t i, new_pos;
if (!count)
return;
if (count < 0) { /* shift left */
count = -count;
if (count >= LCD_BUFFER_SIZE) {
lcd_clear_buffer();
return;
}
for (i = count; i < LCD_BUFFER_SIZE; i++) {
new_pos = i - count;
lcd_buffer[new_pos] = lcd_buffer[i];
lcd_buffer[i] = ' ';
}
if (count > lcd_cursor_pos)
lcd_cursor_pos = 0;
else
lcd_cursor_pos -= count;
} else { /* shift right */
if (count >= LCD_BUFFER_SIZE) {
lcd_clear_buffer();
lcd_cursor_pos = LCD_BUFFER_SIZE - 1;
}
for (i = LCD_BUFFER_SIZE - count - 1; i >= 0; i--) {
new_pos = i + count;
lcd_buffer[new_pos] = lcd_buffer[i];
lcd_buffer[i] = ' ';
}
lcd_cursor_pos += count;
if (lcd_cursor_pos >= LCD_BUFFER_SIZE)
lcd_cursor_pos = LCD_BUFFER_SIZE - 1;
}
}
/** lcd_upload_char - Upload a user defined character to CGRAM.
* @char_code: The character code to use.
* @char_tab: The character bitmap. The bitmap has got one byte
* per character pixel row. The upper 3 bits of each byte
* are unused. For 5x10 char-pixel displays, the char_tab
* is 10 bytes long. For 5x8 char-pixel displays, it's 8 bytes.
*/
void lcd_upload_char(uint8_t char_code,
const uint8_t PROGPTR *char_tab)
{
uint8_t i, c, address;
address = char_code << (LCD_FONT_5x10 ? 4 : 3);
for (i = 0; i < (LCD_FONT_5x10 ? 10 : 8); i++) {
lcd_cmd_cgram_addr_set(address);
c = pgm_read_byte(char_tab);
lcd_data(c);
address++;
char_tab++;
}
lcd_cmd_cursor(lcd_getline(), lcd_getcolumn());
}
/** lcd_init - Initialize the LCD. */
void lcd_init(void)
{
uint8_t i;
LCD_DDR |= (0xF << LCD_DATA_SHIFT) |
LCD_PIN_E | LCD_PIN_RS;
/* Force it into 8-bit mode first */
LCD_PORT &= ~(LCD_PIN_E | LCD_PIN_RS |
(0xF << LCD_DATA_SHIFT));
LCD_PORT |= (0x03 << LCD_DATA_SHIFT);
_delay_ms(200);
for (i = 3; i; i--) {
lcd_enable_pulse();
_delay_ms(5);
}
/* We're in a known state. Enable 4-bit mode. */
LCD_PORT = (LCD_PORT & ~(0xF << LCD_DATA_SHIFT)) |
(0x02 << LCD_DATA_SHIFT);
lcd_enable_pulse();
_delay_ms(10);
lcd_cmd_funcset(0,
(LCD_NR_LINES > 1) ? 1 : 0,
LCD_FONT_5x10 ? 1 : 0);
lcd_cmd_dispctl(0, 0, 0);
lcd_cmd_clear();
lcd_cmd_entrymode(1, 0);
lcd_cmd_shiftctl(0, 0);
lcd_cmd_dispctl(1, 0, 0);
lcd_cmd_home();
lcd_cursor_pos = 0;
lcd_clear_buffer();
lcd_commit();
lcd_upload_char(0x01, sad_smiley);
}
|