-
Notifications
You must be signed in to change notification settings - Fork 0
/
IVC_Tracer_v2.asm
900 lines (813 loc) · 23.9 KB
/
IVC_Tracer_v2.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
;------------------------------------------------------------------------------
; Àâòîìàòè÷åñêîå ñíÿòèå ÂÀÕ ñîëíå÷íîãî ìîäóëÿ
; Âòîðàÿ âåðñèÿ ïðèáîðà
; Äëÿ ñíÿòèÿ ïîëíîé âåòâè ÂÀÕ
;
; (C) 2019-2020 Vitaliy Zinoviev
; https://github.com/nf-zvv/IVC_Tracer_v2
;
; Fuses:
; Low Fuse 0xF7
; High Fuse 0xD4
; Extended Fuse 0xFD
; avrdude -U lfuse:w:0xF7:m -U hfuse:w:0xD4:m -U efuse:w:0xFD:m
;
; Bootloader:
; optiboot_flash_atmega1284p_UART0_115200_18432000L_B7_BIGBOOT
; LED on PB7 (two blinks)
; https://github.com/MCUdude/optiboot_flash
; https://github.com/Optiboot/optiboot/wiki/HowOptibootWorks
;
; Burn flash via UART:
; avrdude -c arduino -b 115200 -P COM9 -p m1284p -U flash:w:IVC_Tracer_v2.hex:i
;
; Burn flash via ISP:
; avrdude -c usbasp -p m1284p -B10 -U flash:w:IVC_Tracer_v2.hex
;
; History
; =======
;------------------------------------------------------------------------------
#define F_CPU (18432000)
;.device ATmega1284P
.nolist
.include "m1284Pdef.inc"
.include "macro.asm"
.include "eeprom_macro.asm"
.include "uart_macro.asm"
.list
.LISTMAC ; Âêëþ÷èòü ðàçâîðà÷èâàíèå ìàêðîñîâ
; Íóëåâîé ðåãèñòð
.ifndef __zero_reg__
.def __zero_reg__ = r2
.endif
; Èñïîëüçóåòñÿ ýíêîäåðîì
.def __enc_reg__ = r7
; Ôëàãè
.equ enc_left_spin = 0
.equ enc_right_spin = 1
.equ btn_press = 2
.equ btn_long_press = 3
.equ update = 4
.equ enc_channel = 5
.equ menu_edit = 6
.equ change_screen = 7
;-------------------------------------------
.equ UART_IN_FULL = 0 ; Ïðèåìíûé áóôåð UART ïîëîí
;.equ UART_OUT_FULL = 1 ; Áóôåð îòïðàâêè UART ïîëîí
.equ UART_STR_RCV = 2 ; Ïîëó÷åíà ñòðîêà ïî UART
.equ UART_CR = 3 ; Ôëàã ïîëó÷åíèÿ êîäà CR (0x0D) âîçâðàò êàðåòêè
;-------------------------------------------
; Ðàçìåð ïðè¸ìíîãî áóôåðà UART (255 max)
.equ MAXBUFF_IN = 64 ; Ðàçìåð âõîäÿùåãî áóôåðà
.equ IVC_MAX_RECORDS = 150
;-------------------------------------------
; Òàéìåð T0 |
;-------------------------------------------|
; âðåìÿ ñðàáîòêè òàéìåðà â ìèëèñåêóíäàõ
#define Period_T0 (1)
; äëÿ ðåæèìà CTC òàéìåðà
; Ïðåääåëèòåëü 256
#define CTC_OCR0A (Period_T0*F_CPU/(256*1000))
;-------------------------------------------
; Òàéìåð T1 |
;-------------------------------------------|
; âðåìÿ äî ïåðåïîëíåíèÿ òàéìåðà â ìèëèñåêóíäàõ
#define period_T1 300
; âû÷èñëåíèå íà÷àëüíîãî çíà÷åíèÿ
#define start_count_T1 (0x10000-(period_T1*F_CPU/(1024*1000)))
;-------------------------------------------
; HW SPI |
;-------------------------------------------|
.equ SPI_PORT = PORTB
.equ SPI_DDR = DDRB
.equ SPI_PIN = PINB
.equ SPI_SS = PB4
.equ SPI_MOSI = PB5
.equ SPI_MISO = PB6
.equ SPI_SCK = PB7
;-------------------------------------------
; Encoder |
;-------------------------------------------|
.equ ENC_A = PB0
.equ ENC_B = PB1
.equ ENC_Btn = PB2
.equ ENC_PORT = PORTB
.equ ENC_DDR = DDRB
.equ ENC_PIN = PINB
#define Default_DAC_STEP 32
#define Default_IVC_DAC_START 0x03e8 ; 1000
#define Default_IVC_DAC_END 0x0708 ; 1800
#define Default_IVC_DAC_STEP 0x0064 ; 100
#define Default_VAH_DELAY 40
#define Default_CH0_DELTA 2465
#define Default_CH1_DELTA 2489
#define Default_ADC_V_REF 4997
#define Default_ACS712_KI 186
#define Default_RESDIV_KU 8
#define Default_ZERO_DAC 2048
#define Default_VREF_DAC 1825
#define Default_LIM_VOLT_NEG -20000
#define Default_LIM_VOLT_POS 20000
#define Default_LIM_CURR_NEG -7000
#define Default_LIM_CURR_POS 7000
; Menu
.equ MAIN_SCREEN_ID = 0
.equ CALIBRATION_SCREEN_ID = 1
.equ IVC_TRACE_SCREEN_ID = 2
;===================================EEPROM=====================================
.eseg
.org 0x100
EEPROM_TEST: .db 0 ; äëÿ ïðîâåðêè, åñëè ðàâíî 0xFF, òî EEPROM ÷èñòà è íàäî ïðîèíèöèàëèçèðîâàòü
E_DAC_STEP: .dw Default_DAC_STEP
E_IVC_DAC_START: .dw Default_IVC_DAC_START
E_IVC_DAC_END: .dw Default_IVC_DAC_END
E_IVC_DAC_STEP: .dw Default_IVC_DAC_STEP
E_VAH_DELAY: .dw Default_VAH_DELAY
E_CH0_DELTA: .dw Default_CH0_DELTA
E_CH1_DELTA: .dw Default_CH1_DELTA
E_ADC_V_REF: .dw Default_ADC_V_REF
E_ACS712_KI: .dw Default_ACS712_KI
E_RESDIV_KU: .dw Default_RESDIV_KU
E_ZERO_DAC: .dw Default_ZERO_DAC
E_VREF_DAC: .dw Default_VREF_DAC
E_LIM_VOLT_NEG: .dw Default_LIM_VOLT_NEG
E_LIM_VOLT_POS: .dw Default_LIM_VOLT_POS
E_LIM_CURR_NEG: .dw Default_LIM_CURR_NEG
E_LIM_CURR_POS: .dw Default_LIM_CURR_POS
;====================================DATA======================================
.dseg
SYS_TICK_MS: .byte 4
START_TIME: .byte 4
NOW: .byte 4
;------------------------
ButtonCounter: .byte 2 ; êîëè÷åñòâî òèêîâ ïðè íàæàòîé êíîïêå ýíêîäåðà
Flags: .byte 1 ;
UART_Flags: .byte 1 ; ôëàãè äëÿ UART
; Áëîê äàííûõ äëÿ îïåðàöèé ñ äèñïëååì
DataBlock: .byte 17 ;
; Áóôåðû äëÿ ïðåîáðàçîâàíèÿ DEC2STR
STRING: .byte 30
;------------------------
DAC_STEP: .byte 2
IVC_DAC_START: .byte 2
IVC_DAC_END: .byte 2
IVC_DAC_STEP: .byte 2
VAH_DELAY: .byte 2
; Êàëèáðîâêà
CH0_DELTA: .byte 2
CH1_DELTA: .byte 2
ADC_V_REF: .byte 2
ACS712_KI: .byte 2
RESDIV_KU: .byte 2
ZERO_DAC: .byte 2
VREF_DAC: .byte 2
; Ëèìèòû
LIM_VOLT_NEG: .byte 2
LIM_VOLT_POS: .byte 2
LIM_CURR_NEG: .byte 2
LIM_CURR_POS: .byte 2
; Menu
menu_ID: .byte 1 ; èäåíòèôèêàòîð òåêóùåãî ïóíêòà ìåíþ
screen_ID: .byte 1 ; èäåíòèôèêàòîð òåêóùåãî ýêðàíà
; Measured values
VOLTAGE_MV: .byte 2 ;
CURRENT_MA: .byte 2 ;
POWER_MW: .byte 2 ;
RES_OHM_INT: .byte 2 ;
RES_OHM_FRAC: .byte 2 ;
;------------------------
IVC_ARRAY: .byte 2*2*IVC_MAX_RECORDS
;------------------------
;====================================CODE======================================
.cseg
.org 0000
rjmp RESET
.include "vectors_m1284p.inc"
;==============================================================================
; Îáðàáîò÷èêè ïðåðûâàíèé
; Interrupt Handlers
;==============================================================================
;------------------------------------------------------------------------------
; Îáðàáîò÷èê UART
;------------------------------------------------------------------------------
.include "uart_irq.asm"
;------------------------------------------------------------------------------
; Ïðåðûâàíèå òàéìåðà T0 ïî ñîâïàäåíèþ
; Timer/Counter0 Compare Match A Handler
; Îáñëóæèâàíèå ýíêîäåðà è êíîïêè
; Ïðåðûâàíèå òàéìåðà êàæäóþ 1 ìñ
;------------------------------------------------------------------------------
TIM0_OC0A_HANDLER:
push r16
in r16,SREG
push r16
push r17
push r24
push r25
push YL
push YH
;----------------------- Increase system uptime by 1 ms -----------------------
ldi YL,low(SYS_TICK_MS)
ldi YH,high(SYS_TICK_MS)
ld r24,Y ; get lower 16 bit
ldd r25,Y+1
adiw r24,1 ; add 1
st Y+,r24 ; write back to mem
st Y+,r25
brcc TIM0_OC0A_HANDLER_NEXT
ld r24,Y ; get higher 16 bit
ldd r25,Y+1
adiw r24,1 ; add 1
st Y+,r25 ; write back to mem
st Y+,r24
TIM0_OC0A_HANDLER_NEXT:
;---------------------------- Encoder rotate event ----------------------------
; ïîó÷åíèå òåêóùåãî ñîñòîÿíèÿ ýíêîäåðà
in r16,ENC_PIN
andi r16,(1<<ENC_A)|(1<<ENC_B)
;andi r16,0b11000000
;swap r16
;lsr r16
;lsr r16
; åñëè ïðåäûäóùåå ñîñòîÿíèå ðàâíî òåêóùåìó - âûõîäèì
mov r17,__enc_reg__ ; çàãðóæàåì ïîñëåäîâàòåëüíîñòü ñîñòîÿíèé
andi r17,0b00000011 ; îòäåëÿåì òîëüêî ïîñëåäíåå
cp r17,r16 ; ñðàâíèâàåì
breq ENC_BTN_PRESS ; íå èçìåíèëîñü - âûõîäèì
; åñëè æå ñîñòîÿíèå èçìåíèëîñü
lsl __enc_reg__ ; äâà ðàçà
lsl __enc_reg__ ; ñäâèãàåì
or __enc_reg__,r16 ; äîáàâëÿåì íîâîå ñîñòîÿíèå íà îñâîáîäèâøååñÿ ìåñòî
; ñðàâíèâàåì ïîëó÷èâøóþñÿ ïîñëåäîâàòåëüíîñòü
mov r17,__enc_reg__
cpi r17,0b11100001
brne next_spin
;sbr Flags_2,(1<<enc_left_spin) ; óñòàíîâêà ôëàãà
lds r16,Flags
ori r16,(1<<enc_left_spin)
sts Flags,r16
clr __enc_reg__
rjmp ENC_BTN_PRESS
next_spin:
cpi r17,0b11010010
brne ENC_BTN_PRESS
;sbr Flags_2,(1<<enc_right_spin) ; óñòàíîâêà ôëàãà
lds r16,Flags
ori r16,(1<<enc_right_spin)
sts Flags,r16
clr __enc_reg__
ENC_BTN_PRESS:
;------------------------- Encoder button press event -------------------------
;sbis BUTTON_PIN,BUTTON ; ñ÷èòûâàåì ñîñòîÿíèå êíîïêè
sbis ENC_PIN,ENC_Btn
rjmp int1_low ; åñëè êíîïêà íàæàòà, ïåðåõîäèì íà int1_low
; åñëè êíîïêà íå íàæàòà (èëè óæå îòïóùåíà?)
lds r24,ButtonCounter+0
lds r25,ButtonCounter+1
;ldi r16,0
;ldi r17,0
;cp r24,r16
;cpc r25,r17
;breq ovf0_exit
ldi r16,120
ldi r17,0
cp r24,r16
cpc r25,r17
brlo too_little_ticks ; ìàëî óäåðæèâàëè êíîïêó
; èòàê, íàáðàëîñü äîñòàòî÷íî òèêîâ
; ñ÷èòàåì ýòî êîðîòêèì íàæàòèåì
; óñòàíàâëèâàåì ôëàã êîðîòêîãî íàæàòèÿ
;sbr Flags_2,(1<<btn_press) ; âîçâåäåíèå ôëàãà
lds r16,Flags
ori r16,(1<<btn_press)
sts Flags,r16
; è îáíóëÿåì ButtonCounter:
too_little_ticks:
; åñëè íàáðàëîñü äî 164 òèêîâ:
; íåäîñòàòî÷íî äîëãî äåðæàëè,
; ëèáî ëîæíîå ñðàáàòûâàíèå
; îáíóëÿåì ButtonCounter
;clr r16
sts ButtonCounter+0,__zero_reg__
sts ButtonCounter+1,__zero_reg__
rjmp TIM0_OC0A_HANDLER_EXIT
int1_low:
; åñëè êíîïêà íàæàòà (INT1=0), òî ButtonCounter++
lds r24,ButtonCounter+0
lds r25,ButtonCounter+1
ldi r16,low(1000)
ldi r17,high(1000)
cp r24,r16
cpc r25,r17
brsh long_button_press ; íàáðàëîñü ìíîãî òèêîâ (äëèííîå íàæàòèå)
; åñëè íåäîñòàòî÷íî, ïðîñòî óâåëè÷èâàåì ñ÷åò÷èê è âûõîäèì
adiw r24,1
sts ButtonCounter+0,r24
sts ButtonCounter+1,r25
rjmp TIM0_OC0A_HANDLER_EXIT
long_button_press:
; óñòàíàâëèâàåì ôëàã äëèííîãî íàæàòèÿ
; (óäåðæàíèå êíîïêè íàæàòîé)
;sbr Flags_2,(1<<btn_long_press) ; âîçâåäåíèå ôëàãà
lds r16,Flags
ori r16,(1<<btn_long_press)
sts Flags,r16
; îáíóëÿåì ButtonCounter
sts ButtonCounter+0,__zero_reg__
sts ButtonCounter+1,__zero_reg__
TIM0_OC0A_HANDLER_EXIT:
pop YH
pop YL
pop r25
pop r24
pop r17
pop r16
out SREG,r16
pop r16
reti
;------------------------------------------------------------------------------
; Îáðàáîò÷èê ïåðåïîëíåíèÿ òàéìåðà T1
;------------------------------------------------------------------------------
OVF1_IRQ:
push r16
in r16,SREG
push r16
;----------------
; Óñàíàâëèâàåì ôëàã
lds r16,Flags
ori r16,(1<<update)
sts Flags,r16
; ïåðåèíèöèàëèçàöèÿ òàéìåðà
ldi r16,high(start_count_T1)
OutReg TCNT1H,r16
ldi r16,low(start_count_T1)
OutReg TCNT1L,r16
;----------------
pop r16
out SREG,r16
pop r16
reti
;==============================================================================
; EEPROM code
;==============================================================================
;------------------------------------------------------------------------------
; Èíèöèàëèçàöèÿ EEPROM
;------------------------------------------------------------------------------
EEPROM_PRELOAD:
ldi r16,low(EEPROM_TEST) ; Çàãðóæàåì àäðåñ ÿ÷åéêè EEPROM
ldi r17,high(EEPROM_TEST) ; èç êîòîðîé õîòèì ïðî÷èòàòü áàéò
rcall EERead ; (OUT: r18)
cpi r18,0xFF
breq EEPROM_INIT ; åñëè ðàâíî 0xFF - ïàìÿòü ïóñòà, íàäî èíèöèàëèçèðîâàòü
ret ; èíà÷å - âûõîäèì
EEPROM_INIT:
ldi r16,low(EEPROM_TEST)
ldi r17,high(EEPROM_TEST)
clr r18
rcall EEWrite
EEPROM_WRITE_WORD E_DAC_STEP,Default_DAC_STEP
EEPROM_WRITE_WORD E_IVC_DAC_START,Default_IVC_DAC_START
EEPROM_WRITE_WORD E_IVC_DAC_END,Default_IVC_DAC_END
EEPROM_WRITE_WORD E_IVC_DAC_STEP,Default_IVC_DAC_STEP
EEPROM_WRITE_WORD E_VAH_DELAY,Default_VAH_DELAY
EEPROM_WRITE_WORD E_CH0_DELTA,Default_CH0_DELTA
EEPROM_WRITE_WORD E_CH1_DELTA,Default_CH1_DELTA
EEPROM_WRITE_WORD E_ADC_V_REF,Default_ADC_V_REF
EEPROM_WRITE_WORD E_ACS712_KI,Default_ACS712_KI
EEPROM_WRITE_WORD E_RESDIV_KU,Default_RESDIV_KU
EEPROM_WRITE_WORD E_ZERO_DAC,Default_ZERO_DAC
EEPROM_WRITE_WORD E_VREF_DAC,Default_VREF_DAC
EEPROM_WRITE_WORD E_LIM_VOLT_NEG,Default_LIM_VOLT_NEG
EEPROM_WRITE_WORD E_LIM_VOLT_POS,Default_LIM_VOLT_POS
EEPROM_WRITE_WORD E_LIM_CURR_NEG,Default_LIM_CURR_NEG
EEPROM_WRITE_WORD E_LIM_CURR_POS,Default_LIM_CURR_POS
ret
;------------------------------------------------------------------------------
; Âîññòàíîâëåíèå ïåðåìåííûõ èç EEPROM â RAM
;------------------------------------------------------------------------------
EEPROM_RESTORE_VAR:
EEPROM_READ_WORD E_DAC_STEP,DAC_STEP
EEPROM_READ_WORD E_IVC_DAC_START,IVC_DAC_START
EEPROM_READ_WORD E_IVC_DAC_END,IVC_DAC_END
EEPROM_READ_WORD E_IVC_DAC_STEP,IVC_DAC_STEP
EEPROM_READ_WORD E_VAH_DELAY,VAH_DELAY
EEPROM_READ_WORD E_CH0_DELTA,CH0_DELTA
EEPROM_READ_WORD E_CH1_DELTA,CH1_DELTA
EEPROM_READ_WORD E_ADC_V_REF,ADC_V_REF
EEPROM_READ_WORD E_ACS712_KI,ACS712_KI
EEPROM_READ_WORD E_RESDIV_KU,RESDIV_KU
EEPROM_READ_WORD E_ZERO_DAC,ZERO_DAC
EEPROM_READ_WORD E_VREF_DAC,VREF_DAC
EEPROM_READ_WORD E_LIM_VOLT_NEG,LIM_VOLT_NEG
EEPROM_READ_WORD E_LIM_VOLT_POS,LIM_VOLT_POS
EEPROM_READ_WORD E_LIM_CURR_NEG,LIM_CURR_NEG
EEPROM_READ_WORD E_LIM_CURR_POS,LIM_CURR_POS
ret
;==============================================================================
; Main code
;==============================================================================
RESET:
; Stack init
ldi r16, low(RAMEND)
out SPL, r16
ldi r16, high(RAMEND)
out SPH, r16
; Îáíóëåíèå ïàìÿòè è ðåãèñòðîâ (îáúåì êîäà: 80 áàéò ïðîøèâêè)
.include "coreinit.inc"
; Íóëåâîé ðåãèñòð
clr __zero_reg__
; Àíàëîãîâûé êîìïàðàòîð âûêëþ÷åí
ldi r16,1<<ACD
out ACSR,r16
; Port A Init
ldi r16,0b00000000
out DDRA,r16
ldi r16,0b00000000
out PORTA,r16
; Port B Init
ldi r16,0b10110000
out DDRB,r16
ldi r16,0b01001111
out PORTB,r16
; Port C Init
ldi r16,0b00000000
out DDRC,r16
ldi r16,0b00000000
out PORTC,r16
; Port D Init
ldi r16,0b01100010
out DDRD,r16
ldi r16,0b01100011
out PORTD,r16
sts Flags,__zero_reg__
sts UART_Flags,__zero_reg__
;---------------------
; Èíèöèàëèçàöèÿ UART
;---------------------
USART_INIT
;---------------------
; Èíèöèàëèçàöèÿ SPI
rcall SPI_INIT
; Èíèöèàëèçàöèÿ ÀÖÏ
rcall ADC_INIT
; Èíèöèàëèçàöèÿ ÖÀÏ
rcall DAC_INIT
; Âîññòàíîâèòü ïåðåìåííûå èç EEPROM
rcall EEPROM_PRELOAD
rcall EEPROM_RESTORE_VAR
; Èíèöèàëèçàöèÿ èíòåðïðåòàòîðà êîìàíä UART
call UART_PARSER_INIT
;------------------------------------------------------------------
; CTC Mode for T0
; Ïðåðûâàíèå ïî ñîâïàäåíèþ êàæäóþ 1 ìñ
;------------------------------------------------------------------
ldi r16,0
OutReg TCNT0,r16
; Íàñòðîéêà ïðåääåëèòåëÿ 256
ldi r16,(1<<CS02)|(0<<CS01)|(0<<CS00)
OutReg TCCR0B,r16
; CTC Mode: WGM2=00,WGM01=1,WGM00=0
ldi r16,(1 << WGM01)
OutReg TCCR0A,r16
; OCR0A = CTC_OCRA
ldi r16,CTC_OCR0A
OutReg OCR0A,r16
; Interrupt Timer/Counter0 Output Compare Match A
ldi r16,(1 << OCIE0A)
OutReg TIMSK0,r16
;------------------------------------------------------------------
;------------------------------------------------------------------
; Èíèöèàëèçàöèÿ òàéìåðà Ò1
;------------------------------------------------------------------
; èíèöèàëèçàöèÿ íà÷àëüíîãî çíà÷åíèÿ òàéìåðà
; îò ýòîãî çíà÷åíèÿ òàéìåð áóäåò ñ÷èòàòü äî ïåðåïîëíåíèÿ
ldi r16,high(start_count_T1)
OutReg TCNT1H,r16
ldi r16,low(start_count_T1)
OutReg TCNT1L,r16
#if defined (__ATmega328P__) || defined(__ATmega1284P__)
; ðàçðåøåíèå ïðåðûâàíèÿ òàéìåðà ïî ïåðåïîëíåíèþ
InReg r16,TIMSK1
ori r16,(1<<TOIE1)
OutReg TIMSK1,r16
; Âêëþ÷èòü òàéìåð Ò1
ldi r16,5 ; Óñòàíîâêà ïðåääåëèòåëÿ 1024
OutReg TCCR1B,r16
#elif defined (__ATmega16A__) || defined(__ATmega16__)
; ðàçðåøåíèå ïðåðûâàíèÿ òàéìåðà ïî ïåðåïîëíåíèþ
InReg r16,TIMSK
ori r16,(1<<TOIE1)
OutReg TIMSK,r16
; Âêëþ÷èòü òàéìåð Ò1
ldi r16,5 ; Óñòàíîâêà ïðåääåëèòåëÿ 1024
OutReg TCCR1B,r16
#else
#error "Unsupported part:" __PART_NAME__
#endif // part specific code
;------------------------------------------------------------------
; Èíèöèàëèçàöèÿ äèñïëåÿ
call T6963C_Initalize
call T6963C_ClearText
call T6963C_ClearGraphic
call T6963C_ClearCG
call T6963C_FillCG
// Ïðîâåðî÷íûé âûâîä ñèìâîëîâ çíàêîãåíåðàòîðà
// clr r20
//char_loop:
// mov r18,r20
// rcall T6963C_WriteDisplayData
// inc r20
// cpi r20,255
// brne char_loop
// ldi r18,255
// rcall T6963C_WriteDisplayData
;ldi r18,0
;ldi r19,100
;ldi r20,64
;ldi r21,8
;rcall T6963C_FillRectangle
;ldi r18,2
;ldi r19,102
;ldi r20,60
;ldi r21,4
;rcall T6963C_ClrRectangle
; Øàã ÖÀÏ
ldi r16,32
sts DAC_STEP+0,r16
sts DAC_STEP+1,__zero_reg__
lds r16,VREF_DAC+0
sts DAC_CH_A+0,r16
lds r16,VREF_DAC+1
sts DAC_CH_A+1,r16
rcall DAC_SET_A
lds r16,ZERO_DAC+0
sts DAC_CH_B+0,r16
lds r16,ZERO_DAC+1
sts DAC_CH_B+1,r16
rcall DAC_SET_B
sei ; Ðàçðåøèòü ïðåðûâàíèÿ
lds r16,Flags
ori r16,(1 << change_screen)
sts Flags,r16
ldi r16,MAIN_SCREEN_ID
sts screen_ID,r16
rjmp MENU_EVENT_HANDLER
;------------------------------------------------------------------------------
; Ãëàâíûé öèêë. Ïðîâåðÿåì ôëàãè
;------------------------------------------------------------------------------
main:
; Íà âñÿêèé ñëó÷àé. Âäðóã ñþäà ïîïàä¸ì
rjmp main
;------------------------------------------------------------------------------
;
;
;------------------------------------------------------------------------------
Encoder_left:
lds r24,DAC_CH_B+0 ; óìåíüøàåìîå
lds r25,DAC_CH_B+1
lds r26,DAC_STEP+0 ; âû÷èòàåìîå
lds r27,DAC_STEP+1
cp r24,r26
cpc r25,r27
brlo DEC_DAC_TO_ZERO
rcall DECREMENT ; ðåçóëüòàò â r25:r24
rjmp DEC_DAC_SET
DEC_DAC_TO_ZERO:
; åñëè óìåíüøàåìîå ìåíüøå âû÷èòàåìîãî, òî ïðîñòî îáíóëÿåì óìåíüøàåìîå
clr r24
clr r25
DEC_DAC_SET:
sts DAC_CH_B+0,r24
sts DAC_CH_B+1,r25
rcall DAC_SET_B
; Âûâîäèì çíà÷åíèå íà äèñïëåé
; Óñòàíîâèòü êîîðäèíàòû âûâîäà
ldi r18,11
ldi r19,2
call T6963C_TextGoTo
; Ïðåîáðàçîâàòü ÷èñëî â ñòðîêó
lds XL,DAC_CH_B+0
lds XH,DAC_CH_B+1
ldi YL,low(STRING)
ldi YH,high(STRING)
call DEC_TO_STR5
; Âûâåñòè ÷èñëà íà äèñïëåé
ldi XL,low(STRING)
ldi XH,high(STRING)
call T6963C_WriteString
ret
;------------------------------------------------------------------------------
;
;
;------------------------------------------------------------------------------
Encoder_right:
lds r24,DAC_CH_B+0
lds r25,DAC_CH_B+1
lds r26,DAC_STEP+0
lds r27,DAC_STEP+1
; Ïðèáàâëÿåì øàã ê òåêóùåìó çíà÷åíèþ ÖÀÏ
rcall INCREMENT ; ðåçóëüòàò â r25:r24
; Ïðîâåðÿåì, íå ïðåâûøàåò ëè ðåçóëüòàò 4096
ldi r26,low(4096)
ldi r27,high(4096)
cp r24,r26
cpc r25,r27
brlo INC_DAC_SET
; åñëè ïðåâûøàåò, òî ïðèíóäèòåëüíî óñòàíàâëèâàåì 4095
ldi r24,low(4095)
ldi r25,high(4095)
INC_DAC_SET:
; Ñîõðàíÿåì ðåçóëüòàò
sts DAC_CH_B+0,r24
sts DAC_CH_B+1,r25
; Ïåðåäàåì çíà÷åíèå ìèêðîñõåìå
rcall DAC_SET_B
; Âûâîäèì çíà÷åíèå íà äèñïëåé
; Óñòàíîâèòü êîîðäèíàòû âûâîäà
ldi r18,11
ldi r19,2
call T6963C_TextGoTo
; Ïðåîáðàçîâàòü ÷èñëî â ñòðîêó
lds XL,DAC_CH_B+0
lds XH,DAC_CH_B+1
ldi YL,low(STRING)
ldi YH,high(STRING)
call DEC_TO_STR5
; Âûâåñòè ÷èñëà íà äèñïëåé
ldi XL,low(STRING)
ldi XH,high(STRING)
call T6963C_WriteString
ret
;------------------------------------------------------------------------------
;
;
;------------------------------------------------------------------------------
Event_update:
;cli
rcall ADC_RUN
;------------------------------------------------------
; Âûâåñòè òîê
;------------------------------------------------------
; Óñòàíîâèòü êîîðäèíàòû âûâîäà
ldi r18,1
ldi r19,9
call T6963C_TextGoTo
; Ïðåîáðàçîâàòü ÷èñëî â ñòðîêó
lds XL,ADC_CH0+0
lds XH,ADC_CH0+1
ldi YL,low(STRING)
ldi YH,high(STRING)
call DEC_TO_STR5
; Âûâåñòè ÷èñëî íà äèñïëåé
ldi XL,low(STRING)
ldi XH,high(STRING)
call T6963C_WriteString
lds r16,ADC_CH0+0
lds r17,ADC_CH0+1
rcall Calculate_current
; convert digit to string
lds XH,CURRENT_MA+1
lds XL,CURRENT_MA+0
ldi YL,low(STRING)
ldi YH,high(STRING)
;call DEC_TO_STR7
call ITOA_FAST_DIV
; Óñòàíîâèòü êîîðäèíàòû âûâîäà
ldi r18,1
ldi r19,8
call T6963C_TextGoTo
; Âûâåñòè ÷èñëî íà äèñïëåé
ldi XL,low(STRING)
ldi XH,high(STRING)
call T6963C_WriteString
;.......................................................
ldi r20,104
ldi r21,56
ldi r22,16
ldi r23,31
call T6963C_ShiftLeftDataBlock
; ïîñòðîèòü ëèíèþ
lds r20,ADC_CH0+0
lds r21,ADC_CH0+1
; ïîäåëèòü ðåçóëüòàò íà 128
add r20,r20
mov r20,r21
adc r20,r20
sbc r21,r21
neg r21
ldi r18,231 ; x
ldi r19,87
sub r19,r20 ; y
call T6963C_VertLine ; (IN: r18 - x, r19 - y, r20 - height)
;------------------------------------------------------
; Âûâåñòè íàïðÿæåíèå
;------------------------------------------------------
; Óñòàíîâèòü êîîðäèíàòû âûâîäà
ldi r18,1
ldi r19,14
call T6963C_TextGoTo
; Ïðåîáðàçîâàòü ÷èñëî â ñòðîêó
lds XL,ADC_CH1+0
lds XH,ADC_CH1+1
ldi YL,low(STRING)
ldi YH,high(STRING)
call DEC_TO_STR5
; Âûâåñòè ÷èñëî íà äèñïëåé
ldi XL,low(STRING)
ldi XH,high(STRING)
call T6963C_WriteString
lds r16,ADC_CH1+0
lds r17,ADC_CH1+1
rcall Calculate_voltage
; convert digit to string
lds XH,VOLTAGE_MV+1
lds XL,VOLTAGE_MV+0
ldi YL,low(STRING)
ldi YH,high(STRING)
;call DEC_TO_STR7
call ITOA_FAST_DIV
; Óñòàíîâèòü êîîðäèíàòû âûâîäà
ldi r18,1
ldi r19,13
call T6963C_TextGoTo
; Âûâåñòè ÷èñëî íà äèñïëåé
ldi XL,low(STRING)
ldi XH,high(STRING)
call T6963C_WriteString
;.......................................................
ldi r20,104
ldi r21,96
ldi r22,16
ldi r23,31
call T6963C_ShiftLeftDataBlock
; ïîñòðîèòü ëèíèþ
lds r20,ADC_CH1+0
lds r21,ADC_CH1+1
; ïîäåëèòü ðåçóëüòàò íà 128
add r20,r20
mov r20,r21
adc r20,r20
sbc r21,r21
neg r21
ldi r18,231 ; x
ldi r19,127
sub r19,r20 ; y
call T6963C_VertLine ; (IN: r18 - x, r19 - y, r20 - height)
;------------------------------------------------------
; Âûâåñòè çíà÷åíèå ìîùíîñòè
;------------------------------------------------------
call Calculate_power
; convert digit to string
lds XH,POWER_MW+1
lds XL,POWER_MW+0
ldi YL,low(STRING)
ldi YH,high(STRING)
call ITOA_FAST_DIV
; îòôîðìàòèðîâàòü ÷èñëî
; IN: STRING
rcall format_number
; Óñòàíîâèòü êîîðäèíàòû âûâîäà
ldi r18,18
ldi r19,2
call T6963C_TextGoTo
; Âûâåñòè ÷èñëî íà äèñïëåé
ldi XL,low(STRING)
ldi XH,high(STRING)
call T6963C_WriteString
;------------------------------------------------------
; Âûâåñòè çíà÷åíèå ñîïðîòèâëåíèÿ
;------------------------------------------------------
call Calculate_resistance
; convert digit to string
ldi YL,low(STRING)
ldi YH,high(STRING)
call RES_TO_STR
; Óñòàíîâèòü êîîðäèíàòû âûâîäà
ldi r18,18
ldi r19,3
call T6963C_TextGoTo
; Âûâåñòè ÷èñëî íà äèñïëåé
ldi XL,low(STRING)
ldi XH,high(STRING)
call T6963C_WriteString
;sei
ret
;------------------------------------------------------------------------------
; IN: STRING
;------------------------------------------------------------------------------
format_number:
ret
.include "IVC_calc.asm"
.include "spi_hw.asm"
.include "MCP320x.asm"
.include "MCP492x_2DAC.asm"
.include "eeprom.asm"
.include "math.asm"
.include "simple_menu.asm"
.include "convert.asm"
.include "uart_funcs.asm"
.include "strings.asm"
.include "cmd.asm"
.include "cmd_func.asm"
.include "wait.asm"
.include "t6963c.asm"