-
Notifications
You must be signed in to change notification settings - Fork 0
/
IVC_calc.asm
432 lines (390 loc) · 9.93 KB
/
IVC_calc.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
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
;------------------------------------------------------------------------------
; Îáðàáîòêà ðåçóëüòàòîâ èçìåðåíèé
;
; (C) 2017-2021 Vitaliy Zinoviev
; https://github.com/nf-zvv/IVC_Tracer_v2
;
; History
; =======
; 07.10.2021 Ïîäïðîãðàììû Calculate_current è Calculate_voltage
; èç ãëàâíîãî ôàéëà IVC_Tracer_v2.asm ïåðåìåùåíû ñþäà
; 07.10.2021 Äîáàâëåíû Calculate_power, Calculate_resistance
;
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Ïðåîáðàçîâàíèå êîäà ÀÖÏ â ìèëëèàìïåðû
;
; Current_mA = (((ADC_code * ADC_V_REF / 4096) - CH0_DELTA) * 1000) / ACS712_KI
;
; Óìíîæåíèå íà 1000 çäåñü íåîáõîäèìî èç-çà òîãî, ÷òî êîýôôèöèåíò ACS712_KI
; ïåðåâîäèò çíà÷åíèå èç ì â À, à íàì íóæíû ìÀ.
;
; MCP3204 - 12-áèòíûé ÀÖÏ. Ìàêñèìàëüíîå çíà÷åíèå ADC_code = 4095
; Îïîðíîå íàïðÿæåíèå ÀÖÏ ADC_V_REF = 5000 ìÂ
; TODO: ñäåëàòü îêðóãëåíèå (ADC_code * ADC_V_REF / 4096)
; IN: r17:r16 - ADC_code
; OUT: CURRENT_MA, r19:r18 - mA
;------------------------------------------------------------------------------
Calculate_current:
; Ïðåîáðàçîâàíèå êîäà ÀÖÏ â ìèëëèâîëüòû
; Óìíîæèòü íà çíà÷åíèå îïîðíîãî íàïðÿæåíèÿ â ìÂ
lds r18,ADC_V_REF+0
lds r19,ADC_V_REF+1
rcall mul16u ; (IN: r17:r16, r19:r18, OUT: r25:r24:r23:r22)
; Ïîäåëèòü íà ðàçðÿäíîñòü ÀÖÏ
rcall DIV_4096 ; (IN, OUT: r25:r24:r23:r22)
; Âû÷åñòü ñìåùåíèå
mov r20,r22
mov r21,r23
lds r24,CH0_DELTA+0
lds r25,CH0_DELTA+1
sub r20,r24
sbc r21,r25
; Óìíîæåíèå íà 1000
; IN: r21:r20, r19:r18
; OUT: r25:r24:r23:r22
ldi r18,low(1000)
ldi r19,high(1000)
rcall muls16x16_32
; Äåëåíèå
lds r18,ACS712_KI
ldi r19,0x00 ; 0
ldi r20,0x00 ; 0
ldi r21,0x00 ; 0
rcall __divmodsi4 ; (OUT: r21:r20:r19:r18)
; save current
sts CURRENT_MA+1,r19
sts CURRENT_MA+0,r18
ret
;------------------------------------------------------------------------------
; Ïðåîáðàçîâàíèå êîäà ÀÖÏ â ìèëëèâîëüòû
;
; Voltage_mV = (CH1_DELTA - (ADC_code * ADC_V_REF / 4096) ) * RESDIV_KU
;
; IN: r17:r16 - ADC_code
; OUT: VOLTAGE_MV, r23:r22 - mV
;------------------------------------------------------------------------------
Calculate_voltage:
; Ïðåîáðàçîâàíèå êîäà ÀÖÏ â ìèëëèâîëüòû
; Óìíîæèòü íà çíà÷åíèå îïîðíîãî íàïðÿæåíèÿ â ìÂ
lds r18,ADC_V_REF+0
lds r19,ADC_V_REF+1
rcall mul16u ; (IN: r17:r16, r19:r18, OUT: r25:r24:r23:r22)
; Ïîäåëèòü íà ðàçðÿäíîñòü ÀÖÏ
rcall DIV_4096 ; (IN, OUT: r25:r24:r23:r22)
; Âû÷åñòü ñìåùåíèå
; r21:r20 = r21:r20 - r25:r24
lds r20,CH1_DELTA+0
lds r21,CH1_DELTA+1
mov r24,r22
mov r25,r23
;mov r20,r22
;mov r21,r23
;lds r24,CH1_DELTA+0
;lds r25,CH1_DELTA+1
sub r20,r24
sbc r21,r25
; Óìíîæèòü íà êîýôôèöèåíò äåëèòåëÿ íàïðÿæåíèÿ
; IN: r21:r20, r19:r18
; OUT: r25:r24:r23:r22
lds r18,RESDIV_KU
ldi r19,0
rcall muls16x16_32
; save voltage
sts VOLTAGE_MV+1,r23
sts VOLTAGE_MV+0,r22
ret
;------------------------------------------------------------------------------
; Calculate power
;
; Power_mW = Voltage_mV * Current_mA / 1000
;
; IN: VOLTAGE_MV, CURRENT_MA
; OUT: POWER_MW
;------------------------------------------------------------------------------
Calculate_power:
; IN: r21:r20, r19:r18
; OUT: r25:r24:r23:r22
lds r21,VOLTAGE_MV+1
lds r20,VOLTAGE_MV+0
lds r19,CURRENT_MA+1
lds r18,CURRENT_MA+0
call muls16x16_32
; IN: r25:r24:r23:r22 - Dividend
; IN: r21:r20:r19:r18 - Divisor
; OUT: r21:r20:r19:r18 - Result
ldi r21,0x00
ldi r20,0x00
ldi r19,high(1000)
ldi r18,high(1000)
call __divmodsi4
sts POWER_MW+1,r19
sts POWER_MW+0,r18
ret
;------------------------------------------------------------------------------
; Calculate resistance
;
; Resistance_Ohm = (Voltage_mV << 16) / Current_mA
;
; IN: VOLTAGE_MV, CURRENT_MA
; OUT: RES_OHM_INT, RES_OHM_FRAC
;------------------------------------------------------------------------------
Calculate_resistance:
; IN: r21:r20, r19:r18
; OUT: r25:r24:r23:r22
lds r21,VOLTAGE_MV+1
lds r20,VOLTAGE_MV+0
lds r19,CURRENT_MA+1
lds r18,CURRENT_MA+0
; Ïðîâåðèòü òîê íà íîëü!!!
; (äåëåíèå íà íîëü)
ldi r17,0
ldi r16,0
cp r18,r16
cpc r19,r17
breq ZERO_DIVISION
; First of all need to drop the sign
; of voltage and current
; signed -> unsigned
; r25:r24:r23:r22 - Dividend
; r21:r20:r19:r18 - Divisor
lds r25,VOLTAGE_MV+1
lds r24,VOLTAGE_MV+0
ldi r23,0x00
ldi r22,0x00
; check for sign
bst r25, 7
brtc Calculate_resistance_1
; óáèðàåì çíàê
com r25
neg r24
sbci r25,0xFF
Calculate_resistance_1:
ldi r21,0x00
ldi r20,0x00
lds r19,CURRENT_MA+1
lds r18,CURRENT_MA+0
; check for sign
bst r19, 7
brtc Calculate_resistance_2
; óáèðàåì çíàê
com r19
neg r18
sbci r19,0xFF
Calculate_resistance_2:
call __udivmodsi4 ; (OUT: r21:r20:r19:r18)
; r21:r20 - integer part of number
; r19:r18 - fractional part of number
; convert fractional part to number
; IN: r19:r18
; OUT: r23:r22
rcall fract_part_10bit
sts RES_OHM_INT+1,r21
sts RES_OHM_INT+0,r20
sts RES_OHM_FRAC+1,r23
sts RES_OHM_FRAC+0,r22
ret
ZERO_DIVISION:
ldi r21,high(9999)
ldi r20,low(9999)
sts RES_OHM_INT+1,r21
sts RES_OHM_INT+0,r20
ret
;------------------------------------------------------------------------------
; input: r19:r18 - fractional part of number
; output: r23:r22
; used: r23:r22,r25:r24
;------------------------------------------------------------------------------
fract_part_10bit:
clr r22
clr r23
lsl r19
brcc fp10b_next_bit_1
ldi r22,low(5000)
ldi r23,high(5000)
fp10b_next_bit_1:
lsl r19
brcc fp10b_next_bit_2
ldi r24,low(2500)
ldi r25,high(2500)
add r22,r24
adc r23,r25
fp10b_next_bit_2:
lsl r19
brcc fp10b_next_bit_3
ldi r24,low(1250)
ldi r25,high(1250)
add r22,r24
adc r23,r25
fp10b_next_bit_3:
lsl r19
brcc fp10b_next_bit_4
ldi r24,low(625)
ldi r25,high(625)
add r22,r24
adc r23,r25
fp10b_next_bit_4:
lsl r19
brcc fp10b_next_bit_5
ldi r24,low(313)
ldi r25,high(313)
add r22,r24
adc r23,r25
fp10b_next_bit_5:
lsl r19
brcc fp10b_next_bit_6
ldi r24,156
ldi r25,0
add r22,r24
adc r23,r25
fp10b_next_bit_6:
lsl r19
brcc fp10b_next_bit_7
ldi r24,78
ldi r25,0
add r22,r24
adc r23,r25
fp10b_next_bit_7:
lsl r19
brcc fp10b_next_bit_8
ldi r24,39
ldi r25,0
add r22,r24
adc r23,r25
fp10b_next_bit_8:
lsl r18
brcc fp10b_next_bit_9
ldi r24,20
ldi r25,0
add r22,r24
adc r23,r25
fp10b_next_bit_9:
lsl r18
brcc fp10b_exit
ldi r24,10
ldi r25,0
add r22,r24
adc r23,r25
fp10b_exit:
ret
;------------------------------------------------------------------------------
; Convert resistance to string
; IN: RES_OHM_INT, RES_OHM_FRAC
; OUT: Y - pointer to null-terminating string
;------------------------------------------------------------------------------
RES_TO_STR:
; Ñíà÷àëà ðàçáèðàåìñÿ â öåëîé ÷àñòüþ
lds r27,RES_OHM_INT+1
lds r26,RES_OHM_INT+0
; Ïðîâåðÿåì ñëó÷àé, êîãäà òîê ðàâåí íóëþ
ldi r17,high(9999)
ldi r16,low(9999)
cp r26,r16
cpc r27,r17
brne RES_TO_STR_OK
rjmp RES_TO_STR_DASH
RES_TO_STR_OK:
; Íàäî ïðîâåðèòü, ÷òî âû÷èñëåííîå çíà÷åíèå íå áîëüøå 1000
ldi r17,high(1000)
ldi r16,low(1000)
cp r26,r16
cpc r27,r17
brsh RES_TO_STR_BIG
; íà÷èíàåì ïðåîáðàçîâûâàòü
; Ñîòíè
LDI r16, -1
RES_TO_STR_1:
INC r16
SUBI r26, Low(100)
SBCI r27, High(100)
BRSH RES_TO_STR_1
SUBI r26, -100
TST r16 ; ïðîâåðêà íà 0
BRNE RES_TO_STR_1_NONZERO
LDI r16,' '
CLT
RJMP RES_TO_STR_1_SAVE
RES_TO_STR_1_NONZERO:
SUBI r16,-0x30 ; ïðåîáðàçîâàòü öèôðó â ASCII êîä
SET
RES_TO_STR_1_SAVE:
ST Y+,r16 ; ñîõðàíèòü êîä öèôðû
; Äåñÿòêè
LDI r16, -1
RES_TO_STR_2:
INC r16
SUBI r26, 10
BRSH RES_TO_STR_2
SUBI r26,-10
BRTS RES_TO_STR_2_NONZERO ; åñëè ôëàã Ò óñòàíîâëåí, çíà÷èò ïðîïóñêàåì
TST r16 ; ïðîâåðêà íà 0
BRNE RES_TO_STR_2_NONZERO
LDI r16,' '
RJMP RES_TO_STR_2_SAVE
RES_TO_STR_2_NONZERO:
SUBI r16,-0x30 ; ïðåîáðàçîâàòü öèôðó â ASCII êîä
RES_TO_STR_2_SAVE:
CLT
ST Y+,r16 ; ñîõðàíèòü êîä öèôðû
; Åäèíèöû
SUBI r26,-0x30 ; ïðåîáðàçîâàòü öèôðó â ASCII êîä
ST Y+,r26 ; ñîõðàíèòü êîä öèôðû
ldi r16,'.'
ST Y+,r16
; Ïåðåõîäèì ê äðîáíîé ÷àñòè
lds r26,RES_OHM_FRAC+0
lds r27,RES_OHM_FRAC+1
LDI r16, -1
RES_TO_STR_3:
INC r16
SUBI r26, Low(1000)
SBCI r27, High(1000)
BRSH RES_TO_STR_3
SUBI r26, Low(-1000)
SBCI r27, High(-1000)
SUBI r16,-0x30 ; ïðåîáðàçîâàòü öèôðó â ASCII êîä
ST Y+,r16 ; ñîõðàíèòü êîä öèôðû
LDI r16, -1
RES_TO_STR_4:
INC r16
SUBI r26, Low(100)
SBCI r27, High(100)
BRSH RES_TO_STR_4
SUBI r26, -100
SUBI r16,-0x30 ; ïðåîáðàçîâàòü öèôðó â ASCII êîä
ST Y+,r16 ; ñîõðàíèòü êîä öèôðû
LDI r16, -1
RES_TO_STR_5:
INC r16
SUBI r26, 10
BRSH RES_TO_STR_5
SUBI r16,-0x30 ; ïðåîáðàçîâàòü öèôðó â ASCII êîä
ST Y+,r16 ; ñîõðàíèòü êîä öèôðû
CLR r16
ST Y+,r16 ; \0 - null-terminating string
ret
RES_TO_STR_BIG:
; Ïîìåñòèòü â áóôåð ñòðîêó "999.999"
ldi ZH,high(2*OVER_RESISTANCE_str)
ldi ZL,low(2*OVER_RESISTANCE_str)
RES_TO_STR_BIG_LOOP:
lpm r16,Z+
ST Y+,r16
tst r16
brne RES_TO_STR_BIG_LOOP
ret
RES_TO_STR_DASH:
; Ïîìåñòèòü â áóôåð ñòðîêó "---.---"
ldi ZH,high(2*ZERO_CURRENT_str)
ldi ZL,low(2*ZERO_CURRENT_str)
RES_TO_STR_DASH_LOOP:
lpm r16,Z+
ST Y+,r16
tst r16
brne RES_TO_STR_DASH_LOOP
ret
OVER_RESISTANCE_str: .db "999.999",0
ZERO_CURRENT_str: .db "---.---",0
;------------------------------------------------------------------------------
; End of file
;------------------------------------------------------------------------------