-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
482 lines (409 loc) · 15.2 KB
/
lcd.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#define F_CPU 8000000UL
#include <stdbool.h>
#include "lcd.h"
#include "delay.h"
#include "definitions.h"
/* Low-level functions */
static void lcd_write(const lcd_config_s *config, interface_s *interface, uint8_t cmd, uint8_t is_data){
// Empty LCD command
lcd_cmd_s lcd_cmd;
lcd_cmd.data = 0x00;
lcd_cmd.ledk = 1;
lcd_cmd.rs = is_data;
lcd_cmd.rw = 0;
lcd_cmd.e = 1;
if (config->bus_width == LCD_BUS_WIDTH_4){
// Set control lines
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_OUTPUT_DELAY_US);
lcd_cmd.e = 1;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_OUTPUT_DELAY_US);
// Send upper nibble
lcd_cmd.data = cmd & 0xf0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(LEVEL_DELAY_US);
// High-low transition on Enable bit
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_HOLD_DELAY_US);
// Send lower nibble
lcd_cmd.e = 1;
lcd_cmd.data = (cmd & 0x0f) << 4;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(LEVEL_DELAY_US);
// High-low transition on Enable bit
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_HOLD_DELAY_US);
}
else{
// Send command word at once
lcd_cmd.data = cmd;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(LEVEL_DELAY_US);
// High-low transition on Enable bit
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_HOLD_DELAY_US);
}
delay_usec(CMD_DELAY_US);
}
static uint8_t lcd_read(const lcd_config_s *config, interface_s *interface, uint8_t is_data){
// Empty LCD command
lcd_cmd_s lcd_cmd;
lcd_cmd.data = 0xff;
lcd_cmd.ledk = 1;
lcd_cmd.rs = is_data;
lcd_cmd.rw = 1;
lcd_cmd.e = 1;
uint8_t read_value;
if (config->bus_width == LCD_BUS_WIDTH_4){
// Set control lines
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_OUTPUT_DELAY_US);
lcd_cmd.e = 1;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_OUTPUT_DELAY_US);
// Read upper nibble
read_value = interface->read_fun(interface->config) & 0xF0;
// High-low transition on Enable bit
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_HOLD_DELAY_US);
// Set control lines
lcd_cmd.e = 1;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_OUTPUT_DELAY_US);
// Read lower nibble
read_value |= (interface->read_fun(interface->config) & 0xF0) >> 4;
// High-low transition on Enable bit
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_HOLD_DELAY_US);
}
else{
// Set control lines
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_OUTPUT_DELAY_US);
// Read register at once
read_value = interface->read_fun(interface->config);
// High-low transition on Enable bit
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
delay_usec(DATA_HOLD_DELAY_US);
}
return read_value;
}
/* Misc. functions */
void lcd_clear(const lcd_config_s *config, interface_s *interface){
lcd_write(config, interface, LCD_CLEAR_DISPLAY, 0);
wait_busy(config, interface);
}
void lcd_home(const lcd_config_s *config, interface_s *interface){
lcd_write(config, interface, LCD_RETURN_HOME, 0);
wait_busy(config, interface);
}
void wait_busy(const lcd_config_s *config, interface_s *interface){
lcd_status_s status;
do{
status = lcd_get_status(config, interface);
} while(status.busy);
}
/* Setup functions */
int lcd_configure(lcd_config_s *config, lcd_bit_e bus_width, lcd_font_e font, uint8_t rows, uint8_t cols, uint8_t mode){
// Assign correct bus type
switch(bus_width){
// Intentional fall-through for all valid cases
case LCD_BUS_WIDTH_8:
case LCD_BUS_WIDTH_4:
config->bus_width = bus_width;
break;
default:
// Unknown bit type, error code
return -1;
}
// Assign correct font type
switch(font){
// Intentional fall-through for all valid cases
case LCD_FONT_5x8:
case LCD_FONT_5x10:
config->font = font;
break;
default:
// Unknown font type
return -1;
}
// Assign rows and columns
if (rows > 0 && rows <= MAX_ROWS_SUPPORTED && cols > 0){
config->rows = rows;
config->cols = cols;
}
else
return -1;
// Assign default states
config->state_display_control = LCD_DISPLAY_ON;
config->mode = mode;
// All good
return 0;
}
void lcd_init(lcd_config_s *config, interface_s * interface){
uint8_t config_cmd = LCD_FUNCTION_SET;
lcd_bit_e original_bus_width = config->bus_width;
// Send 3x 0x30 with delays in between in 8-bit mode first
// Delays are necessary because busy flag is still unavailable
config->bus_width = LCD_BUS_WIDTH_8;
delay_usec(BOOT_DELAY_US);
lcd_write(config, interface, LCD_FUNCTION_SET | LCD_8BIT, 0);
delay_usec(BOOT_DELAY_US/5);
lcd_write(config, interface, LCD_FUNCTION_SET | LCD_8BIT, 0);
delay_usec(BOOT_DELAY_US/10);
lcd_write(config, interface, LCD_FUNCTION_SET | LCD_8BIT, 0);
// Set bus width
if (original_bus_width == LCD_BUS_WIDTH_8)
config_cmd |= LCD_8BIT;
else
config_cmd |= LCD_4BIT;
// Set the mode
wait_busy(config, interface);
lcd_write(config, interface, config_cmd, 0);
// Reset bus to original bus width
config->bus_width = original_bus_width;
// Set row configuration
if (config->rows == 1)
config_cmd |= LCD_1_LINE;
else
config_cmd |= LCD_2_LINE;
// Set font configuration
if (config->font == LCD_FONT_5x8)
config_cmd |= LCD_5F8;
else
config_cmd |= LCD_5F10;
// Send the configuration command
wait_busy(config, interface);
lcd_write(config, interface, config_cmd, 0);
// Display on, no cursor, no blink
wait_busy(config, interface);
lcd_write(config, interface, LCD_DISPLAY_CONTROL | LCD_CURSOR_OFF | LCD_BLINK_OFF | LCD_DISPLAY_ON, 0);
// Clear display
wait_busy(config, interface);
lcd_write(config, interface, LCD_CLEAR_DISPLAY, 0);
// Entry mode set
wait_busy(config, interface);
lcd_write(config, interface, LCD_ENTRY_MODE_SET | LCD_INCREMENT | LCD_NO_SHIFT, 0);
// Home LCD
wait_busy(config, interface);
lcd_write(config, interface, LCD_RETURN_HOME, 0);
wait_busy(config, interface);
}
/* High level commands for user */
// Power switching
void lcd_power_on(interface_s *interface){
// Empty LCD command, no E pulse, only cathode
lcd_cmd_s lcd_cmd;
lcd_cmd.data = 0x00;
lcd_cmd.ledk = 1;
lcd_cmd.rs = 0;
lcd_cmd.rw = 0;
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
}
void lcd_power_off(interface_s *interface){
// Empty LCD command, no E pulse, only cathode
lcd_cmd_s lcd_cmd;
lcd_cmd.data = 0x00;
lcd_cmd.ledk = 0;
lcd_cmd.rs = 0;
lcd_cmd.rw = 0;
lcd_cmd.e = 0;
interface->write_fun(interface->config, lcd_cmd);
}
// Display functions
void lcd_display_on(lcd_config_s *config, interface_s *interface){
// Update state
config->state_display_control |= LCD_DISPLAY_ON;
// Use state
lcd_write(config, interface, LCD_DISPLAY_CONTROL | config->state_display_control, 0);
}
void lcd_display_off(lcd_config_s *config, interface_s *interface){
// Update state
config->state_display_control &= (~LCD_DISPLAY_ON);
// Use state
lcd_write(config, interface, LCD_DISPLAY_CONTROL | config->state_display_control, 0);
}
void lcd_blink_on(lcd_config_s *config, interface_s *interface){
// Update state
config->state_display_control |= LCD_BLINK_ON;
// Use state
lcd_write(config, interface, LCD_DISPLAY_CONTROL | config->state_display_control, 0);
}
void lcd_blink_off(lcd_config_s *config, interface_s *interface){
// Update state
config->state_display_control &= (~LCD_BLINK_ON);
// Use state
lcd_write(config, interface, LCD_DISPLAY_CONTROL | config->state_display_control, 0);
}
void lcd_mv_right(const lcd_config_s *config, interface_s *interface){
lcd_write(config, interface, LCD_CURSOR_SHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT, 0);
}
void lcd_mv_left(const lcd_config_s *config, interface_s *interface){
lcd_write(config, interface, LCD_CURSOR_SHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT, 0);
}
// Cursor functions
void lcd_cursor_on(lcd_config_s *config, interface_s *interface){
// Update state
config->state_display_control |= LCD_CURSOR_ON;
// Use state
lcd_write(config, interface, LCD_DISPLAY_CONTROL | config->state_display_control, 0);
}
void lcd_cursor_off(lcd_config_s *config, interface_s *interface){
// Update state
config->state_display_control &= (~LCD_CURSOR_ON);
// Use state
lcd_write(config, interface, LCD_DISPLAY_CONTROL | config->state_display_control, 0);
}
void lcd_mv_cursor_right(const lcd_config_s *config, interface_s *interface){
lcd_write(config, interface, LCD_CURSOR_SHIFT | LCD_MOVERIGHT, 0);
}
void lcd_mv_cursor_left(const lcd_config_s *config, interface_s *interface){
lcd_write(config, interface, LCD_CURSOR_SHIFT | LCD_MOVELEFT, 0);
}
void lcd_mv_cursor(const lcd_config_s *config, interface_s *interface, uint8_t row, uint8_t col){
// No write possible if target is outside of specified LCD area
if (col >= config->cols || row >= config->rows)
return;
// Calculate target address (7-bit, 8th bit is always set to indicate command)
uint8_t addr = LCD_SET_DDRAM_ADDR;
// Switch for line start addresses since they are not continuous in memory
switch(row){
case 0:
addr |= (LCD_LINE0_ADDR + col);
break;
case 1:
addr |= (LCD_LINE1_ADDR + col);
break;
case 2:
addr |= (LCD_LINE2_ADDR + col);
break;
case 3:
addr |= (LCD_LINE3_ADDR + col);
break;
default:
return;
}
// Set display address
lcd_write(config, interface, addr, 0);
}
lcd_pos_s lcd_get_cursor(const lcd_config_s *config, interface_s *interface){
lcd_pos_s curr_pos;
// Get value of address counter
lcd_status_s lcd_status = lcd_get_status(config, interface);
// Derive rows and columns from address
switch(config->rows){
case 2:
curr_pos.row = lcd_status.address >= LCD_LINE1_ADDR;
curr_pos.col = lcd_status.address - curr_pos.row * LCD_LINE1_ADDR;
break;
case 3:
// Intentional fall-through
case 4:
if (config->rows > 3 && lcd_status.address >= LCD_LINE3_ADDR){
curr_pos.row = 3;
curr_pos.col = lcd_status.address - LCD_LINE3_ADDR;
}
else if (lcd_status.address >= LCD_LINE2_ADDR){
curr_pos.row = 2;
curr_pos.col = lcd_status.address - LCD_LINE2_ADDR;
}
else if (lcd_status.address >= LCD_LINE1_ADDR){
curr_pos.row = 1;
curr_pos.col = lcd_status.address - LCD_LINE1_ADDR;
}
else{
curr_pos.row = 0;
curr_pos.col = lcd_status.address;
}
break;
case 1:
// Intentional fall-through
default:
// Row is always one
curr_pos.row = 0;
curr_pos.col = lcd_status.address;
}
return curr_pos;
}
// Print functions
void lcd_putc(const lcd_config_s *config, interface_s *interface, char c){
lcd_write(config, interface, (uint8_t) c, 1);
}
void lcd_printf(const lcd_config_s *config, interface_s *interface, char *s){
// Maximum string length is rows * columns of the display
uint16_t size = config->rows * config->cols;
lcd_pos_s init_pos = lcd_get_cursor(config, interface);
uint8_t counter = 0;
// Print characters one by one
// Stop after rows*cols characters or at null terminator
for(char *c = s; *c != '\0' && counter < size; c++, counter++){
// Wrap cursor to next line
if (config->mode == LCD_MODE_WRAP && (init_pos.col + counter) >= config->cols){
// Stop if last row is reached, no further wrap possible
init_pos.row++;
if (init_pos.row >= config->rows)
return;
// Reset counter for next row and go to start of next row
counter = 0;
lcd_mv_cursor(config, interface, init_pos.row, 0);
}
// Print next character at current cursor position
// Cursor is automatically incremented
lcd_putc(config, interface, *c);
}
}
void lcd_printf_at(const lcd_config_s *config, interface_s *interface, char *s, uint8_t row, uint8_t col){
lcd_mv_cursor(config, interface, row, col);
lcd_printf(config, interface, s);
}
char lcd_getc(const lcd_config_s *config, interface_s *interface){
return (char) lcd_read(config, interface, 1);
}
// LCD Status
lcd_status_s lcd_get_status(const lcd_config_s *config, interface_s *interface){
uint8_t temp;
lcd_status_s status;
temp = lcd_read(config, interface, 0);
status.address = temp & ~(1 << 7);
status.busy = temp & (1 << 7);
return status;
}
// Special characters
void lcd_create_custom(const lcd_config_s *config, interface_s *interface, uint8_t addr, uint8_t *character){
int max_row;
// Get number of rows to write to CGRAM
switch (config->font){
case LCD_FONT_5x10:
// Only four characters possible
if (addr >= 0x04)
return;
addr <<= 4;
max_row = 10;
break;
case LCD_FONT_5x8:
// Intentional fall-through
default:
// Only eight characters possible
if (addr >= 0x08)
return;
addr <<= 3;
max_row = 8;
}
// Set initial CGRAM address
lcd_write(config, interface, LCD_SET_CGRAM_ADDR | addr, 0);
// Write data to CGRAM address
// CGRAM address is incremented automatically
for (int row=0; row < max_row; row++, character++){
lcd_write(config, interface, *character, 1);
}
}