-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.inc
155 lines (121 loc) · 7.62 KB
/
main.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
;
; File : main.inc (part of "Feron Light Advanced Control" project)
;
; Author : Sergey V. DUDANOV
; E-Mail : [email protected]
; Git : https://github.com/dudanov/feron-light-advanced-control
; License: GPL v3 (https://www.gnu.org/licenses/gpl.txt)
_main_loop:
; NEXT RANDOM VALUE
lsr RAND_REG ; [1]
brcc PC+3 ; [1][2] -> 1:
ldi TMP_REG, RAND_MASK ; [1]
eor RAND_REG, TMP_REG ; [1]
; CHECK FOR TIMER EVENT (~0.75 NEC_T)
in TMP_REG, TIM_FLAGS_REG ; [1] <- 1:
sbrs TMP_REG, TIM_FLAG_BIT ; [2][1]
rjmp pcint_event ; [0][2] -> NO OVERFLOW. CHECK FOR PIN CHANGE EVENT.
; TIMER IS OVERFLOWED
out TIM_FLAGS_REG, TMP_REG ; [1] clear all active timer flags
cpi NECCNT_REG, NECCNT_MAX ; [1]
adc NECCNT_REG, ZERO_REG ; [1] if (neccnt < max) neccnt++
dec PWRTMR_REG ; [1]
brne pcint_event ; [1][2] -> POWER IS OK. CHECK FOR PIN CHANGE EVENT.
; AC POWER LOSS. STORE RETAIN DATA TO EEPROM.
.if PD_MASK
out DDRB, ZERO_REG ; [1]
.endif
; *** STORE BH TO EEPROM ***
ldi TMP_REG, ee_bh_current ; [1]
rcall eeprom_write_bh ; [20+]
.if INIT_PWR_STATE > 1
; *** STORE STATUS TO EEPROM ***
push BH_VALUE_REG ; [2]
ldi BH_VALUE_REG, ~_BV(PWR_BIT) ; [1]
or BH_VALUE_REG, STATUS_REG ; [1]
ldi TMP_REG, ee_status ; [1]
rcall eeprom_write_bh ; [20+]
pop BH_VALUE_REG ; [2]
.endif
.if PD_MASK
; Waiting for EEPROM write is completed
sbic EECR, EEPE ; [2][1] <-
rjmp PC-1 ; [0][2] ->
; FAST DISCHARGE POWER CAPACITORS
ldi TMP_REG, PD_MASK ; [1]
out DDRB, TMP_REG ; [1]
.endif
; WAIT UNTIL AC POWER HAS BEEN RESTORED OR RESET BY BOD
sbis PINB, AC_PIN ; [2][1] <-
rjmp PC-1 ; [0][2] ->
pcint_event:
; CHECK FOR PIN CHANGE EVENT
mov PINCHG_REG, PIN_REG ; [1] <-
in PIN_REG, PINB ; [1]
eor PINCHG_REG, PIN_REG ; [1]
; CHECK FOR IR SIGNAL CHANGE
sbrs PINCHG_REG, TSSOP_PIN ; [2][1]
rjmp ac_event ; [0][2] -> NO IR SIGNAL CHANGE. CHECK FOR AC PIN CHANGE
.include "..\nec.inc"
ac_event:
; CHECK FOR AC EVENT (POSITIVE EDGE)
sbrc PINCHG_REG, AC_PIN ; [2][1]
sbrs PIN_REG, AC_PIN ; [2][1]
rjmp _main_loop ; [0][2] -> NO AC EVENT. BACK TO START LOOP
; **** START AC EVENT ****
ldi PWRTMR_REG, MS_T(AC_LOSS_MS) ; [1] reset power timer
dec TICK_REG ; [1]
brne ac_skip_1s ; [1][2] ->
; **** START OF 1S BLOCK ****
ldi TICK_REG, AC_FREQ ; [1]
sbrs STATUS_REG, TMR_BIT ; [2][1]
rjmp ac_skip_1s ; [0][2] -> timer not active
; Timer tick
sbiw TIMLO_REG, 1 ; [2]
brne ac_skip_1s ; [1][2] -> timer not expired
clr STATUS_REG ; [1]
rcall reset_target_registers ; [10]
; **** END OF 1S BLOCK ****
ac_skip_1s:
sbrs TICK_REG, 0 ; [2][1]
rjmp _main_loop ; [0][2] -> main loop
.include "ctrl.inc"
; **** END OF AC EVENT ****
rjmp _main_loop ; [2] -> BACK TO THE START
.include "..\subroutines.inc"
; In last 128 words (256 bytes) of Flash memory allocate Brightness-Hue to OCR value conversion table.
.org FLASHEND - 127
pm_table:
; AVR Assembler doesn't have repeat macro extensinons. So, enter all this terrible table by hands... 8)
.db BH2OCR(0x00),BH2OCR(0x01),BH2OCR(0x02),BH2OCR(0x03),BH2OCR(0x04),BH2OCR(0x05),BH2OCR(0x06),BH2OCR(0x07)
.db BH2OCR(0x08),BH2OCR(0x09),BH2OCR(0x0A),BH2OCR(0x0B),BH2OCR(0x0C),BH2OCR(0x0D),BH2OCR(0x0E),BH2OCR(0x0F)
.db BH2OCR(0x10),BH2OCR(0x11),BH2OCR(0x12),BH2OCR(0x13),BH2OCR(0x14),BH2OCR(0x15),BH2OCR(0x16),BH2OCR(0x17)
.db BH2OCR(0x18),BH2OCR(0x19),BH2OCR(0x1A),BH2OCR(0x1B),BH2OCR(0x1C),BH2OCR(0x1D),BH2OCR(0x1E),BH2OCR(0x1F)
.db BH2OCR(0x20),BH2OCR(0x21),BH2OCR(0x22),BH2OCR(0x23),BH2OCR(0x24),BH2OCR(0x25),BH2OCR(0x26),BH2OCR(0x27)
.db BH2OCR(0x28),BH2OCR(0x29),BH2OCR(0x2A),BH2OCR(0x2B),BH2OCR(0x2C),BH2OCR(0x2D),BH2OCR(0x2E),BH2OCR(0x2F)
.db BH2OCR(0x30),BH2OCR(0x31),BH2OCR(0x32),BH2OCR(0x33),BH2OCR(0x34),BH2OCR(0x35),BH2OCR(0x36),BH2OCR(0x37)
.db BH2OCR(0x38),BH2OCR(0x39),BH2OCR(0x3A),BH2OCR(0x3B),BH2OCR(0x3C),BH2OCR(0x3D),BH2OCR(0x3E),BH2OCR(0x3F)
.db BH2OCR(0x40),BH2OCR(0x41),BH2OCR(0x42),BH2OCR(0x43),BH2OCR(0x44),BH2OCR(0x45),BH2OCR(0x46),BH2OCR(0x47)
.db BH2OCR(0x48),BH2OCR(0x49),BH2OCR(0x4A),BH2OCR(0x4B),BH2OCR(0x4C),BH2OCR(0x4D),BH2OCR(0x4E),BH2OCR(0x4F)
.db BH2OCR(0x50),BH2OCR(0x51),BH2OCR(0x52),BH2OCR(0x53),BH2OCR(0x54),BH2OCR(0x55),BH2OCR(0x56),BH2OCR(0x57)
.db BH2OCR(0x58),BH2OCR(0x59),BH2OCR(0x5A),BH2OCR(0x5B),BH2OCR(0x5C),BH2OCR(0x5D),BH2OCR(0x5E),BH2OCR(0x5F)
.db BH2OCR(0x60),BH2OCR(0x61),BH2OCR(0x62),BH2OCR(0x63),BH2OCR(0x64),BH2OCR(0x65),BH2OCR(0x66),BH2OCR(0x67)
.db BH2OCR(0x68),BH2OCR(0x69),BH2OCR(0x6A),BH2OCR(0x6B),BH2OCR(0x6C),BH2OCR(0x6D),BH2OCR(0x6E),BH2OCR(0x6F)
.db BH2OCR(0x70),BH2OCR(0x71),BH2OCR(0x72),BH2OCR(0x73),BH2OCR(0x74),BH2OCR(0x75),BH2OCR(0x76),BH2OCR(0x77)
.db BH2OCR(0x78),BH2OCR(0x79),BH2OCR(0x7A),BH2OCR(0x7B),BH2OCR(0x7C),BH2OCR(0x7D),BH2OCR(0x7E),BH2OCR(0x7F)
.db BH2OCR(0x80),BH2OCR(0x81),BH2OCR(0x82),BH2OCR(0x83),BH2OCR(0x84),BH2OCR(0x85),BH2OCR(0x86),BH2OCR(0x87)
.db BH2OCR(0x88),BH2OCR(0x89),BH2OCR(0x8A),BH2OCR(0x8B),BH2OCR(0x8C),BH2OCR(0x8D),BH2OCR(0x8E),BH2OCR(0x8F)
.db BH2OCR(0x90),BH2OCR(0x91),BH2OCR(0x92),BH2OCR(0x93),BH2OCR(0x94),BH2OCR(0x95),BH2OCR(0x96),BH2OCR(0x97)
.db BH2OCR(0x98),BH2OCR(0x99),BH2OCR(0x9A),BH2OCR(0x9B),BH2OCR(0x9C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