forked from mist64/msbasic
-
Notifications
You must be signed in to change notification settings - Fork 24
/
lcd.s
146 lines (133 loc) · 2.42 KB
/
lcd.s
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
.segment "CODE"
.ifdef EATER
PORTB = $6000
DDRB = $6002
E = %01000000
RW = %00100000
RS = %00010000
lcd_wait:
pha
lda #%11110000 ; LCD data is input
sta DDRB
lcdbusy:
lda #RW
sta PORTB
lda #(RW | E)
sta PORTB
lda PORTB ; Read high nibble
pha ; and put on stack since it has the busy flag
lda #RW
sta PORTB
lda #(RW | E)
sta PORTB
lda PORTB ; Read low nibble
pla ; Get high nibble off stack
and #%00001000
bne lcdbusy
lda #RW
sta PORTB
lda #%11111111 ; LCD data is output
sta DDRB
pla
rts
LCDINIT:
lda #$ff ; Set all pins on port B to output
sta DDRB
lda #%00000011 ; Set 8-bit mode
sta PORTB
ora #E
sta PORTB
and #%00001111
sta PORTB
lda #%00000011 ; Set 8-bit mode
sta PORTB
ora #E
sta PORTB
and #%00001111
sta PORTB
lda #%00000011 ; Set 8-bit mode
sta PORTB
ora #E
sta PORTB
and #%00001111
sta PORTB
; Okay, now we're really in 8-bit mode.
; Command to get to 4-bit mode ought to work now
lda #%00000010 ; Set 4-bit mode
sta PORTB
ora #E
sta PORTB
and #%00001111
sta PORTB
lda #%00101000 ; Set 4-bit mode; 2-line display; 5x8 font
jsr lcd_instruction
lda #%00001110 ; Display on; cursor on; blink off
jsr lcd_instruction
lda #%00000110 ; Increment and shift cursor; don't shift display
jsr lcd_instruction
lda #%00000001 ; Clear display
jsr lcd_instruction
rts
LCDCMD:
jsr GETBYT
txa
lcd_instruction:
jsr lcd_wait
pha
lsr
lsr
lsr
lsr ; Send high 4 bits
sta PORTB
ora #E ; Set E bit to send instruction
sta PORTB
eor #E ; Clear E bit
sta PORTB
pla
and #%00001111 ; Send low 4 bits
sta PORTB
ora #E ; Set E bit to send instruction
sta PORTB
eor #E ; Clear E bit
sta PORTB
rts
LCDPRINT:
jsr FRMEVL
bit VALTYP
bmi lcd_print
jsr FOUT
jsr STRLIT
lcd_print:
jsr FREFAC
tax
ldy #0
lcd_print_loop:
lda (INDEX),y
jsr lcd_print_char
iny
dex
bne lcd_print_loop
rts
lcd_print_char:
jsr lcd_wait
pha
lsr
lsr
lsr
lsr ; Send high 4 bits
ora #RS ; Set RS
sta PORTB
ora #E ; Set E bit to send instruction
sta PORTB
eor #E ; Clear E bit
sta PORTB
pla
and #%00001111 ; Send low 4 bits
ora #RS ; Set RS
sta PORTB
ora #E ; Set E bit to send instruction
sta PORTB
eor #E ; Clear E bit
sta PORTB
rts
.endif