-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_macros.inc
334 lines (314 loc) · 9.99 KB
/
extra_macros.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
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
$NOLIST
;---------------------------------;
; Initial configuration of ports. ;
; After reset the default for the ;
; pins is 'Open Drain'. This ;
; routine changes them pins to ;
; Quasi-bidirectional like in the ;
; original 8051. ;
; Notice that P1.2 and P1.3 are ;
; always 'Open Drain'. If those ;
; pins are to be used as output ;
; they need a pull-up resistor. ;
;---------------------------------;
Ports_Init:
; Configure all the ports in bidirectional mode:
mov P0M1, #00H
mov P0M2, #00H
mov P1M1, #00H
mov P1M2, #00H ; WARNING: P1.2 and P1.3 need 1 kohm pull-up resistors if used as outputs!
mov P2M1, #00H
mov P2M2, #00H
mov P3M1, #00H
mov P3M2, #00H
ret
;---------------------------------;
; Sends a byte via serial port ;
;---------------------------------;
putchar:
jbc TI,putchar_L1
sjmp putchar
putchar_L1:
mov SBUF,a
ret
;---------------------------------;
; Receive a byte from serial port ;
;---------------------------------;
getchar:
jbc RI,getchar_L1
sjmp getchar
getchar_L1:
mov a,SBUF
ret
;---------------------------------;
; Initialize the serial port ;
;---------------------------------;
InitSerialPort:
mov BRGCON,#0x00
mov BRGR1,#high(BRVAL)
mov BRGR0,#low(BRVAL)
mov BRGCON,#0x03 ; Turn-on the baud rate generator
mov SCON,#0x52 ; Serial port in mode 1, ren, txrdy, rxempty
; Make sure that TXD(P1.0) and RXD(P1.1) are configured as bidrectional I/O
anl P1M1,#11111100B
anl P1M2,#11111100B
ret
;---------------------------------;
; Initialize ADC1/DAC1 as DAC1. ;
; Warning, the ADC1/DAC1 can work ;
; only as ADC or DAC, not both. ;
; The P89LPC9351 has two ADC/DAC ;
; interfaces. One can be used as ;
; ADC and the other can be used ;
; as DAC. Also configures the ;
; pin associated with the DAC, in ;
; this case P0.4 as 'Open Drain'. ;
;---------------------------------;
InitDAC1:
; Configure pin P0.4 (DAC1 output pin) as open drain
orl P0M1, #00010000B
orl P0M2, #00010000B
mov ADMODB, #00101000B ; Select main clock/2 for ADC/DAC. Also enable DAC1 output (Table 25 of reference manual)
mov ADCON1, #00000100B ; Enable the converter
mov AD1DAT3, #0x80 ; Start value is 3.3V/2 (zero reference for AC WAV file)
ret
;---------------------------------;
; Initialize ADC0/DAC0 as ADC0. ;
;---------------------------------;
InitADC0:
; ADC0_0 is connected to P1.7
; ADC0_1 is connected to P0.0
; ADC0_2 is connected to P2.1
; ADC0_3 is connected to P2.0
; Configure pins P1.7, P0.0, P2.1, and P2.0 as inputs
orl P0M1, #00000001b
anl P0M2, #11111110b
orl P1M1, #10000000b
anl P1M2, #01111111b
orl P2M1, #00000011b
anl P2M2, #11111100b
; Setup ADC0
setb BURST0 ; Autoscan continuos conversion mode
mov ADMODB,#0x20 ;ADC0 clock is 7.3728MHz/2
mov ADINS,#0x0c ; Select the four channels of ADC0 for conversion
mov ADCON0,#0x05 ; Enable the converter and start immediately
; Wait for first conversion to complete
InitADC0_L1:
mov a,ADCON0
jnb acc.3,InitADC0_L1
ret
;---------------------------------;
; Change the internal RC osc. clk ;
; from 7.373MHz to 14.746MHz. ;
;---------------------------------;
Double_Clk:
mov dptr, #CLKCON
movx a, @dptr
orl a, #00001000B ; double the clock speed to 14.746MHz
movx @dptr,a
ret
;---------------------------------;
; Initialize the SPI interface ;
; and the pins associated to SPI. ;
;---------------------------------;
Init_SPI:
; Configure MOSI (P2.2), CS* (P2.4), and SPICLK (P2.5) as push-pull outputs (see table 42, page 51)
anl P2M1, #low(not(00110100B))
orl P2M2, #00110100B
; Configure MISO (P2.3) as input (see table 42, page 51)
orl P2M1, #00001000B
anl P2M2, #low(not(00001000B))
; Configure SPI
mov SPCTL, #11010000B ; Ignore /SS, Enable SPI, DORD=0, Master=1, CPOL=0, CPHA=0, clk/4
ret
;---------------------------------;
; Sends AND receives a byte via ;
; SPI. ;
;---------------------------------;
Send_SPI:
mov SPDAT, a
Send_SPI_1:
mov a, SPSTAT
jnb acc.7, Send_SPI_1 ; Check SPI Transfer Completion Flag
mov SPSTAT, a ; Clear SPI Transfer Completion Flag
mov a, SPDAT ; return received byte via accumulator
ret
;---------------------------------;
; SPI flash 'write enable' ;
; instruction. ;
;---------------------------------;
Enable_Write:
clr FLASH_CE
mov a, #WRITE_ENABLE
lcall Send_SPI
setb FLASH_CE
ret
;---------------------------------;
; This function checks the 'write ;
; in progress' bit of the SPI ;
; flash memory. ;
;---------------------------------;
Check_WIP:
clr FLASH_CE
mov a, #READ_STATUS
lcall Send_SPI
mov a, #0x55
lcall Send_SPI
setb FLASH_CE
jb acc.0, Check_WIP ; Check the Write in Progress bit
ret
;---------------------------------;
; CRC-CCITT (XModem) Polynomial: ;
; x^16 + x^12 + x^5 + 1 (0x1021) ;
; CRC in [R7,R6]. ;
; Converted to a macro to remove ;
; the overhead of 'lcall' and ;
; 'ret' instructions, since this ;
; 'routine' may be executed over ;
; 4 million times! ;
;---------------------------------;
;crc16:
crc16 mac
xrl a, r7 ; XOR high of CRC with byte
mov r0, a ; Save for later use
mov dptr, #CRC16_TH ; dptr points to table high
movc a, @a+dptr ; Get high part from table
xrl a, r6 ; XOR With low byte of CRC
mov r7, a ; Store to high byte of CRC
mov a, r0 ; Retrieve saved accumulator
mov dptr, #CRC16_TL ; dptr points to table low
movc a, @a+dptr ; Get Low from table
mov r6, a ; Store to low byte of CRC
;ret
endmac
;---------------------------------;
; High constants for CRC-CCITT ;
; (XModem) Polynomial: ;
; x^16 + x^12 + x^5 + 1 (0x1021) ;
;---------------------------------;
CRC16_TH:
db 000h, 010h, 020h, 030h, 040h, 050h, 060h, 070h
db 081h, 091h, 0A1h, 0B1h, 0C1h, 0D1h, 0E1h, 0F1h
db 012h, 002h, 032h, 022h, 052h, 042h, 072h, 062h
db 093h, 083h, 0B3h, 0A3h, 0D3h, 0C3h, 0F3h, 0E3h
db 024h, 034h, 004h, 014h, 064h, 074h, 044h, 054h
db 0A5h, 0B5h, 085h, 095h, 0E5h, 0F5h, 0C5h, 0D5h
db 036h, 026h, 016h, 006h, 076h, 066h, 056h, 046h
db 0B7h, 0A7h, 097h, 087h, 0F7h, 0E7h, 0D7h, 0C7h
db 048h, 058h, 068h, 078h, 008h, 018h, 028h, 038h
db 0C9h, 0D9h, 0E9h, 0F9h, 089h, 099h, 0A9h, 0B9h
db 05Ah, 04Ah, 07Ah, 06Ah, 01Ah, 00Ah, 03Ah, 02Ah
db 0DBh, 0CBh, 0FBh, 0EBh, 09Bh, 08Bh, 0BBh, 0ABh
db 06Ch, 07Ch, 04Ch, 05Ch, 02Ch, 03Ch, 00Ch, 01Ch
db 0EDh, 0FDh, 0CDh, 0DDh, 0ADh, 0BDh, 08Dh, 09Dh
db 07Eh, 06Eh, 05Eh, 04Eh, 03Eh, 02Eh, 01Eh, 00Eh
db 0FFh, 0EFh, 0DFh, 0CFh, 0BFh, 0AFh, 09Fh, 08Fh
db 091h, 081h, 0B1h, 0A1h, 0D1h, 0C1h, 0F1h, 0E1h
db 010h, 000h, 030h, 020h, 050h, 040h, 070h, 060h
db 083h, 093h, 0A3h, 0B3h, 0C3h, 0D3h, 0E3h, 0F3h
db 002h, 012h, 022h, 032h, 042h, 052h, 062h, 072h
db 0B5h, 0A5h, 095h, 085h, 0F5h, 0E5h, 0D5h, 0C5h
db 034h, 024h, 014h, 004h, 074h, 064h, 054h, 044h
db 0A7h, 0B7h, 087h, 097h, 0E7h, 0F7h, 0C7h, 0D7h
db 026h, 036h, 006h, 016h, 066h, 076h, 046h, 056h
db 0D9h, 0C9h, 0F9h, 0E9h, 099h, 089h, 0B9h, 0A9h
db 058h, 048h, 078h, 068h, 018h, 008h, 038h, 028h
db 0CBh, 0DBh, 0EBh, 0FBh, 08Bh, 09Bh, 0ABh, 0BBh
db 04Ah, 05Ah, 06Ah, 07Ah, 00Ah, 01Ah, 02Ah, 03Ah
db 0FDh, 0EDh, 0DDh, 0CDh, 0BDh, 0ADh, 09Dh, 08Dh
db 07Ch, 06Ch, 05Ch, 04Ch, 03Ch, 02Ch, 01Ch, 00Ch
db 0EFh, 0FFh, 0CFh, 0DFh, 0AFh, 0BFh, 08Fh, 09Fh
db 06Eh, 07Eh, 04Eh, 05Eh, 02Eh, 03Eh, 00Eh, 01Eh
;---------------------------------;
; Low constants for CRC-CCITT ;
; (XModem) Polynomial: ;
; x^16 + x^12 + x^5 + 1 (0x1021) ;
;---------------------------------;
CRC16_TL:
db 000h, 021h, 042h, 063h, 084h, 0A5h, 0C6h, 0E7h
db 008h, 029h, 04Ah, 06Bh, 08Ch, 0ADh, 0CEh, 0EFh
db 031h, 010h, 073h, 052h, 0B5h, 094h, 0F7h, 0D6h
db 039h, 018h, 07Bh, 05Ah, 0BDh, 09Ch, 0FFh, 0DEh
db 062h, 043h, 020h, 001h, 0E6h, 0C7h, 0A4h, 085h
db 06Ah, 04Bh, 028h, 009h, 0EEh, 0CFh, 0ACh, 08Dh
db 053h, 072h, 011h, 030h, 0D7h, 0F6h, 095h, 0B4h
db 05Bh, 07Ah, 019h, 038h, 0DFh, 0FEh, 09Dh, 0BCh
db 0C4h, 0E5h, 086h, 0A7h, 040h, 061h, 002h, 023h
db 0CCh, 0EDh, 08Eh, 0AFh, 048h, 069h, 00Ah, 02Bh
db 0F5h, 0D4h, 0B7h, 096h, 071h, 050h, 033h, 012h
db 0FDh, 0DCh, 0BFh, 09Eh, 079h, 058h, 03Bh, 01Ah
db 0A6h, 087h, 0E4h, 0C5h, 022h, 003h, 060h, 041h
db 0AEh, 08Fh, 0ECh, 0CDh, 02Ah, 00Bh, 068h, 049h
db 097h, 0B6h, 0D5h, 0F4h, 013h, 032h, 051h, 070h
db 09Fh, 0BEh, 0DDh, 0FCh, 01Bh, 03Ah, 059h, 078h
db 088h, 0A9h, 0CAh, 0EBh, 00Ch, 02Dh, 04Eh, 06Fh
db 080h, 0A1h, 0C2h, 0E3h, 004h, 025h, 046h, 067h
db 0B9h, 098h, 0FBh, 0DAh, 03Dh, 01Ch, 07Fh, 05Eh
db 0B1h, 090h, 0F3h, 0D2h, 035h, 014h, 077h, 056h
db 0EAh, 0CBh, 0A8h, 089h, 06Eh, 04Fh, 02Ch, 00Dh
db 0E2h, 0C3h, 0A0h, 081h, 066h, 047h, 024h, 005h
db 0DBh, 0FAh, 099h, 0B8h, 05Fh, 07Eh, 01Dh, 03Ch
db 0D3h, 0F2h, 091h, 0B0h, 057h, 076h, 015h, 034h
db 04Ch, 06Dh, 00Eh, 02Fh, 0C8h, 0E9h, 08Ah, 0ABh
db 044h, 065h, 006h, 027h, 0C0h, 0E1h, 082h, 0A3h
db 07Dh, 05Ch, 03Fh, 01Eh, 0F9h, 0D8h, 0BBh, 09Ah
db 075h, 054h, 037h, 016h, 0F1h, 0D0h, 0B3h, 092h
db 02Eh, 00Fh, 06Ch, 04Dh, 0AAh, 08Bh, 0E8h, 0C9h
db 026h, 007h, 064h, 045h, 0A2h, 083h, 0E0h, 0C1h
db 01Fh, 03Eh, 05Dh, 07Ch, 09Bh, 0BAh, 0D9h, 0F8h
db 017h, 036h, 055h, 074h, 093h, 0B2h, 0D1h, 0F0h
Line1: db 'CH3 CH2 CH1 CH0', 0
Line2: db 'xxx xxx xxx xxx', 0
; Display a 3-digit BCD number in the LCD
LCD_3BCD:
mov a, bcd+1
anl a, #0x0f
orl a, #'0'
lcall ?WriteData
mov a, bcd+0
swap a
anl a, #0x0f
orl a, #'0'
lcall ?WriteData
mov a, bcd+0
anl a, #0x0f
orl a, #'0'
lcall ?WriteData
ret
Display_ADC_Values:
; Analog input to pin P1.7
mov x+0, AD0DAT3
mov x+1, #0
mov x+2, #0
mov x+3, #0
lcall Hex2BCD
Set_Cursor(2, 1)
lcall LCD_3BCD
; Analog input to pin P0.0
mov x+0, AD0DAT2
mov x+1, #0
mov x+2, #0
mov x+3, #0
lcall Hex2BCD
Set_Cursor(2, 5)
lcall LCD_3BCD
; Analog input to pin P2.0
mov x+0, AD0DAT1
mov x+1, #0
mov x+2, #0
mov x+3, #0
lcall Hex2BCD
Set_Cursor(2, 9)
lcall LCD_3BCD
; Analog input to pin P2.1
mov x+0, AD0DAT0
mov x+1, #0
mov x+2, #0
mov x+3, #0
lcall Hex2BCD
Set_Cursor(2, 13)
lcall LCD_3BCD
; Some delay so the LCD looks ok
Wait_Milli_Seconds(#250)
ret
END