3cdbda3afb07b73f6d73c67c123517e7cbcae2cd
[my-code/atmel.git] / beginners / rgb_mali.asm
1 ; rgb_mali.asm
2 ;
3 ; 3 leds (red, green, blue) on different ports.
4 ; cm mode: continuous mixed colors
5 ; rgb mode: keep one special color
6 ;
7 ; modes switchable by uart
8 ;
9 ; author: hackbard@hackdaworld.dyndns.org
10
11
12 .include "../include/2313def.inc"
13
14 ; defines & equals
15 .def zero = r0
16 .def one = r1
17 .def countrgb = r2
18 .def desc = r3
19 .equ RUP = 0
20 .equ RDOWN = 1
21 .equ GUP = 2
22 .equ GDOWN = 3
23 .equ BUP = 4
24 .equ BDOWN = 5
25 .def tmp = r16
26 .def uart_count = r17
27 .def count = r18
28 .def RED = r19
29 .def GREEN = r20
30 .def BLUE = r21
31 .def mode = r22
32 .equ cm = 0
33 .equ rgb = 1
34 .def OUTR = r23
35 .def FULLR = r24
36 .def tmp2 = r25
37 .equ LED_R = PD2
38 .equ LED_G = PD3
39 .equ LED_B = PD4
40 .equ LED_PORT = PORTD
41 .equ CM1 = 85
42 .equ CM2 = 170
43
44 ;
45 ; interrupt voctors
46 ;
47 ; reset
48 rjmp INIT
49 ; int0
50 reti
51 ; int1
52 reti
53 ; timer/counter capt 1
54 reti
55 ; timer/counter compare
56 reti
57 ; timer/counter overflow 1
58 rjmp DO_IT
59 ; timer/counter overflow 0
60 reti
61 ; uart rx complete
62 rjmp UART_RECEIVE
63 ; uart data register empty
64 reti
65 ; uart tx complete
66 reti
67 ; analog comparator
68 reti
69
70 ;
71 ; init routine
72 ;
73 INIT:
74 ; alloc stack pointer
75 ldi r16,low(RAMEND)
76 out SPL,r16
77 ; timer: clock/8
78 ldi tmp,((1<<CS11))
79 out TCCR1B,tmp
80 ; enable timer overflow interrupt
81 ldi tmp,(1<<TOIE1)
82 out TIMSK,tmp
83 ; enable uart + rx complete interrupt
84 ldi tmp,51 ; 9600 baud, 0,2% error @ 8mhz
85 out UBRR,tmp
86 ldi tmp,((1<<RXCIE)|(1<<RXEN))
87 out UCR,tmp
88 ; init registers
89 ldi tmp,1
90 mov one,tmp
91 ldi tmp,0
92 mov zero,tmp
93 mov countrgb,tmp
94 ldi uart_count,0
95 ldi count,0
96 ldi RED,CM2
97 ldi GREEN,0x0
98 ldi BLUE,0x0
99 ; led pins are outputs
100 ldi tmp,((1<<LED_R)|(1<<LED_G)|(1<<LED_B))
101 out DDRD,tmp
102 ; default mode: cm
103 ldi mode,cm
104 ; enable interrupts (global)
105 sei
106 ; jump to main
107 rjmp MAIN
108
109 ;
110 ; decide what to do
111 ;
112 DO_IT:
113 cpi mode,cm
114 breq CM_ACTION
115 reti
116
117 ;
118 ; continuous mode action
119 ;
120 CM_ACTION:
121 cp countrgb,zero
122 breq RD_GU
123 ldi tmp,CM1
124 cp countrgb,tmp
125 breq GD_BU
126 ldi tmp,CM2
127 cp countrgb,tmp
128 breq BD_RU
129 ldi tmp,255
130 cp countrgb,tmp
131 breq RESET_RGB
132 rjmp MAKE_RGB
133
134 ;
135 ; reset rgb
136 ;
137 RESET_RGB:
138 ldi RED,CM2
139 mov GREEN,zero
140 mov BLUE,zero
141 mov desc,zero
142 rjmp MAKE_RGB
143
144 ;
145 ; set desc
146 ;
147 RD_GU:
148 ldi tmp,((1<<RDOWN)|(1<<GUP))
149 mov desc,tmp
150 rjmp MAKE_RGB
151 GD_BU:
152 ldi tmp,((1<<GDOWN)|(1<<BUP))
153 mov desc,tmp
154 rjmp MAKE_RGB
155 BD_RU:
156 ldi tmp,((1<<BDOWN)|(1<<RUP))
157 mov desc,tmp
158 rjmp MAKE_RGB
159
160 ;
161 ; do the actual rgb calculation
162 ;
163 MAKE_RGB:
164 ldi tmp,(1<<RUP)
165 and tmp,desc
166 add tmp,tmp
167 add RED,tmp
168 ldi tmp,(1<<GUP)
169 and tmp,desc
170 lsr tmp
171 lsr tmp
172 add tmp,tmp
173 add GREEN,tmp
174 ldi tmp,(1<<BUP)
175 and tmp,desc
176 lsr tmp
177 lsr tmp
178 lsr tmp
179 lsr tmp
180 add tmp,tmp
181 add BLUE,tmp
182 ldi tmp,(1<<RDOWN)
183 and tmp,desc
184 lsr tmp
185 add tmp,tmp
186 sub RED,tmp
187 ldi tmp,(1<<GDOWN)
188 and tmp,desc
189 lsr tmp
190 lsr tmp
191 lsr tmp
192 add tmp,tmp
193 sub GREEN,tmp
194 ldi tmp,(1<<BDOWN)
195 and tmp,desc
196 lsr tmp
197 lsr tmp
198 lsr tmp
199 lsr tmp
200 lsr tmp
201 add tmp,tmp
202 sub BLUE,tmp
203 add countrgb,one
204 reti
205
206 ;
207 ; main routine
208 ;
209 MAIN:
210 ; jump to main loop
211 rjmp LOOP
212
213 :
214 ; main loop
215 ;
216 LOOP:
217 ; reset lights if loop starts 
218 cpi count,0
219 breq RESET_LEDS
220 ; now check leds
221 rjmp CHECK_RED
222
223 ;
224 ; reset leds
225 ;
226 RESET_LEDS:
227 ldi OUTR,((1<<LED_R)|(1<<LED_G)|(1<<LED_B))
228 rjmp CHECK_RED
229
230 ;
231 ; check red + jump to green check
232 ;
233 CHECK_RED:
234 cp count,RED
235 breq SWITCH_OFF_R
236 rjmp CHECK_GREEN
237
238 ;
239 ; switch of red led
240 ;
241 SWITCH_OFF_R:
242 subi OUTR,(1<<LED_R)
243 rjmp CHECK_GREEN
244
245 ;
246 ; check green + jump to blue check
247 ;
248 CHECK_GREEN:
249 cp count,GREEN
250 breq SWITCH_OFF_G
251 rjmp CHECK_BLUE
252
253 ;
254 ; switch of green led
255 ;
256 SWITCH_OFF_G:
257 subi OUTR,(1<<LED_G)
258 rjmp CHECK_BLUE
259
260 ;
261 ; check blue + "do the light"
262 ;
263 CHECK_BLUE:
264 cp count,BLUE
265 breq SWITCH_OFF_B
266 rjmp LIGHT
267
268 ;
269 ; switch of blue led
270 ;
271 SWITCH_OFF_B:
272 subi OUTR,(1<<LED_B)
273 rjmp LIGHT
274
275 ;
276 ; light it up
277 ;
278 LIGHT:
279 ldi FULLR,0xff
280 eor FULLR,OUTR
281 out LED_PORT,FULLR
282 rjmp INC_COUNT
283
284 ;
285 ; increase counter + jump back to main loop
286 ;
287 INC_COUNT:
288 add count,one
289 rjmp LOOP
290
291 ;
292 ; receive from uart
293 ;
294 UART_RECEIVE:
295 reti
296