-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathST7920.c
1401 lines (1066 loc) · 37.5 KB
/
ST7920.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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ************************************************************************
*
* driver functions for ST7920 compatible graphic displays
* - 128 x 64 pixels
* - other resolutions are not supported because of different address
* to pixel mappings
* (64 x 32 pixels native, with ST7921s up to 256 x 64)
* - interfaces
* - 3 line SPI
* - 4 bit parallel interface
* - 8 bit parallel interface (not supported)
*
* (c) 2017-2022 by Markus Reschke
*
* ************************************************************************ */
/*
* hints:
* - pin assignment for SPI
* /XRESET Vcc or LCD_RESET (optional)
* CS (RS) Gnd or LCD_CS (optional)
* SCLK (E) LCD_SCLK
* SID (RW) LCD_SID
* PSB Gnd (enable serial mode)
* For hardware SPI LCD_SCLK and LCD_SID have to be the MCU's SCK and
* MOSI pins.
* - max. SPI clock: 2.5MHz at 4.5V / 1.6MHz at 2.7V
* - write only when using SPI
* - doesn't allow other SPI chips on same bus (says the datasheet)
* - pin assignment for 4 bit parallel
* /XRESET Vcc or LCD_RESET (optional)
* E LCD_EN
* RS LCD_RS
* RW Gnd or LCD_RW (optional)
* D4 LCD_DB4 (default: LCD_PORT pin #0)
* D5 LCD_DB5 (default: LCD_PORT pin #1)
* D6 LCD_DB6 (default: LCD_PORT pin #2)
* D7 LCD_DB7 (default: LCD_PORT pin #3)
* PSB pull-up resistor to Vcc (enable parallel mode)
*/
/* local includes */
#include "config.h" /* global configuration */
#ifdef LCD_ST7920
/*
* local constants
*/
/* source management */
#define LCD_DRIVER_C
/*
* include header files
*/
/* local includes */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
#include "ST7920.h" /* ST7920 specifics */
/*
* fonts and symbols
* - Because of the horizontal addressing we're stuck with 8 pixels in x
* direction for the font. And we don't want to use a pixel screen buffer.
*/
#ifndef LCD_ROT180
/* horizontally aligned */
#include "font_8x8_h.h"
#include "font_8x8_iso8859-2_h.h"
#include "symbols_24x24_h.h"
#include "symbols_24x24_alt1_h.h"
#include "symbols_24x24_alt2_h.h"
#include "symbols_24x24_old_h.h"
#endif
#ifdef LCD_ROT180
/* horizontally aligned, horizontal bit order flipped */
#include "font_8x8_hf.h"
#include "font_8x8_iso8859-2_hf.h"
#include "symbols_24x24_hf.h"
#include "symbols_24x24_alt1_hf.h"
#include "symbols_24x24_alt2_hf.h"
#include "symbols_24x24_old_hf.h"
#include "symbols_32x32_hf.h"
#include "symbols_32x32_alt1_hf.h"
#include "symbols_32x32_alt2_hf.h"
#include "symbols_32x32_old_hf.h"
#endif
/* sanity check */
#ifndef FONT_SET
#error <<< No font selected! >>>
#endif
#ifdef SW_SYMBOLS
#ifndef SYMBOL_SET
#error <<< No symbols selected! >>>
#endif
#endif
/*
* derived constants
*/
/* number of 16 bit steps for x direction */
#define LCD_STEPS_X (LCD_DOTS_X / 16)
/* number of lines and characters per line */
#define LCD_CHAR_X (LCD_DOTS_X / FONT_SIZE_X)
#define LCD_CHAR_Y (LCD_DOTS_Y / FONT_SIZE_Y)
/* component symbols */
#ifdef SW_SYMBOLS
/* number of 16 bit steps for x direction */
#define LCD_SYMBOL_STEPS_X (SYMBOL_SIZE_X / 16)
/* size in relation to a character */
#define LCD_SYMBOL_CHAR_X ((SYMBOL_SIZE_X + FONT_SIZE_X - 1) / FONT_SIZE_X)
#define LCD_SYMBOL_CHAR_Y ((SYMBOL_SIZE_Y + FONT_SIZE_Y - 1) / FONT_SIZE_Y)
/* check x size: we need at least 3 chars */
#if LCD_SYMBOL_CHAR_X < 3
#error <<< Symbols too small! >>>
#endif
#endif
/*
* local variables
*/
/* character matrix (copy of screen, horizontally aligned) */
unsigned char Matrix[LCD_CHAR_X * LCD_CHAR_Y]; /* char matrix */
/* position management */
uint8_t X_Start; /* start position X (column in 16 bit steps) */
uint8_t Y_Start; /* start position Y (row) */
/*
* function declarations (prototypes)
*/
void LCD_Char(unsigned char Char);
/* ************************************************************************
* low level functions for 4 bit parallel interface
* ************************************************************************ */
#ifdef LCD_PAR_4
/*
* set up interface bus
* - should be called at firmware startup
*/
void LCD_BusSetup(void)
{
uint8_t Bits; /* register bits */
/*
* set control signals
*/
/* set directions */
Bits = LCD_DDR; /* get current directions */
/* required pins */
Bits |= (1 << LCD_RS) | (1 << LCD_EN) |
(1 << LCD_DB4) | (1 << LCD_DB5) | (1 << LCD_DB6) | (1 << LCD_DB7);
/* optional output pins */
#ifdef LCD_RESET
Bits |= (1 << LCD_RESET);
#endif
#ifdef LCD_RW
Bits |= (1 << LCD_RW);
#endif
LCD_DDR = Bits; /* set new directions */
/* set default levels */
/* LCD_EN should be low by default */
#ifdef LCD_RESET
/* disable reset */
LCD_PORT |= (1 << LCD_RESET); /* set /RESET high */
#endif
/* LCD_RW should be low by default (write mode) */
}
/*
* send a nibble to the LCD (4 bit mode)
*
* requires:
* - nibble value to send
*/
void LCD_SendNibble(uint8_t Nibble)
{
uint8_t Data;
Data = LCD_PORT; /* get current bits */
/* clear all 4 data lines */
Data &= ~((1 << LCD_DB4) | (1 << LCD_DB5) | (1 << LCD_DB6) | (1 << LCD_DB7));
#ifdef LCD_DB_STD
/* standard pins: simply take nibble */
Data |= Nibble;
#else
/* non-standard pins: set bits individually */
if (Nibble & 0b00000001) Data |= (1 << LCD_DB4);
if (Nibble & 0b00000010) Data |= (1 << LCD_DB5);
if (Nibble & 0b00000100) Data |= (1 << LCD_DB6);
if (Nibble & 0b00001000) Data |= (1 << LCD_DB7);
#endif
LCD_PORT = Data; /* set nibble */
/* enable pulse */
LCD_PORT |= (1 << LCD_EN); /* set EN high */
wait1us(); /* wait >= 140ns */
LCD_PORT &= ~(1 << LCD_EN); /* set EN low */
/* data hold time >= 20ns */
/* Enable cycle time >= 1200ns */
wait1us(); /* wait 1µs */
}
/*
* send a byte (data or command) to the LCD
* - send byte as two nibbles (MSB first, LSB last)
*
* requires:
* - byte value to send
*/
void LCD_Send(uint8_t Byte)
{
uint8_t Nibble;
/* send upper nibble (bits 4-7) */
Nibble = (Byte >> 4) & 0x0F; /* get upper nibble */
LCD_SendNibble(Nibble);
/* send lower nibble (bits 0-3) */
Nibble = Byte & 0x0F; /* get lower nibble */
LCD_SendNibble(Nibble);
}
/*
* send a command to the LCD
*
* requires:
* - byte value to send
*/
void LCD_Cmd(uint8_t Byte)
{
/* indicate command mode */
LCD_PORT &= ~(1 << LCD_RS); /* set RS low */
#ifdef LCD_RW
/* write mode */
LCD_PORT &= ~(1 << LCD_RW); /* set RW low */
#endif
LCD_Send(Byte); /* send command */
}
/*
* send data to the LCD
*
* requires:
* - byte value to send
*/
void LCD_Data(uint8_t Byte)
{
/* indicate data mode */
LCD_PORT |= (1 << LCD_RS); /* set RS high */
#ifdef LCD_RW
/* write mode */
LCD_PORT &= ~(1 << LCD_RW); /* set RW low */
#endif
LCD_Send(Byte); /* send data */
wait100us(); /* 72µs processing delay */
}
#endif
/* ************************************************************************
* low level functions for 3 line SPI interface
* ************************************************************************ */
#ifdef LCD_SPI
/*
* set up interface bus
* - should be called at firmware startup
*/
void LCD_BusSetup(void)
{
uint8_t Bits; /* register bits */
/*
* set control signals
*/
/* set directions */
Bits = LCD_DDR; /* get current directions */
/* optional output pins */
#ifdef LCD_RESET
Bits |= (1 << LCD_RESET);
#endif
#ifdef LCD_CS
Bits |= (1 << LCD_CS);
#endif
LCD_DDR = Bits; /* set new directions */
/* set default levels */
/* LCD_CS should be low by default */
#ifdef LCD_RESET
/* disable reset */
LCD_PORT |= (1 << LCD_RESET); /* set /RESET high */
#endif
/*
* init SPI bus
* - SPI bus is set up already in main()
*/
#ifdef SPI_HARDWARE
/*
* set SPI clock rate (max. 2.5MHz)
*/
/* 1MHz -> f_osc/2 (SPR1 = 0, SPR0 = 0, SPI2X = 1) */
#if CPU_FREQ / 1000000 == 1
SPI.ClockRate = SPI_CLOCK_2X;
#endif
/* 8MHz -> f_osc/4 (SPR1 = 0, SPR0 = 0, SPI2X = 0) */
#if CPU_FREQ / 1000000 == 8
SPI.ClockRate = 0
#endif
/* 16MHz -> f_osc/8 (SPR1 = 0, SPR0 = 1, SPI2X = 1) */
#if CPU_FREQ / 1000000 == 16
SPI.ClockRate = SPI_CLOCK_R0 | SPI_CLOCK_2X;
#endif
/* 20MHz -> f_osc/8 (SPR1 = 0, SPR0 = 1, SPI2X = 1) */
#if CPU_FREQ / 1000000 == 20
SPI.ClockRate = SPI_CLOCK_R0 | SPI_CLOCK_2X;
#endif
SPI_Clock(); /* update SPI clock */
#endif
}
/*
* send a byte (command or data) to the LCD
* - hint: ST7920 doesn't take multiple bytes after the start byte.
*
* requires:
* - byte value to send
*/
void LCD_Send(uint8_t Byte)
{
uint8_t Nibble;
/* upper nibble plus 4 zeros */
Nibble = Byte & 0xF0;
SPI_Write_Byte(Nibble); /* write byte */
/* lower nibble plus 4 zeros */
Nibble = Byte << 4;
SPI_Write_Byte(Nibble); /* write byte */
}
/*
* send a command to the LCD
*
* requires:
* - byte value to send
*/
void LCD_Cmd(uint8_t Byte)
{
uint8_t Start;
#ifdef LCD_CS
/* select chip */
LCD_PORT |= (1 << LCD_CS); /* set CS high */
#endif
/* start byte */
Start = START_BYTE | FLAG_WRITE | FLAG_CMD;
SPI_Write_Byte(Start); /* write start byte */
LCD_Send(Byte); /* write command */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT &= ~(1 << LCD_CS); /* set CS low */
#endif
}
/*
* send data to the LCD
*
* requires:
* - byte value to send
*/
void LCD_Data(uint8_t Byte)
{
uint8_t Start;
#ifdef LCD_CS
/* select chip */
LCD_PORT |= (1 << LCD_CS); /* set CS high */
#endif
/* start byte */
Start = START_BYTE | FLAG_WRITE | FLAG_DATA;
SPI_Write_Byte(Start); /* write start byte */
LCD_Send(Byte); /* write data */
#ifdef LCD_CS
/* deselect chip */
LCD_PORT &= ~(1 << LCD_CS); /* set CS low */
#endif
/* todo: do we need a 72µs delay for LCD processing? */
wait50us(); /* 72µs processing delay */
}
#endif
/* ************************************************************************
* high level functions
* ************************************************************************ */
/*
* set LCD dot position
* - horizontal position is based on 16 bit steps
* - top left: 0/0
*
* requires:
* - x: horizontal position (0-)
* - y: vertical position (0-)
*/
void LCD_DotPos(uint8_t x, uint8_t y)
{
uint8_t Cmd_X; /* command for setting x */
uint8_t Cmd_Y; /* command for setting y */
/* in case of a low MCU clock */
wdt_reset(); /* reset watchdog */
/* take care about address to pixel mapping */
if (y < 32) /* top LCD half (0-31) */
{
/* left address space is mapped to top LCD half */
Cmd_Y = CMD_SET_GDRAM_ADDR | y; /* vertical address */
Cmd_X = CMD_SET_GDRAM_ADDR | x; /* horizontal address */
}
else /* bottom LCD half (32-63) */
{
/* right address space is mapped to bottom LCD half */
Cmd_Y = CMD_SET_GDRAM_ADDR | (y - 32); /* vertical address */
Cmd_X = CMD_SET_GDRAM_ADDR | 8 | x; /* horizontal address */
}
LCD_Cmd(Cmd_Y); /* set vertical address */
wait100us(); /* 72µs processing delay */
LCD_Cmd(Cmd_X); /* set horizontal address */
wait100us(); /* 72µs processing delay */
/*
* The ST7920 seems to have a bug when setting the GDRAM address without
* sending any data afterwards. A second CMD_SET_GDRAM_ADDR causes a
* the output of 16 bits in a row. So we should call LCD_DotPos() only if
* we also send data.
*/
}
#ifndef LCD_ROT180
/*
* set LCD character position
* - top left: 1/1
*
* requires:
* - x: horizontal position (1-)
* - y: vertical position (1-)
*/
void LCD_CharPos(uint8_t x, uint8_t y)
{
/* update UI */
UI.CharPos_X = x;
UI.CharPos_Y = y;
/*
* calculate dot position
* - top left of character
*/
/* horizontal position (column in 16 bit steps) */
x--; /* columns start at 0 */
x *= FONT_SIZE_X; /* offset for character */
x /= 16; /* convert into 16 bit steps */
X_Start = x; /* update start position */
/* vertical position (row) */
y--; /* rows start at 0 */
y *= FONT_SIZE_Y; /* offset for character */
Y_Start = y; /* update start position */
/*
* we don't call LCD_DotPos() here
* - see LCD_DotPos() for explanation
*/
}
#endif
#ifdef LCD_ROT180
/*
* set LCD character position
* - version for display rotated by 180°
* - top left: 1/1
*
* requires:
* - x: horizontal position (1-)
* - y: vertical position (1-)
*/
void LCD_CharPos(uint8_t x, uint8_t y)
{
/* update UI */
UI.CharPos_X = x;
UI.CharPos_Y = y;
/*
* calculate dot position
* - top right of character (180° view)
* or bottom left for 0° view
*/
/* horizontal position (column in 16 bit steps), flipped */
x--; /* columns start at 0 */
x *= FONT_SIZE_X; /* offset for character */
x /= 16; /* convert into 16 bit steps */
X_Start = (LCD_STEPS_X - 1) - x; /* update start position */
/* vertical position (row), flipped */
y--; /* rows start at 0 */
y *= FONT_SIZE_Y; /* offset for character */
Y_Start = (LCD_DOTS_Y - 1) - y; /* update start position */
/*
* we don't call LCD_DotPos() here
* - see LCD_DotPos() for explaination
*/
}
#endif
#ifndef LCD_ROT180
/*
* clear one single character line
*
* requires:
* - Line: line number (1-)
* special case line 0: clear remaining space in current line
*/
void LCD_ClearLine(uint8_t Line)
{
uint8_t n = 1; /* counter */
uint8_t Temp; /* temp. value */
uint8_t MatrixFlag = 1; /* control flag */
unsigned char *Buffer; /* char matrix */
if (Line == 0) /* special case: rest of current line */
{
Line = UI.CharPos_Y; /* get current line */
n = UI.CharPos_X; /* get current character position */
}
/* check for row overflow */
Temp = Line - 1; /* rows start at 0 */
Temp *= FONT_SIZE_Y; /* offset for line */
if (Temp > (LCD_DOTS_Y - 1)) return; /* overflow */
LCD_CharPos(n, Line); /* set char position */
if ((n % 2) == 0) /* left neighbor */
{
LCD_Char(' '); /* display space and keep neighbor */
n++; /* one char done */
}
/* set start position in char matrix */
Buffer = (unsigned char *)&Matrix; /* start of matrix */
Temp = (Line - 1) * LCD_CHAR_X; /* offset for line */
Temp += (n - 1); /* offset for column */
Buffer += Temp; /* add offset for position */
/* init row range */
if (Line <= LCD_CHAR_Y) /* within character lines */
{
/* clear character line */
n = FONT_SIZE_Y; /* number of rows to clear */
}
else /* remaining rows */
{
/* clear up to last row */
n = LCD_DOTS_Y - Y_Start; /* number of rows to clear */
MatrixFlag = 0; /* don't clear chars in matrix (overflow) */
}
/* clear line */
Line = Y_Start; /* starting row */
while (n > 0) /* loop for rows */
{
LCD_DotPos(X_Start, Line); /* set new dot position */
Temp = LCD_STEPS_X - X_Start; /* number of columns */
while (Temp > 0) /* loop for columns */
{
LCD_Data(0); /* clear 8 pixels */
LCD_Data(0); /* clear another 8 pixels */
Temp--; /* next 16 bit step */
if (MatrixFlag) /* clear char in char matrix */
{
*Buffer = ' '; /* set space */
Buffer++; /* next char */
*Buffer = ' '; /* set space */
Buffer++; /* next char */
}
}
MatrixFlag = 0; /* reset flag */
Line++; /* next row */
n--; /* next row */
}
}
#endif
#ifdef LCD_ROT180
/*
* clear one single character line
* - version for display rotated by 180°
*
* requires:
* - Line: line number (1-)
* special case line 0: clear remaining space in current line
*/
void LCD_ClearLine(uint8_t Line)
{
uint8_t n = 1; /* counter */
uint8_t Temp; /* temp. value */
uint8_t MatrixFlag = 1; /* flag for matrix update */
unsigned char *Buffer; /* char matrix */
if (Line == 0) /* special case: rest of current line */
{
Line = UI.CharPos_Y; /* get current line */
n = UI.CharPos_X; /* get current character position */
}
/* check for row overflow */
Temp = Line - 1; /* rows start at 0 */
Temp *= FONT_SIZE_Y; /* offset for line */
if (Temp > (LCD_DOTS_Y - 1)) return; /* overflow */
/*
* Because of the ST7920's fixed X address logic, we clear each line
* starting at the end of the textline. Also we clear lines starting
* with the bottom one to follow the vertical text direction.
*/
LCD_CharPos(n, Line); /* set char position */
if ((n % 2) == 0) /* left neighbor */
{
LCD_Char(' '); /* display space and keep neighbor */
}
/* set start position in char matrix (end of line) */
Buffer = (unsigned char *)&Matrix; /* start of matrix */
Temp = Line * LCD_CHAR_X; /* offset for next line */
Temp -= 1; /* offset for end of current line */
Buffer += Temp; /* add offset for position */
/* init row range (starting at bottom) */
if (Line <= LCD_CHAR_Y) /* within character lines */
{
/* clear character line */
n = FONT_SIZE_Y; /* number of rows to clear */
}
else /* remaining rows */
{
/* clear up to last row */
n = Y_Start + 1; /* number of rows to clear */
MatrixFlag = 0; /* don't clear chars in matrix (overflow) */
}
/* clear line */
Line = Y_Start; /* starting row */
while (n > 0) /* loop for rows */
{
LCD_DotPos(0, Line); /* set new dot position */
Temp = X_Start + 1; /* number of column steps */
while (Temp > 0) /* loop for columns */
{
LCD_Data(0); /* clear 8 pixels */
LCD_Data(0); /* clear another 8 pixels */
Temp--; /* next 16 bit step */
if (MatrixFlag) /* clear char in char matrix */
{
*Buffer = ' '; /* set space */
Buffer--; /* next char */
*Buffer = ' '; /* set space */
Buffer--; /* next char */
}
}
MatrixFlag = 0; /* reset flag */
Line--; /* next row */
n--; /* next row */
}
}
#endif
/*
* clear the display
*/
void LCD_Clear(void)
{
uint8_t n = 1; /* counter */
/* we have to clear all dots manually :( */
/* loop for all lines plus possible remaining row */
while (n <= (LCD_CHAR_Y + 1))
{
LCD_ClearLine(n); /* clear line */
n++; /* next line */
}
LCD_CharPos(1, 1); /* reset character position */
}
/*
* initialize LCD
*/
void LCD_Init(void)
{
MilliSleep(40); /* wait 40ms for Vcc to become stable */
#ifdef LCD_RESET
/* reset display */
LCD_PORT &= ~(1 << LCD_RESET); /* set /RES low */
wait10us(); /* wait 10µs */
LCD_PORT |= (1 << LCD_RESET); /* set /RES high */
MilliSleep(1); /* wait 1ms */
#endif
/*
* init LCD module
*/
#ifdef LCD_PAR_4
/* setup for 4 bit parallel interface */
LCD_Cmd(CMD_FUNCTION_SET | FLAG_INTERFACE_4BIT);
wait100us(); /* 72µs processing delay */
LCD_Cmd(CMD_FUNCTION_SET | FLAG_INTERFACE_4BIT);
wait100us(); /* 72µs processing delay */
#endif
#ifdef LCD_SPI
/* setup for SPI and 8 bit parallel interface */
LCD_Cmd(CMD_FUNCTION_SET | FLAG_INTERFACE_8BIT);
wait100us(); /* 72µs processing delay */
LCD_Cmd(CMD_FUNCTION_SET | FLAG_INTERFACE_8BIT);
wait100us(); /* 72µs processing delay */
#endif
/* switch display on */
LCD_Cmd(CMD_DISPLAY | FLAG_DISPLAY_ON);
wait100us(); /* 72µs processing delay */
/* clear display */
LCD_Cmd(CMD_CLEAR);
MilliSleep(2); /* 1.6ms processing delay */
/* entry mode: left to right, no scrolling */
LCD_Cmd(CMD_ENTRY_MODE | FLAG_INCREASE);
wait100us(); /* 72µs processing delay */
/*
* enter graphics mode
*/
#ifdef LCD_PAR_4
/* enable extended instruction set */
LCD_Cmd(CMD_FUNCTION_SET | FLAG_INTERFACE_4BIT | FLAG_CMD_EXT);
wait100us(); /* 72µs processing delay */
/* enable graphics mode */
LCD_Cmd(CMD_EXT_FUNC_SET | FLAG_INTERFACE_4BIT | FLAG_CMD_EXT | FLAG_GFX_ON);
wait100us(); /* 72µs processing delay */
#endif
#ifdef LCD_SPI
/* enable extended instruction set */
LCD_Cmd(CMD_FUNCTION_SET | FLAG_INTERFACE_8BIT | FLAG_CMD_EXT);
wait100us(); /* 72µs processing delay */
/* enable graphics mode */
LCD_Cmd(CMD_EXT_FUNC_SET | FLAG_INTERFACE_8BIT | FLAG_CMD_EXT | FLAG_GFX_ON);
wait100us(); /* 72µs processing delay */
#endif
/* update maximums */
UI.CharMax_X = LCD_CHAR_X; /* characters per line */
UI.CharMax_Y = LCD_CHAR_Y; /* lines */
#ifdef SW_SYMBOLS
UI.SymbolSize_X = LCD_SYMBOL_CHAR_X; /* x size in chars */
UI.SymbolSize_Y = LCD_SYMBOL_CHAR_Y; /* y size in chars */
#endif
LCD_Clear(); /* clear screen */
}
#ifndef LCD_ROT180
/*
* display a single character
*
* requires:
* - Char: character to display
*/
void LCD_Char(unsigned char Char)
{
uint8_t *Table1; /* pointer to table */
uint8_t *Table2; /* pointer to table */
unsigned char *Buffer;
uint8_t Index; /* font index */
uint16_t Offset; /* address offset */
uint8_t Neighbor; /* neighboring character */
uint8_t StepFlag; /* offset control flag */
uint8_t y; /* bitmap y byte counter */
uint8_t Row; /* screen row */
/* prevent x overflow */
if (UI.CharPos_X > LCD_CHAR_X) return;
/*
* get bitmap of character
*/
/* get font index number from lookup table */
Table1 = (uint8_t *)&FontTable; /* start address */
Table1 += Char; /* add offset for character */
Index = pgm_read_byte(Table1); /* get index number */
if (Index == 0xff) return; /* no character bitmap available */
/* calculate start address of character bitmap */
Table1 = (uint8_t *)&FontData; /* start address of font data */
Offset = FONT_BYTES_N * Index; /* offset for character */
Table1 += Offset; /* address of character data */
/*
* get bitmap of neighbor
*/
/* get neighboring character from char matrix */
StepFlag = 0; /* right by default */
Index = (UI.CharPos_Y - 1) * LCD_CHAR_X; /* offset for line */
Index += UI.CharPos_X; /* offset for column (right neighbor) */
if ((UI.CharPos_X % 2) == 0) /* got left neighbor */
{
StepFlag = 1; /* set flag */
Index -= 2; /* adjust buffer offset */
}
Buffer = (unsigned char *)&Matrix; /* start of matrix */
Buffer += Index; /* add offset for position */
Neighbor = *Buffer; /* get neighbor from matrix */
/* get font index number from lookup table */
Table2 = (uint8_t *)&FontTable; /* start address */
Table2 += Neighbor; /* add offset for character */
Index = pgm_read_byte(Table2); /* get index number */
if (Index == 0xff) return; /* no character bitmap available */
/* calculate start address of character bitmap */
Table2 = (uint8_t *)&FontData; /* start address of font data */
Offset = FONT_BYTES_N * Index; /* offset for character */
Table2 += Offset; /* address of character data */
/*
* display bitmaps of new and neighboring char
*/
/* read character bitmap and send it to display */
y = 1;
Row = Y_Start; /* get start row for screen */