-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADC_LED_ref_lab3.asm
319 lines (266 loc) · 6.87 KB
/
ADC_LED_ref_lab3.asm
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
; 76E003 ADC test program: Reads channel 7 on P1.1, pin 14
; This version uses an LED as voltage reference connected to pin 6 (P1.7/AIN0)
$NOLIST
$MODN76E003
$LIST
; N76E003 pinout:
; -------
; PWM2/IC6/T0/AIN4/P0.5 -|1 20|- P0.4/AIN5/STADC/PWM3/IC3
; TXD/AIN3/P0.6 -|2 19|- P0.3/PWM5/IC5/AIN6
; RXD/AIN2/P0.7 -|3 18|- P0.2/ICPCK/OCDCK/RXD_1/[SCL]
; RST/P2.0 -|4 17|- P0.1/PWM4/IC4/MISO
; INT0/OSCIN/AIN1/P3.0 -|5 16|- P0.0/PWM3/IC3/MOSI/T1
; INT1/AIN0/P1.7 -|6 15|- P1.0/PWM2/IC2/SPCLK
; GND -|7 14|- P1.1/PWM1/IC1/AIN7/CLO
;[SDA]/TXD_1/ICPDA/OCDDA/P1.6 -|8 13|- P1.2/PWM0/IC0
; VDD -|9 12|- P1.3/SCL/[STADC]
; PWM5/IC7/SS/P1.5 -|10 11|- P1.4/SDA/FB/PWM1
; -------
;
CLK EQU 16600000 ; Microcontroller system frequency in Hz
BAUD EQU 115200 ; Baud rate of UART in bps
TIMER1_RELOAD EQU (0x100-(CLK/(16*BAUD)))
TIMER0_RELOAD_1MS EQU (0x10000-(CLK/1000))
ORG 0x0000
ljmp main
; 1234567890123456 <- This helps determine the location of the counter
test_message: db '*** ADC TEST ***', 0
value_message: db 'V(pin 14)= ', 0
cseg
; These 'equ' must match the hardware wiring
LCD_RS equ P1.3
LCD_E equ P1.4
LCD_D4 equ P0.0
LCD_D5 equ P0.1
LCD_D6 equ P0.2
LCD_D7 equ P0.3
$NOLIST
$include(LCD_4bit.inc) ; A library of LCD related functions and utility macros
$LIST
; These register definitions needed by 'math32.inc'
DSEG at 30H
x: ds 4
y: ds 4
bcd: ds 5
VLED_ADC: ds 2
BSEG
mf: dbit 1
$NOLIST
$include(math32.inc)
$LIST
Init_All:
; Configure all the pins for biderectional I/O
mov P3M1, #0x00
mov P3M2, #0x00
mov P1M1, #0x00
mov P1M2, #0x00
mov P0M1, #0x00
mov P0M2, #0x00
orl CKCON, #0x10 ; CLK is the input for timer 1
orl PCON, #0x80 ; Bit SMOD=1, double baud rate
mov SCON, #0x52
anl T3CON, #0b11011111
anl TMOD, #0x0F ; Clear the configuration bits for timer 1
orl TMOD, #0x20 ; Timer 1 Mode 2
mov TH1, #TIMER1_RELOAD ; TH1=TIMER1_RELOAD;
setb TR1
; Using timer 0 for delay functions. Initialize here:
clr TR0 ; Stop timer 0
orl CKCON,#0x08 ; CLK is the input for timer 0
anl TMOD,#0xF0 ; Clear the configuration bits for timer 0
orl TMOD,#0x01 ; Timer 0 in Mode 1: 16-bit timer
; Initialize the pins used by the ADC (P1.1, P1.7) as input.
orl P1M1, #0b10000010
anl P1M2, #0b01111101
; Initialize and start the ADC:
anl ADCCON0, #0xF0
orl ADCCON0, #0x07 ; Select channel 7
; AINDIDS select if some pins are analog inputs or digital I/O:
mov AINDIDS, #0x00 ; Disable all analog inputs
orl AINDIDS, #0b10000001 ; Activate AIN0 and AIN7 analog inputs
orl ADCCON1, #0x01 ; Enable ADC
ret
InitSerialPort:
; Since the reset button bounces, we need to wait a bit before
; sending messages, otherwise we risk displaying gibberish!
mov R1, #200
mov R0, #104
djnz R0, $ ; 4 cycles->4*60.285ns*104=25us
djnz R1, $-4 ; 25us*200=5.0ms
; Now we can proceed with the configuration of the serial port
orl CKCON, #0x10 ; CLK is the input for timer 1
orl PCON, #0x80 ; Bit SMOD=1, double baud rate
mov SCON, #0x52
anl T3CON, #0b11011111
anl TMOD, #0x0F ; Clear the configuration bits for timer 1
orl TMOD, #0x20 ; Timer 1 Mode 2
mov TH1, #TIMER1_RELOAD
setb TR1
ret
; Send a character using the serial port
putchar:
jnb TI, putchar
clr TI
mov SBUF, a
ret
;send a BCD num to PuTTy
Send_BCD mac
push ar0
mov r0, %0
lcall ?Send_BCD
pop ar0
endmac
?Send_BCD:
push acc
;write most significant digit
mov a, r0
swap a
anl a, #0fh
orl a, #30h
lcall putchar
;write least significant digit
mov a, r0
anl a, #0fh
orl a, #30h
lcall putchar
pop acc
ret
Send_formated_BCD:
Send_BCD(bcd+2)
Send_BCD(bcd+1)
mov a, #46
lcall putchar
Send_BCD(bcd+0)
lcall putCR_NL
ret
; Send a constant-zero-terminated string using the serial port
SendString:
clr A
movc A, @A+DPTR
jz SendStringDone
lcall putchar
inc DPTR
sjmp SendString
SendStringDone:
ret
Hello_World:
DB 'Hello, World!', '\r', '\n', 0
wait_1ms:
clr TR0 ; Stop timer 0
clr TF0 ; Clear overflow flag
mov TH0, #high(TIMER0_RELOAD_1MS)
mov TL0,#low(TIMER0_RELOAD_1MS)
setb TR0
jnb TF0, $ ; Wait for overflow
ret
; Wait the number of miliseconds in R2
waitms:
lcall wait_1ms
djnz R2, waitms
ret
; We can display a number any way we want. In this case with
; four decimal places.
Display_formated_BCD:
Set_Cursor(2, 10)
Display_BCD(bcd+2)
Display_char(#'.')
Display_BCD(bcd+1)
Display_BCD(bcd+0)
Set_Cursor(2, 10)
Display_char(#'=')
ret
Read_ADC:
clr ADCF
setb ADCS ; ADC start trigger signal
jnb ADCF, $ ; Wait for conversion complete
; Read the ADC result and store in [R1, R0]
mov a, ADCRL
anl a, #0x0f
mov R0, a
mov a, ADCRH
swap a
push acc
anl a, #0x0f
mov R1, a
pop acc
anl a, #0xf0
orl a, R0
mov R0, A
ret
putNum:
clr a
mov a, r1
mov b, #10
div ab
add a, #48
lcall putchar
mov a, b
add a, #48
lcall putchar
ret
putCR_NL: ;function to print Carriage return and NL
clr a
mov a, #13
lcall putchar
mov a, #10
lcall putchar
ret
main:
mov sp, #0x7f
lcall Init_All
lcall LCD_4BIT
;now inserting the serial initialization
lcall InitSerialPort
mov DPTR, #Hello_World
lcall SendString
; initial messages in LCD
Set_Cursor(1, 1)
Send_Constant_String(#test_message)
Set_Cursor(2, 1)
Send_Constant_String(#value_message)
Forever:
; Read the 2.08V LED voltage connected to AIN0 on pin 6
anl ADCCON0, #0xF0
orl ADCCON0, #0x00 ; Select channel 0
lcall Read_ADC
; Save result for later use
mov VLED_ADC+0, R0
mov VLED_ADC+1, R1
; Read the signal connected to AIN7
anl ADCCON0, #0xF0
orl ADCCON0, #0x07 ; Select channel 7
lcall Read_ADC
; Convert to voltage
mov x+0, R0
mov x+1, R1
; Pad other bits with zero
mov x+2, #0
mov x+3, #0
Load_y(20430) ; The MEASURED LED voltage: 2.074V, with 4 decimal places
lcall mul32
; Retrive the ADC LED value
mov y+0, VLED_ADC+0
mov y+1, VLED_ADC+1
; Pad other bits with zero
mov y+2, #0
mov y+3, #0
lcall div32
;use put char to put individual characters
; Convert to BCD and display
lcall hex2bcd
lcall Display_formated_BCD
Load_y(27305)
lcall sub32
lcall hex2bcd
lcall Send_formated_BCD
mov R2, #250
lcall waitms
mov R2, #250
lcall waitms
; Wait 500 ms between conversions
mov R2, #250
lcall waitms
mov R2, #250
lcall waitms
ljmp Forever
END