-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD_4bit.inc
204 lines (180 loc) · 4.13 KB
/
LCD_4bit.inc
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
cseg
; When using a 22.1184MHz crystal in fast mode
; one cycle takes 1.0/22.1184MHz = 45.21123 ns
;---------------------------------;
; Wait 40 microseconds ;
;---------------------------------;
Wait40uSec:
push AR0
mov R0, #177
L0:
nop
nop
djnz R0, L0 ; 1+1+3 cycles->5*45.21123ns*177=40us
pop AR0
ret
;---------------------------------;
; Wait 'R2' milliseconds ;
;---------------------------------;
Wait_Milli_Seconds mac
push AR2
mov R2, %0
lcall ?Wait_Milli_Seconds
pop AR2
endmac
?Wait_Milli_Seconds:
push AR0
push AR1
L3: mov R1, #45
L2: mov R0, #166
L1: djnz R0, L1 ; 3 cycles->3*45.21123ns*166=22.51519us
djnz R1, L2 ; 22.51519us*45=1.013ms
djnz R2, L3 ; number of millisecons to wait passed in R2
pop AR1
pop AR0
ret
;---------------------------------;
; Toggles the 'E' pin in the LCD ;
;---------------------------------;
LCD_pulse:
setb LCD_E
lcall Wait40uSec
clr LCD_E
ret
;---------------------------------;
; Writes acc to LCD in 4-bit mode ;
;---------------------------------;
LCD_byte:
; Write high 4 bits first
mov c, ACC.7
mov LCD_D7, c
mov c, ACC.6
mov LCD_D6, c
mov c, ACC.5
mov LCD_D5, c
mov c, ACC.4
mov LCD_D4, c
lcall LCD_pulse
lcall Wait40uSec
; Write low 4 bits next
mov c, ACC.3
mov LCD_D7, c
mov c, ACC.2
mov LCD_D6, c
mov c, ACC.1
mov LCD_D5, c
mov c, ACC.0
mov LCD_D4, c
lcall LCD_pulse
ret
;---------------------------------;
; Write data to LCD ;
;---------------------------------;
WriteData mac
mov a, %0
lcall ?WriteData
endmac
?WriteData:
setb LCD_RS
ljmp LCD_byte
;---------------------------------;
; Write command to LCD ;
;---------------------------------;
WriteCommand mac
mov a, %0
lcall ?WriteCommand
endmac
?WriteCommand:
clr LCD_RS
ljmp LCD_byte
;---------------------------------;
; Configure LCD in 4-bit mode ;
;---------------------------------;
LCD_4BIT:
clr LCD_E ; Resting state of LCD's enable pin is zero
; clr LCD_RW ; Only writing to the LCD. Tie this pin to ground
; After power on, let the LCD start up before initializing
; NOTE: the preprogrammed power-on delay of 16 ms on the AT89LP51RC2
; seems to be enough. That is why this line is commented out.
; Wait_Milli_Seconds(#40)
; First make sure the LCD is in 8-bit mode and then change to 4-bit mode
WriteCommand(#0x33)
WriteCommand(#0x33)
WriteCommand(#0x32) ; change to 4-bit mode
; Configure the LCD
WriteCommand(#0x28)
WriteCommand(#0x0c)
WriteCommand(#0x01) ; Clear screen command (takes some time)
;Wait for the clear screen command to finish.
Wait_Milli_Seconds(#2)
ret
;---------------------------------;
; Send a constant string to LCD ;
;---------------------------------;
Send_Constant_String mac
push dph
push dpl
push acc
mov dptr, %0
lcall ?Send_Constant_String
pop acc
pop dpl
pop dph
endmac
?Send_Constant_String:
clr a
movc a, @a+dptr
jz ?Send_Constant_String_Done
lcall ?WriteData
inc dptr
sjmp ?Send_Constant_String
?Send_Constant_String_Done:
ret
;---------------------------------;
; Set LCD cursor at row, column ;
;---------------------------------;
Set_Cursor mac
push acc
mov a, #%1
dec a
lcall ?Set_Cursor_%0 ; Select column and row
pop acc
endmac
?Set_Cursor_2:
orl a, #01000000B
?Set_Cursor_1:
orl a, #10000000B
ljmp ?WriteCommand ; Select column and row
;---------------------------------;
; Display a BCD number in the LCD ;
;---------------------------------;
Display_BCD mac
push ar0
mov r0, %0
lcall ?Display_BCD
pop ar0
endmac
?Display_BCD:
push acc
; Write most significant digit
mov a, r0
swap a
anl a, #0fh
orl a, #30h
lcall ?WriteData
; write least significant digit
mov a, r0
anl a, #0fh
orl a, #30h
lcall ?WriteData
pop acc
ret
;------------------------------------;
; Display a char in the LCD ;
;------------------------------------;
Display_char mac
push acc
mov a, %0
lcall ?WriteData
pop acc
endmac