-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.asm
281 lines (246 loc) · 5.3 KB
/
main.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
;-------------------------------------------------------------------------
; Acrylic Sign
;
; Copylight (C) 2020 Minagi Yu
;-------------------------------------------------------------------------
; PB0: In, Switch
; PB1: In, Volume
; PB2: Out, WS2812B data pin
; PB3: In, Reset
.include "tn10def.inc"
.def temp = r16
.def zero = r17
.def YL_ = r18
.def rlvl = r19
.def glvl = r20
.def blvl = r21
.def tcchclr = r22 ; Timer counter for change color
.def rvchclr = r23 ; Reload value for change color timer
.def arg = r24
.def stopflag = r25
.equ SW_PORT = PORTB
.equ SW_PUE = PUEB
.equ SW_BIT = 0
.equ SW_DDR = DDRB
.equ VR_PORT = PORTB
.equ VR_BIT = 1
.equ VR_DDR = DDRB
.equ LED_PORT = PORTB
.equ LED_BIT = 2
.equ LED_DDR = DDRB
.equ FIFO_SIZE = 16
;-------------------------------------------------------------------------
; Data Section
.dseg
.org SRAM_START
; FIFO for ADC result
fifo:
.byte FIFO_SIZE
;-------------------------------------------------------------------------
; Code Section
.cseg
.org 0
;
; Interrupt Vectors
;
vectors:
vector_reset: ; Reset Handler
rjmp reset
vector_int0: ; IRQ0 Handler
; rjmp isr_int0
rjmp reset
vector_pcint0: ; PCINT0 Handler
; rjmp isr_pcint0
rjmp reset
vector_tim0_capt: ; Timer0 Capture Handler
; rjmp isr_tim0_capt
rjmp reset
vector_tim0_ovf: ; Timer0 Overflow Handler
; rjmp isr_tim0_ovf
rjmp reset
vector_tim0_compa: ; Timer0 Compare A Handler
; rjmp isr_tim0_compa
rjmp reset
vector_tim0_compb: ; Timer0 Compare B Handler
; rjmp isr_tim0_compb
rjmp reset
vector_ana_comp: ; Analog Comparator Handler
; rjmp isr_ana_comp
rjmp reset
vector_wdt: ; Watchdog Interrupt Handler
; rjmp isr_wdt
rjmp reset
vector_vlm: ; Voltage Level Monitor Handler
; rjmp isr_vlm
rjmp reset
vector_adc: ; ADC Conversion Handler
; rjmp isr_adc
rjmp reset
;
; Startup routine
;
reset:
clr zero ; Clear zero register
ldi YH, high(SRAM_START) ; Clear RAM
ldi YL, low(SRAM_START) ; |
ldi temp, SRAM_SIZE ; |
st Y+, zero ; |
dec temp ; |
brne PC-2 ; v
; rjmp main ; Go to main
;
; Main routine
;
main:
ldi temp, 0xd8 ; Set F_CPU = 8MHz
out CCP, temp ; |
out CLKPSR, zero ; v
sbi LED_DDR, LED_BIT ; Set pin as output
sbi SW_PUE, SW_BIT ; Enable pull up
sbi ADMUX, 0 ; Input channel ADC1 (PB1)
ldi temp, 0x86 ; ADC enable, 8MHz / 64 = 125kHz
out ADCSRA, temp ; v
sbi DIDR0, 1 ; ADC1 Digital input disable
ldi rvchclr, 127 ;
mov tcchclr, rvchclr ;
ldi rlvl, 255 ;
ldi glvl, 0 ;
ldi blvl, 0 ;
ldi ZH, high(r2y) ;
ldi ZL, low(r2y) ;
ldi YH, high(fifo) ;
ldi YL, low(fifo) ;
clr stopflag ;
set ;
sei ;
loop: ; Main Loop
sbi ADCSRA, ADSC ; Start ADC conversion
sbis ADCSRA, ADIF ; Wait ADC conversion
rjmp PC-1 ; v
sbi ADCSRA, ADIF ; Clear ADC interrupt flag
in temp, ADCL ; Store the result to FIFO
st Y+, temp ; v
mov YL_, YL ; Save YL
ldi YL, low(fifo) ; for (i = 0; i < FIFO_SIZE; i++) {
clr XH ; X += fifo[i];
clr XL ; }
ld temp, Y+ ; |
add XL, temp ; |
adc XH, zero ; |
cpi YL, low(fifo + FIFO_SIZE) ; |
brne PC-4 ; v
.if FIFO_SIZE == 32
swap XH ; X /= 32
swap XL ; |
lsr XH ; |
ror XL ; |
andi XH, 0x78 ; |
andi XL, 0x87 ; |
or XL, XH ; v
.elseif FIFO_SIZE == 16
swap XH ; X /= 16
swap XL ; |
andi XH, 0xf0 ; |
andi XL, 0x0f ; |
or XL, XH ; v
.endif
inc XL ;
mov rvchclr, XL ;
cpi YL_, low(fifo + FIFO_SIZE) ;
brne PC+2 ;
ldi YL_, low(fifo) ;
mov YL, YL_ ; Restore YL
in temp, SW_BIT ; Load switch status
brtc PC+3 ; if ((T == 1) &&
sbrs temp, 0 ; (temp & 1) == 0)
inc stopflag ; stopflag ^= 1;
bst temp, 0 ; T = temp;
sbrc stopflag, 0 ; if (stopflag)
rjmp loop ; goto loop;
dec tcchclr ;
brne loop ;
mov tcchclr, rvchclr ; Reload timer counter value
ijmp ; Jump to (Z)
r2y: ; Red to Yellow
rcall send_led ;
inc glvl ;
cpi glvl, 255 ;
brne PC+3 ;
ldi ZH, high(y2g) ;
ldi ZL, low(y2g) ;
rjmp loop ;
y2g: ; Yellow to Green
rcall send_led ;
dec rlvl ;
brne PC+3 ;
ldi ZH, high(g2c) ;
ldi ZL, low(g2c) ;
rjmp loop ;
g2c: ; Green to Cyan
rcall send_led ;
inc blvl ;
cpi blvl, 255 ;
brne PC+3 ;
ldi ZH, high(c2b) ;
ldi ZL, low(c2b) ;
rjmp loop ;
c2b: ; Cyan to Blue
rcall send_led ;
dec glvl ;
brne PC+3 ;
ldi ZH, high(b2m) ;
ldi ZL, low(b2m) ;
rjmp loop ;
b2m: ; Blue to Magenta
rcall send_led ;
inc rlvl ;
cpi rlvl, 255 ;
brne PC+3 ;
ldi ZH, high(m2r) ;
ldi ZL, low(m2r) ;
rjmp loop ;
m2r: ; Magenta to Red
rcall send_led ;
dec blvl ;
brne PC+3 ;
ldi ZH, high(r2y) ;
ldi ZL, low(r2y) ;
rjmp loop
;
; send_led()
; Parameters:
; r18: red data
; r19: green data
; r20: blue data
; Returns:
; none
;
send_led:
cli ; Disable interrupt
mov arg, glvl ;
rcall ws2812b_send ;
mov arg, rlvl ;
rcall ws2812b_send ;
mov arg, blvl ;
rcall ws2812b_send ;
sei ; Enable interrupt
ret ;
;
; ws2812b_send()
; Parameters:
; r24: data to ws2812b
; Returns:
; none
;
ws2812b_send:
ldi r16, 8 ; Loop counter
sbi LED_PORT, LED_BIT ; 10
sbrs r24, 7 ; 1
cbi LED_PORT, LED_BIT ; 2
nop ; 3
nop ; 4
cbi LED_PORT, LED_BIT ; 5
lsl r24 ; 6
dec r16 ; 7
brne PC-8 ; 9
ret ;