-
Notifications
You must be signed in to change notification settings - Fork 6
/
hd44780.c
529 lines (412 loc) · 18.6 KB
/
hd44780.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include <stdlib.h>
#include <stdint.h>
#include "hd44780.h"
#ifndef NDEBUG
#define HD44780_ASSERT(x) \
{ \
if (!(x)) \
{ \
if (display->cfg.assert_failure_handler != NULL) \
display->cfg.assert_failure_handler(__FILE__, __LINE__); \
} \
}
#define HD44780_RETURN_ASSERT(x,ret) \
do { \
int condition = (x); \
HD44780_ASSERT(condition) \
if (!condition) \
return (ret); \
} while (0)
#else
#define HD44780_ASSERT(x)
#define HD44780_RETURN_ASSERT(x,ret)
#endif
#define HD44780_RETURN_IF_ERROR(result) \
do {\
HD44780_Result r = (result); \
if (r != HD44780_RESULT_OK) \
return r; \
} while (0)
#define HD44780_CMD_CLEARDISPLAY 0x01
#define HD44780_CMD_RETURNHOME 0x02
#define HD44780_CMD_ENTRYMODESET 0x04
#define HD44780_CMD_DISPLAYCONTROL 0x08
#define HD44780_CMD_CURSORSHIFT 0x10
#define HD44780_CMD_FUNCTIONSET 0x20
#define HD44780_CMD_SETCGRAMADDR 0x40
#define HD44780_CMD_SETDDRAMADDR 0x80
// flags for display entry mode
#define HD44780_FLAG_ENTRYRIGHT 0x00
#define HD44780_FLAG_ENTRYLEFT 0x02
#define HD44780_FLAG_ENTRYSHIFTINCREMENT 0x01
#define HD44780_FLAG_ENTRYSHIFTDECREMENT 0x00
// flags for display on/off control
#define HD44780_FLAG_DISPLAYON 0x04
#define HD44780_FLAG_DISPLAYOFF 0x00
#define HD44780_FLAG_CURSORON 0x02
#define HD44780_FLAG_CURSOROFF 0x00
#define HD44780_FLAG_BLINKON 0x01
#define HD44780_FLAG_BLINKOFF 0x00
// flags for display/cursor shift
#define HD44780_FLAG_DISPLAYMOVE 0x08
#define HD44780_FLAG_CURSORMOVE 0x00
#define HD44780_FLAG_MOVERIGHT 0x04
#define HD44780_FLAG_MOVELEFT 0x00
// flags for function set
#define HD44780_FLAG_8BITMODE 0x10
#define HD44780_FLAG_4BITMODE 0x00
#define HD44780_FLAG_2LINE 0x08
#define HD44780_FLAG_1LINE 0x00
#define HD44780_FLAG_5x10DOTS 0x04
#define HD44780_FLAG_5x8DOTS 0x00
// encoding stuff
#define HD44780_ENC_EDGE 0x80
#define HD44780_ENC_CONST 0xC0
reencoding_table[]= { 0x41,0xA0,0x42,0xA1,0xE0,0x45,0xA3,0xA4,
0xA5,0x03,0x4B,0xA7,0x4D,0x48,0x4F,0xA8,
0x50,0x43,0x54,0xA9,0xAA,0x58,0xE1,0xAB,
0xAC,0xE2,0xAD,0xAE,0x02,0xAF,0xB0,0xB1,
0x61,0xB2,0xB3,0xB4,0xE3,0x65,0xB6,0xB7,
0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0x6F,0xBE,
0x70,0x63,0xBF,0x79,0xE4,0x78,0xE5,0xC0,
0xC1,0xE6,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7 };
HD44780_Result hd44780_config(HD44780 *display);
HD44780_Result hd44780_command(HD44780 *display, uint8_t value);
HD44780_Result hd44780_write_byte(HD44780 *display, uint8_t value);
HD44780_Result hd44780_send(HD44780 *display, uint8_t value, HD44780_PinState rs_mode);
HD44780_Result hd44780_write_bits(HD44780 *display, uint8_t value);
HD44780_Result hd44780_read_bits(HD44780 *display, uint8_t *value);
HD44780_Result hd44780_pulse_enable_pin(HD44780 *display);
HD44780_Result hd44780_init(HD44780 *display, HD44780_Mode mode,
const HD44780_Config *config, uint8_t columns, uint8_t rows, HD44780_CharSize charsize)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(config != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(config->gpios != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(config->gpios->write != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(config->delay_microseconds != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(columns > 0, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(rows > 0, HD44780_RESULT_ERROR);
display->cfg = *config;
HD44780_GPIO_Interface *const gpios = display->cfg.gpios;
HD44780_DelayMicrosecondsFn delay_microseconds = display->cfg.delay_microseconds;
if (gpios->configure != NULL)
{
HD44780_RETURN_IF_ERROR(gpios->configure(gpios, HD44780_PIN_RS, HD44780_PINMODE_OUTPUT));
HD44780_RETURN_IF_ERROR(gpios->configure(gpios, HD44780_PIN_ENABLE, HD44780_PINMODE_OUTPUT));
if (display->cfg.options & HD44780_OPT_USE_RW)
HD44780_RETURN_IF_ERROR(gpios->configure(gpios, HD44780_PIN_RW, HD44780_PINMODE_OUTPUT));
if (display->cfg.options & HD44780_OPT_USE_BACKLIGHT)
HD44780_RETURN_IF_ERROR(gpios->configure(gpios, HD44780_PIN_BACKLIGHT, HD44780_PINMODE_OUTPUT));
}
if (display->cfg.options & HD44780_OPT_USE_BACKLIGHT)
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_BACKLIGHT, HD44780_PINSTATE_LOW));
if (mode == HD44780_MODE_4BIT)
{
display->displayfunction = HD44780_FLAG_4BITMODE | HD44780_FLAG_1LINE | HD44780_FLAG_5x8DOTS;
display->dp_first = HD44780_PIN_DP4;
display->dp_amount = 4;
}
else
{
display->displayfunction = HD44780_FLAG_8BITMODE | HD44780_FLAG_1LINE | HD44780_FLAG_5x8DOTS;
display->dp_first = HD44780_PIN_DP0;
display->dp_amount = 8;
}
/* For some 1 line displays you can select a 10 pixel high font */
if (charsize == HD44780_CHARSIZE_5x10 && rows == 1)
display->displayfunction |= HD44780_FLAG_5x10DOTS;
if (rows > 1)
display->displayfunction |= HD44780_FLAG_2LINE;
display->columns_amount = columns;
display->lines_amount = rows;
display->currline = 0;
if (hd44780_config(display) != HD44780_RESULT_OK)
return HD44780_RESULT_ERROR;
/* Put the LCD into 4 bit or 8 bit mode */
if (display->displayfunction & HD44780_FLAG_8BITMODE)
{
/*** This is according to the hitachi HD44780 datasheet page 45 figure 23 ***/
/* Send function set command sequence */
HD44780_RETURN_IF_ERROR(hd44780_command(display, HD44780_CMD_FUNCTIONSET | display->displayfunction));
delay_microseconds(4500); // wait more than 4.1ms
/* Second try */
HD44780_RETURN_IF_ERROR(hd44780_command(display, HD44780_CMD_FUNCTIONSET | display->displayfunction));
delay_microseconds(150);
/* Third go */
HD44780_RETURN_IF_ERROR(hd44780_command(display, HD44780_CMD_FUNCTIONSET | display->displayfunction));
}
else
{
/*** This is according to the hitachi HD44780 datasheet figure 24, pg 46 ***/
/* We start in 8bit mode, try to set 4 bit mode */
HD44780_RETURN_IF_ERROR(hd44780_write_bits(display, 0x03));
delay_microseconds(4500); // wait min 4.1ms
/* Second try */
HD44780_RETURN_IF_ERROR(hd44780_write_bits(display, 0x03));
delay_microseconds(4500); // wait min 4.1ms
/* Third go! */
HD44780_RETURN_IF_ERROR(hd44780_write_bits(display, 0x03));
delay_microseconds(150);
/* Finally, set to 4-bit interface */
HD44780_RETURN_IF_ERROR(hd44780_write_bits(display, 0x02));
}
/* Finally, set # lines, font size, etc. */
HD44780_RETURN_IF_ERROR(hd44780_command(display, HD44780_CMD_FUNCTIONSET | display->displayfunction));
/* Turn the display on with no cursor or blinking default */
display->displaycontrol = HD44780_FLAG_DISPLAYON | HD44780_FLAG_CURSOROFF | HD44780_FLAG_BLINKOFF;
HD44780_RETURN_IF_ERROR(hd44780_display_on(display));
/* Clear it off */
HD44780_RETURN_IF_ERROR(hd44780_clear(display));
/* Initialize to default text direction (for romance languages) */
display->displaymode = HD44780_FLAG_ENTRYLEFT | HD44780_FLAG_ENTRYSHIFTDECREMENT;
/* Set the entry mode */
HD44780_RETURN_IF_ERROR(hd44780_command(display, HD44780_CMD_ENTRYMODESET | display->displaymode));
return HD44780_RESULT_OK;
}
char encode_cyrillic(char c)
{
return c==0xA8?0xA2:c==0xB8?0xB5:reencoding_table[c-HD44780_ENC_CONST];
}
HD44780_Result hd44780_write_char(HD44780 *display, char c)
{
return hd44780_write_byte(display, (uint8_t)(c<HD44780_ENC_EDGE?c:encode_cyrillic(c)));
}
HD44780_Result hd44780_write_string(HD44780 *display, const char *s)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(s != NULL, HD44780_RESULT_ERROR);
while (*s)
HD44780_RETURN_IF_ERROR(hd44780_write_char(display, *s++));
return HD44780_RESULT_OK;
}
HD44780_Result hd44780_clear(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.delay_microseconds != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_IF_ERROR(hd44780_command(display, HD44780_CMD_CLEARDISPLAY)); // clear display, set cursor position to zero
display->cfg.delay_microseconds(3000); // this command takes a long time!
return HD44780_RESULT_OK;
}
HD44780_Result hd44780_home(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.delay_microseconds != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_IF_ERROR(hd44780_command(display, HD44780_CMD_RETURNHOME));
display->cfg.delay_microseconds(3000); // this command takes a long time!
return HD44780_RESULT_OK;
}
HD44780_Result hd44780_scroll_left(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
return hd44780_command(display,
HD44780_CMD_CURSORSHIFT | HD44780_FLAG_DISPLAYMOVE | HD44780_FLAG_MOVELEFT);
}
HD44780_Result hd44780_scroll_right(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
return hd44780_command(display,
HD44780_CMD_CURSORSHIFT | HD44780_FLAG_DISPLAYMOVE | HD44780_FLAG_MOVERIGHT);
}
HD44780_Result hd44780_left_to_right(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaymode |= HD44780_FLAG_ENTRYLEFT;
return hd44780_command(display, HD44780_CMD_ENTRYMODESET | display->displaymode);
}
HD44780_Result hd44780_right_to_left(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaymode &= ~HD44780_FLAG_ENTRYLEFT;
return hd44780_command(display, HD44780_CMD_ENTRYMODESET | display->displaymode);
}
/* FIXME moves the cursor out of screen */
HD44780_Result hd44780_create_char(HD44780 *display, uint8_t location, const uint8_t *charmap)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(charmap != NULL, HD44780_RESULT_ERROR);
location &= 0x7; // we only have 8 locations 0-7
HD44780_RETURN_IF_ERROR(hd44780_command(display, HD44780_CMD_SETCGRAMADDR | (location << 3)));
for (unsigned i = 0; i < 8; ++i)
HD44780_RETURN_IF_ERROR(hd44780_write_byte(display, charmap[i]));
return HD44780_RESULT_OK;
}
HD44780_Result hd44780_move_cursor(HD44780 *display, uint8_t column, uint8_t row)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
static const int row_offsets[] = { 0x00, 0x40, 0x10, 0x50 };
if (row > display->lines_amount)
row = display->lines_amount - 1; // we count rows starting with zero
return hd44780_command(display, HD44780_CMD_SETDDRAMADDR | (column + row_offsets[row]));
}
HD44780_Result hd44780_display_on(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaycontrol |= HD44780_FLAG_DISPLAYON;
return hd44780_command(display, HD44780_CMD_DISPLAYCONTROL | display->displaycontrol);
}
HD44780_Result hd44780_display_off(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaycontrol &= ~HD44780_FLAG_DISPLAYON;
return hd44780_command(display, HD44780_CMD_DISPLAYCONTROL | display->displaycontrol);
}
HD44780_Result hd44780_blink_on(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaycontrol |= HD44780_FLAG_BLINKON;
return hd44780_command(display, HD44780_CMD_DISPLAYCONTROL | display->displaycontrol);
}
HD44780_Result hd44780_blink_off(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaycontrol &= ~HD44780_FLAG_BLINKON;
return hd44780_command(display, HD44780_CMD_DISPLAYCONTROL | display->displaycontrol);
}
HD44780_Result hd44780_cursor_on(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaycontrol |= HD44780_FLAG_CURSORON;
return hd44780_command(display, HD44780_CMD_DISPLAYCONTROL | display->displaycontrol);
}
HD44780_Result hd44780_cursor_off(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaycontrol &= ~HD44780_FLAG_CURSORON;
return hd44780_command(display, HD44780_CMD_DISPLAYCONTROL | display->displaycontrol);
}
HD44780_Result hd44780_autoscroll_on(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaymode |= HD44780_FLAG_ENTRYSHIFTINCREMENT;
return hd44780_command(display, HD44780_CMD_ENTRYMODESET | display->displaymode);
}
HD44780_Result hd44780_autoscroll_off(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
display->displaymode &= ~HD44780_FLAG_ENTRYSHIFTINCREMENT;
return hd44780_command(display, HD44780_CMD_ENTRYMODESET | display->displaymode);
}
HD44780_Result hd44780_backlight_on(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->write != NULL, HD44780_RESULT_ERROR);
return display->cfg.gpios->write(display->cfg.gpios, HD44780_PIN_BACKLIGHT, HD44780_PINSTATE_HIGH);
}
HD44780_Result hd44780_backlight_off(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->write != NULL, HD44780_RESULT_ERROR);
return display->cfg.gpios->write(display->cfg.gpios, HD44780_PIN_BACKLIGHT, HD44780_PINSTATE_LOW);
}
/*----------------------*/
/* Internal API */
/*----------------------*/
HD44780_Result hd44780_config(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.delay_microseconds != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->write != NULL, HD44780_RESULT_ERROR);
HD44780_GPIO_Interface *const gpios = display->cfg.gpios;
for (unsigned i = 0; i < display->dp_amount; ++i)
{
if (gpios->configure != NULL)
HD44780_RETURN_IF_ERROR(gpios->configure(gpios, display->dp_first + i, HD44780_PINMODE_OUTPUT));
HD44780_RETURN_IF_ERROR(gpios->write(gpios, display->dp_first + i, HD44780_PINSTATE_LOW));
}
/* SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
* according to datasheet, we need at least 40ms after power rises above 2.7V
* before sending commands.
*/
display->cfg.delay_microseconds(50000);
/* Now we pull both RS and R/W low to begin commands */
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_RS, HD44780_PINSTATE_LOW));
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_ENABLE, HD44780_PINSTATE_LOW));
if (display->cfg.options & HD44780_OPT_USE_RW)
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_RW, HD44780_PINSTATE_LOW));
return HD44780_RESULT_OK;
}
HD44780_Result hd44780_command(HD44780 *display, uint8_t value)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
return hd44780_send(display, value, HD44780_PINSTATE_LOW);
}
HD44780_Result hd44780_write_byte(HD44780 *display, uint8_t value)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
return hd44780_send(display, value, HD44780_PINSTATE_HIGH);
}
HD44780_Result hd44780_send(HD44780 *display, uint8_t value, HD44780_PinState rs_mode)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->write != NULL, HD44780_RESULT_ERROR);
HD44780_GPIO_Interface *const gpios = display->cfg.gpios;
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_RS, rs_mode));
if (display->cfg.options & HD44780_OPT_USE_RW)
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_RW, HD44780_PINSTATE_LOW));
if (display->displayfunction & HD44780_FLAG_8BITMODE)
HD44780_RETURN_IF_ERROR(hd44780_write_bits(display, value));
else
{
HD44780_RETURN_IF_ERROR(hd44780_write_bits(display, value >> 4));
HD44780_RETURN_IF_ERROR(hd44780_write_bits(display, value));
}
return HD44780_RESULT_OK;
}
HD44780_Result hd44780_write_bits(HD44780 *display, uint8_t value)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->configure != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->write != NULL, HD44780_RESULT_ERROR);
HD44780_GPIO_Interface *const gpios = display->cfg.gpios;
for (unsigned i = 0; i < display->dp_amount; ++i)
{
HD44780_RETURN_IF_ERROR(gpios->configure(gpios, display->dp_first + i, HD44780_PINMODE_OUTPUT));
HD44780_RETURN_IF_ERROR(gpios->write(gpios, display->dp_first + i, (value >> i) & 0x01));
}
HD44780_RETURN_IF_ERROR(hd44780_pulse_enable_pin(display));
return HD44780_RESULT_OK;
}
HD44780_Result hd44780_read_bits(HD44780 *display, uint8_t *value)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->configure != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->write != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(value != NULL, HD44780_RESULT_ERROR);
HD44780_GPIO_Interface *const gpios = display->cfg.gpios;
if (display->cfg.options & HD44780_OPT_USE_RW)
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_RW, HD44780_PINSTATE_LOW));
uint8_t value_read = 0;
uint8_t bit = 0;
for (unsigned i = 0; i < display->dp_amount; ++i)
{
HD44780_RETURN_IF_ERROR(gpios->configure(gpios, display->dp_first + i, HD44780_PINMODE_INPUT));
HD44780_RETURN_IF_ERROR(gpios->read(gpios, display->dp_first + i, &bit));
value_read = (value_read << i) | (bit & 0x01);
}
HD44780_RETURN_IF_ERROR(hd44780_pulse_enable_pin(display));
*value = value_read;
return HD44780_RESULT_OK;
}
HD44780_Result hd44780_pulse_enable_pin(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.gpios->write != NULL, HD44780_RESULT_ERROR);
HD44780_RETURN_ASSERT(display->cfg.delay_microseconds != NULL, HD44780_RESULT_ERROR);
HD44780_GPIO_Interface *const gpios = display->cfg.gpios;
HD44780_DelayMicrosecondsFn delay_microseconds = display->cfg.delay_microseconds;
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_ENABLE, HD44780_PINSTATE_LOW));
delay_microseconds(1);
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_ENABLE, HD44780_PINSTATE_HIGH));
delay_microseconds(1); // enable pulse must be >450ns
HD44780_RETURN_IF_ERROR(gpios->write(gpios, HD44780_PIN_ENABLE, HD44780_PINSTATE_LOW));
delay_microseconds(100); // commands need > 37us to settle
return HD44780_RESULT_OK;
}