-
Notifications
You must be signed in to change notification settings - Fork 1
/
hd44780.c
716 lines (620 loc) · 21.8 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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*****************************************************************************
Title : HD44780 Library
Author : SA Development
Version: 1.11
*****************************************************************************/
#include <avr/pgmspace.h>
#include "hd44780.h"
#include <avr/sfr_defs.h>
#if (USE_ADELAY_LIBRARY==1)
#include "adelay.h"
#else
#define Delay_ns(__ns) \
if((unsigned long) (F_CPU/1000000000.0 * __ns) != F_CPU/1000000000.0 * __ns)\
__builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000000.0 * __ns)+1);\
else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000000.0 * __ns))
#define Delay_us(__us) \
if((unsigned long) (F_CPU/1000000.0 * __us) != F_CPU/1000000.0 * __us)\
__builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000.0 * __us)+1);\
else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000.0 * __us))
#define Delay_ms(__ms) \
if((unsigned long) (F_CPU/1000.0 * __ms) != F_CPU/1000.0 * __ms)\
__builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000.0 * __ms)+1);\
else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000.0 * __ms))
#define Delay_s(__s) \
if((unsigned long) (F_CPU/1.0 * __s) != F_CPU/1.0 * __s)\
__builtin_avr_delay_cycles((unsigned long) ( F_CPU/1.0 * __s)+1);\
else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1.0 * __s))
#endif
#if !defined(LCD_BITS) || (LCD_BITS!=4 && LCD_BITS!=8)
#error LCD_BITS is not defined or not valid.
#endif
#if !defined(WAIT_MODE) || (WAIT_MODE!=0 && WAIT_MODE!=1)
#error WAIT_MODE is not defined or not valid.
#endif
#if !defined(RW_LINE_IMPLEMENTED) || (RW_LINE_IMPLEMENTED!=0 && RW_LINE_IMPLEMENTED!=1)
#error RW_LINE_IMPLEMENTED is not defined or not valid.
#endif
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED!=1)
#error WAIT_MODE=1 requires RW_LINE_IMPLEMENTED=1.
#endif
#if !defined(LCD_DISPLAYS) || (LCD_DISPLAYS<1) || (LCD_DISPLAYS>4)
#error LCD_DISPLAYS is not defined or not valid.
#endif
// Constants/Macros
#define PIN(x) (*(&x - 2)) // Address of Data Direction Register of Port X
#define DDR(x) (*(&x - 1)) // Address of Input Register of Port X
//PORT defines
#define lcd_rs_port_low() LCD_RS_PORT&=~_BV(LCD_RS_PIN)
#if RW_LINE_IMPLEMENTED==1
#define lcd_rw_port_low() LCD_RW_PORT&=~_BV(LCD_RW_PIN)
#endif
#define lcd_db0_port_low() LCD_DB0_PORT&=~_BV(LCD_DB0_PIN)
#define lcd_db1_port_low() LCD_DB1_PORT&=~_BV(LCD_DB1_PIN)
#define lcd_db2_port_low() LCD_DB2_PORT&=~_BV(LCD_DB2_PIN)
#define lcd_db3_port_low() LCD_DB3_PORT&=~_BV(LCD_DB3_PIN)
#define lcd_db4_port_low() LCD_DB4_PORT&=~_BV(LCD_DB4_PIN)
#define lcd_db5_port_low() LCD_DB5_PORT&=~_BV(LCD_DB5_PIN)
#define lcd_db6_port_low() LCD_DB6_PORT&=~_BV(LCD_DB6_PIN)
#define lcd_db7_port_low() LCD_DB7_PORT&=~_BV(LCD_DB7_PIN)
#define lcd_rs_port_high() LCD_RS_PORT|=_BV(LCD_RS_PIN)
#if RW_LINE_IMPLEMENTED==1
#define lcd_rw_port_high() LCD_RW_PORT|=_BV(LCD_RW_PIN)
#endif
#define lcd_db0_port_high() LCD_DB0_PORT|=_BV(LCD_DB0_PIN)
#define lcd_db1_port_high() LCD_DB1_PORT|=_BV(LCD_DB1_PIN)
#define lcd_db2_port_high() LCD_DB2_PORT|=_BV(LCD_DB2_PIN)
#define lcd_db3_port_high() LCD_DB3_PORT|=_BV(LCD_DB3_PIN)
#define lcd_db4_port_high() LCD_DB4_PORT|=_BV(LCD_DB4_PIN)
#define lcd_db5_port_high() LCD_DB5_PORT|=_BV(LCD_DB5_PIN)
#define lcd_db6_port_high() LCD_DB6_PORT|=_BV(LCD_DB6_PIN)
#define lcd_db7_port_high() LCD_DB7_PORT|=_BV(LCD_DB7_PIN)
#define lcd_rs_port_set(value) if (value) lcd_rs_port_high(); else lcd_rs_port_low();
#if RW_LINE_IMPLEMENTED==1
#define lcd_rw_port_set(value) if (value) lcd_rw_port_high(); else lcd_rw_port_low();
#endif
#define lcd_db0_port_set(value) if (value) lcd_db0_port_high(); else lcd_db0_port_low();
#define lcd_db1_port_set(value) if (value) lcd_db1_port_high(); else lcd_db1_port_low();
#define lcd_db2_port_set(value) if (value) lcd_db2_port_high(); else lcd_db2_port_low();
#define lcd_db3_port_set(value) if (value) lcd_db3_port_high(); else lcd_db3_port_low();
#define lcd_db4_port_set(value) if (value) lcd_db4_port_high(); else lcd_db4_port_low();
#define lcd_db5_port_set(value) if (value) lcd_db5_port_high(); else lcd_db5_port_low();
#define lcd_db6_port_set(value) if (value) lcd_db6_port_high(); else lcd_db6_port_low();
#define lcd_db7_port_set(value) if (value) lcd_db7_port_high(); else lcd_db7_port_low();
//PIN defines
#define lcd_db0_pin_get() (((PIN(LCD_DB0_PORT) & _BV(LCD_DB0_PIN))==0)?0:1)
#define lcd_db1_pin_get() (((PIN(LCD_DB1_PORT) & _BV(LCD_DB1_PIN))==0)?0:1)
#define lcd_db2_pin_get() (((PIN(LCD_DB2_PORT) & _BV(LCD_DB2_PIN))==0)?0:1)
#define lcd_db3_pin_get() (((PIN(LCD_DB3_PORT) & _BV(LCD_DB3_PIN))==0)?0:1)
#define lcd_db4_pin_get() (((PIN(LCD_DB4_PORT) & _BV(LCD_DB4_PIN))==0)?0:1)
#define lcd_db5_pin_get() (((PIN(LCD_DB5_PORT) & _BV(LCD_DB5_PIN))==0)?0:1)
#define lcd_db6_pin_get() (((PIN(LCD_DB6_PORT) & _BV(LCD_DB6_PIN))==0)?0:1)
#define lcd_db7_pin_get() (((PIN(LCD_DB7_PORT) & _BV(LCD_DB7_PIN))==0)?0:1)
//DDR defines
#define lcd_rs_ddr_low() DDR(LCD_RS_PORT)&=~_BV(LCD_RS_PIN)
#if RW_LINE_IMPLEMENTED==1
#define lcd_rw_ddr_low() DDR(LCD_RW_PORT)&=~_BV(LCD_RW_PIN)
#endif
#define lcd_db0_ddr_low() DDR(LCD_DB0_PORT)&=~_BV(LCD_DB0_PIN)
#define lcd_db1_ddr_low() DDR(LCD_DB1_PORT)&=~_BV(LCD_DB1_PIN)
#define lcd_db2_ddr_low() DDR(LCD_DB2_PORT)&=~_BV(LCD_DB2_PIN)
#define lcd_db3_ddr_low() DDR(LCD_DB3_PORT)&=~_BV(LCD_DB3_PIN)
#define lcd_db4_ddr_low() DDR(LCD_DB4_PORT)&=~_BV(LCD_DB4_PIN)
#define lcd_db5_ddr_low() DDR(LCD_DB5_PORT)&=~_BV(LCD_DB5_PIN)
#define lcd_db6_ddr_low() DDR(LCD_DB6_PORT)&=~_BV(LCD_DB6_PIN)
#define lcd_db7_ddr_low() DDR(LCD_DB7_PORT)&=~_BV(LCD_DB7_PIN)
#define lcd_rs_ddr_high() DDR(LCD_RS_PORT)|=_BV(LCD_RS_PIN)
#if RW_LINE_IMPLEMENTED==1
#define lcd_rw_ddr_high() DDR(LCD_RW_PORT)|=_BV(LCD_RW_PIN)
#endif
#define lcd_db0_ddr_high() DDR(LCD_DB0_PORT)|=_BV(LCD_DB0_PIN)
#define lcd_db1_ddr_high() DDR(LCD_DB1_PORT)|=_BV(LCD_DB1_PIN)
#define lcd_db2_ddr_high() DDR(LCD_DB2_PORT)|=_BV(LCD_DB2_PIN)
#define lcd_db3_ddr_high() DDR(LCD_DB3_PORT)|=_BV(LCD_DB3_PIN)
#define lcd_db4_ddr_high() DDR(LCD_DB4_PORT)|=_BV(LCD_DB4_PIN)
#define lcd_db5_ddr_high() DDR(LCD_DB5_PORT)|=_BV(LCD_DB5_PIN)
#define lcd_db6_ddr_high() DDR(LCD_DB6_PORT)|=_BV(LCD_DB6_PIN)
#define lcd_db7_ddr_high() DDR(LCD_DB7_PORT)|=_BV(LCD_DB7_PIN)
#define lcd_rs_ddr_set(value) if (value) lcd_rs_ddr_high(); else lcd_rs_ddr_low();
#if RW_LINE_IMPLEMENTED==1
#define lcd_rw_ddr_set(value) if (value) lcd_rw_ddr_high(); else lcd_rw_ddr_low();
#endif
#define lcd_db0_ddr_set(value) if (value) lcd_db0_ddr_high(); else lcd_db0_ddr_low();
#define lcd_db1_ddr_set(value) if (value) lcd_db1_ddr_high(); else lcd_db1_ddr_low();
#define lcd_db2_ddr_set(value) if (value) lcd_db2_ddr_high(); else lcd_db2_ddr_low();
#define lcd_db3_ddr_set(value) if (value) lcd_db3_ddr_high(); else lcd_db3_ddr_low();
#define lcd_db4_ddr_set(value) if (value) lcd_db4_ddr_high(); else lcd_db4_ddr_low();
#define lcd_db5_ddr_set(value) if (value) lcd_db5_ddr_high(); else lcd_db5_ddr_low();
#define lcd_db6_ddr_set(value) if (value) lcd_db6_ddr_high(); else lcd_db6_ddr_low();
#define lcd_db7_ddr_set(value) if (value) lcd_db7_ddr_high(); else lcd_db7_ddr_low();
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
static unsigned char PrevCmdInvolvedAddressCounter=0;
#endif
#if (LCD_DISPLAYS>1)
static unsigned char ActiveDisplay=1;
#endif
static inline void lcd_e_port_low()
{
#if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{
case 2 : LCD_E2_PORT&=~_BV(LCD_E2_PIN);
break;
#if (LCD_DISPLAYS>=3)
case 3 : LCD_E3_PORT&=~_BV(LCD_E3_PIN);
break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : LCD_E4_PORT&=~_BV(LCD_E4_PIN);
break;
#endif
default :
#endif
LCD_E_PORT&=~_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
}
#endif
}
static inline void lcd_e_port_high()
{
#if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{
case 2 : LCD_E2_PORT|=_BV(LCD_E2_PIN);
break;
#if (LCD_DISPLAYS>=3)
case 3 : LCD_E3_PORT|=_BV(LCD_E3_PIN);
break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : LCD_E4_PORT|=_BV(LCD_E4_PIN);
break;
#endif
default :
#endif
LCD_E_PORT|=_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
}
#endif
}
static inline void lcd_e_ddr_low()
{
#if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{
case 2 : DDR(LCD_E2_PORT)&=~_BV(LCD_E2_PIN);
break;
#if (LCD_DISPLAYS>=3)
case 3 : DDR(LCD_E3_PORT)&=~_BV(LCD_E3_PIN);
break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : DDR(LCD_E4_PORT)&=~_BV(LCD_E4_PIN);
break;
#endif
default :
#endif
DDR(LCD_E_PORT)&=~_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
}
#endif
}
static inline void lcd_e_ddr_high()
{
#if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{
case 2 : DDR(LCD_E2_PORT)|=_BV(LCD_E2_PIN);
break;
#if (LCD_DISPLAYS>=3)
case 3 : DDR(LCD_E3_PORT)|=_BV(LCD_E3_PIN);
break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : DDR(LCD_E4_PORT)|=_BV(LCD_E4_PIN);
break;
#endif
default :
#endif
DDR(LCD_E_PORT)|=_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
}
#endif
}
/*************************************************************************
loops while lcd is busy, returns address counter
*************************************************************************/
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
static uint8_t lcd_read(uint8_t rs);
static void lcd_waitbusy(void)
{
register uint8_t c;
unsigned int ul1=0;
while ( ((c=lcd_read(0)) & (1<<LCD_BUSY)) && ul1<((F_CPU/16384>=16)?F_CPU/16384:16)) // Wait Until Busy Flag is Cleared
ul1++;
}
#endif
/*************************************************************************
Low-level function to read byte from LCD controller
Input: rs 1: read data
0: read busy flag / address counter
Returns: byte read from LCD controller
*************************************************************************/
#if RW_LINE_IMPLEMENTED==1
static uint8_t lcd_read(uint8_t rs)
{
uint8_t data;
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
if (rs)
lcd_waitbusy();
if (PrevCmdInvolvedAddressCounter)
{
Delay_us(5);
PrevCmdInvolvedAddressCounter=0;
}
#endif
if (rs)
{
lcd_rs_port_high(); // RS=1: Read Data
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter=1;
#endif
}
else lcd_rs_port_low(); // RS=0: Read Busy Flag
lcd_rw_port_high(); // RW=1: Read Mode
#if LCD_BITS==4
lcd_db7_ddr_low(); // Configure Data Pins as Input
lcd_db6_ddr_low();
lcd_db5_ddr_low();
lcd_db4_ddr_low();
lcd_e_port_high(); // Read High Nibble First
Delay_ns(500);
data=lcd_db4_pin_get() << 4 | lcd_db5_pin_get() << 5 |
lcd_db6_pin_get() << 6 | lcd_db7_pin_get() << 7;
lcd_e_port_low();
Delay_ns(500);
lcd_e_port_high(); // Read Low Nibble
Delay_ns(500);
data|=lcd_db4_pin_get() << 0 | lcd_db5_pin_get() << 1 |
lcd_db6_pin_get() << 2 | lcd_db7_pin_get() << 3;
lcd_e_port_low();
lcd_db7_ddr_high(); // Configure Data Pins as Output
lcd_db6_ddr_high();
lcd_db5_ddr_high();
lcd_db4_ddr_high();
lcd_db7_port_high(); // Pins High (Inactive)
lcd_db6_port_high();
lcd_db5_port_high();
lcd_db4_port_high();
#else //using 8-Bit-Mode
lcd_db7_ddr_low(); // Configure Data Pins as Input
lcd_db6_ddr_low();
lcd_db5_ddr_low();
lcd_db4_ddr_low();
lcd_db3_ddr_low();
lcd_db2_ddr_low();
lcd_db1_ddr_low();
lcd_db0_ddr_low();
lcd_e_port_high();
Delay_ns(500);
data=lcd_db7_pin_get() << 7 | lcd_db6_pin_get() << 6 |
lcd_db5_pin_get() << 5 | lcd_db4_pin_get() << 4 |
lcd_db3_pin_get() << 3 | lcd_db2_pin_get() << 2 |
lcd_db1_pin_get() << 1 | lcd_db0_pin_get();
lcd_e_port_low();
lcd_db7_ddr_high(); // Configure Data Pins as Output
lcd_db6_ddr_high();
lcd_db5_ddr_high();
lcd_db4_ddr_high();
lcd_db3_ddr_high();
lcd_db2_ddr_high();
lcd_db1_ddr_high();
lcd_db0_ddr_high();
lcd_db7_port_high(); // Pins High (Inactive)
lcd_db6_port_high();
lcd_db5_port_high();
lcd_db4_port_high();
lcd_db3_port_high();
lcd_db2_port_high();
lcd_db1_port_high();
lcd_db0_port_high();
#endif
lcd_rw_port_low();
#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0)
if (rs)
Delay_us(40);
else Delay_us(1);
#endif
return data;
}
uint8_t lcd_getc()
{
return lcd_read(1);
}
#endif
/*************************************************************************
Low-level function to write byte to LCD controller
Input: data byte to write to LCD
rs 1: write data
0: write instruction
Returns: none
*************************************************************************/
static void lcd_write(uint8_t data,uint8_t rs)
{
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
lcd_waitbusy();
if (PrevCmdInvolvedAddressCounter)
{
Delay_us(5);
PrevCmdInvolvedAddressCounter=0;
}
#endif
if (rs)
{
lcd_rs_port_high(); // RS=1: Write Character
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter=1;
#endif
}
else
{
lcd_rs_port_low(); // RS=0: Write Command
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter=0;
#endif
}
#if LCD_BITS==4
lcd_db7_port_set(data&_BV(7)); //Output High Nibble
lcd_db6_port_set(data&_BV(6));
lcd_db5_port_set(data&_BV(5));
lcd_db4_port_set(data&_BV(4));
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
lcd_db7_port_set(data&_BV(3)); //Output High Nibble
lcd_db6_port_set(data&_BV(2));
lcd_db5_port_set(data&_BV(1));
lcd_db4_port_set(data&_BV(0));
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
lcd_db7_port_high(); // All Data Pins High (Inactive)
lcd_db6_port_high();
lcd_db5_port_high();
lcd_db4_port_high();
#else //using 8-Bit_Mode
lcd_db7_port_set(data&_BV(7)); //Output High Nibble
lcd_db6_port_set(data&_BV(6));
lcd_db5_port_set(data&_BV(5));
lcd_db4_port_set(data&_BV(4));
lcd_db3_port_set(data&_BV(3)); //Output High Nibble
lcd_db2_port_set(data&_BV(2));
lcd_db1_port_set(data&_BV(1));
lcd_db0_port_set(data&_BV(0));
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
lcd_db7_port_high(); // All Data Pins High (Inactive)
lcd_db6_port_high();
lcd_db5_port_high();
lcd_db4_port_high();
lcd_db3_port_high();
lcd_db2_port_high();
lcd_db1_port_high();
lcd_db0_port_high();
#endif
#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0)
if (!rs && data<=((1<<LCD_CLR) | (1<<LCD_HOME))) // Is command clrscr or home?
Delay_us(1640);
else Delay_us(40);
#endif
}
/*************************************************************************
Send LCD controller instruction command
Input: instruction to send to LCD controller, see HD44780 data sheet
Returns: none
*************************************************************************/
void lcd_command(uint8_t cmd)
{
lcd_write(cmd,0);
}
/*************************************************************************
Set cursor to specified position
Input: pos position
Returns: none
*************************************************************************/
void lcd_goto(uint8_t pos)
{
lcd_command((1<<LCD_DDRAM)+pos);
}
/*************************************************************************
Clear screen
Input: none
Returns: none
*************************************************************************/
void lcd_clrscr()
{
lcd_command(1<<LCD_CLR);
}
/*************************************************************************
Return home
Input: none
Returns: none
*************************************************************************/
void lcd_home()
{
lcd_command(1<<LCD_HOME);
}
/*************************************************************************
Display character
Input: character to be displayed
Returns: none
*************************************************************************/
void lcd_putc(char c)
{
lcd_write(c,1);
}
/*************************************************************************
Display string
Input: string to be displayed
Returns: none
*************************************************************************/
void lcd_puts(const char *s)
{
register char c;
while ((c=*s++))
lcd_putc(c);
}
/*************************************************************************
Display string from flash
Input: string to be displayed
Returns: none
*************************************************************************/
void lcd_puts_P(const char *progmem_s)
{
register char c;
while ((c=pgm_read_byte(progmem_s++)))
lcd_putc(c);
}
/*************************************************************************
Initialize display
Input: none
Returns: none
*************************************************************************/
void lcd_init()
{
//Set All Pins as Output
lcd_e_ddr_high();
lcd_rs_ddr_high();
#if RW_LINE_IMPLEMENTED==1
lcd_rw_ddr_high();
#endif
lcd_db7_ddr_high();
lcd_db6_ddr_high();
lcd_db5_ddr_high();
lcd_db4_ddr_high();
#if LCD_BITS==8
lcd_db3_ddr_high();
lcd_db2_ddr_high();
lcd_db1_ddr_high();
lcd_db0_ddr_high();
#endif
//Set All Control Lines Low
lcd_e_port_low();
lcd_rs_port_low();
#if RW_LINE_IMPLEMENTED==1
lcd_rw_port_low();
#endif
//Set All Data Lines High
lcd_db7_port_high();
lcd_db6_port_high();
lcd_db5_port_high();
lcd_db4_port_high();
#if LCD_BITS==8
lcd_db3_port_high();
lcd_db2_port_high();
lcd_db1_port_high();
lcd_db0_port_high();
#endif
//Startup Delay
Delay_ms(DELAY_RESET);
//Initialize Display
lcd_db7_port_low();
lcd_db6_port_low();
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
Delay_us(4100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
Delay_us(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
Delay_us(40);
//Init differs between 4-bit and 8-bit from here
#if (LCD_BITS==4)
lcd_db4_port_low();
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
Delay_us(40);
lcd_db4_port_low();
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
Delay_ns(500);
#if (LCD_DISPLAYS==1)
if (LCD_DISPLAY_LINES>1)
lcd_db7_port_high();
#else
unsigned char c;
switch (ActiveDisplay)
{
case 1 : c=LCD_DISPLAY_LINES; break;
case 2 : c=LCD_DISPLAY2_LINES; break;
#if (LCD_DISPLAYS>=3)
case 3 : c=LCD_DISPLAY3_LINES; break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : c=LCD_DISPLAY4_LINES; break;
#endif
}
if (c>1)
lcd_db7_port_high();
#endif
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
Delay_us(40);
#else
#if (LCD_DISPLAYS==1)
if (LCD_DISPLAY_LINES<2)
lcd_db3_port_low();
#else
unsigned char c;
switch (ActiveDisplay)
{
case 1 : c=LCD_DISPLAY_LINES; break;
case 2 : c=LCD_DISPLAY2_LINES; break;
#if (LCD_DISPLAYS>=3)
case 3 : c=LCD_DISPLAY3_LINES; break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : c=LCD_DISPLAY4_LINES; break;
#endif
}
if (c<2)
lcd_db3_port_low();
#endif
lcd_db2_port_low();
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
lcd_e_port_low();
Delay_us(40);
#endif
//Display Off
lcd_command(_BV(LCD_DISPLAYMODE));
//Display Clear
lcd_clrscr();
//Entry Mode Set
lcd_command(_BV(LCD_ENTRY_MODE) | _BV(LCD_ENTRY_INC));
//Display On
lcd_command(_BV(LCD_DISPLAYMODE) | _BV(LCD_DISPLAYMODE_ON));
}
#if (LCD_DISPLAYS>1)
void lcd_use_display(int ADisplay)
{
if (ADisplay>=1 && ADisplay<=LCD_DISPLAYS)
ActiveDisplay=ADisplay;
}
#endif