basic implementation done ...
[my-code/atmel.git] / led_plex / main.asm
1 ; main file of led_plex project
2 ;
3 ; schematics: http://hackdaworld.org/download/led_plex/led_plex.png
4 ;
5 ; author: hackbard@hackdaworld.org
6 ;
7
8 ; device specific definition file
9 .include "../include/tn2313def.inc"
10
11 ; defines
12 .def    zero            = r1
13 .def    one             = r2
14 .def    tmp1            = r16
15 .def    tmp2            = r17
16 .def    uart_rxtx       = r18
17 .def    hc595_sink      = r19
18 .def    hc595_source    = r20
19 .def    column          = r21
20 .def    count           = r22
21
22 ;
23 ; interrupts
24 ;
25
26 ; RESET
27 rjmp INIT
28
29 ; INT0
30 reti
31
32 ; INT1
33 reti
34
35 ; T1 CAPT1
36 reti
37
38 ; T1 COMP A
39 reti
40
41 ; T1 OVF1
42 rjmp T1_OVF
43
44 ; T0 OVF0
45 rjmp T0_OVF
46
47 ; UART RX
48 rjmp UART_RECEIVE
49
50 ; UART UDRE
51 reti
52
53 ; UART TX
54 reti
55
56 ; ANA COMP
57 reti
58
59 ; PCINT
60 reti
61
62 ; T1 COMP B
63 reti
64
65 ; T0 COMP A
66 reti
67
68 ; T0 COMP B
69 reti
70
71 ; USI START
72 reti
73
74 ; USI OVF
75 reti
76
77 ; EE READY
78 reti
79
80 ; WDT OVF
81 reti
82
83 ;
84 ; init and main code
85 ;
86
87 RESET:
88 INIT:
89
90         ; gio port init
91         rcall PORT_INIT
92
93         ; timer0 init
94         rcall TIMER0_INIT
95
96         ; timer0 interrupt enable
97         rcall TIMER0_INT_INIT
98
99         ; timer1 init
100         rcall TIMER1_INIT
101
102         ; timer1 interrupt enable
103         rcall TIMER1_INT_INIT
104
105         ; uart init
106         rcall UART_INIT
107
108         ; uart interrupt enable
109         rcall UART_INT_RX_INIT
110
111         ; zero and one initialization
112         ldi tmp1,0
113         mov zero,tmp1
114         ldi tmp1,1
115         mov one,tmp1
116
117         ; set stackpointer
118         ldi tmp1,low(RAMEND)
119         out SPL,tmp1
120
121         ; framebuffer init: Z :)
122         ldi ZL,low(FB_C)
123         ldi ZH,high(FB_C)
124         ldi tmp1,0xff
125         st Z+,tmp1
126         ldi tmp1,0xc3
127         st Z+,tmp1
128         ldi tmp1,0xa5
129         st Z+,tmp1
130         ldi tmp1,0x99
131         st Z+,tmp1
132         ldi tmp1,0x99
133         st Z+,tmp1
134         ldi tmp1,0xa5
135         st Z+,tmp1
136         ldi tmp1,0xc3
137         st Z+,tmp1
138         ldi tmp1,0xff
139         st Z,tmp1
140
141         ; led init
142         ldi hc595_sink,0x00
143         ldi hc595_source,0x01
144         ldi column,0
145         rcall SET_HC595
146
147         ; more init
148         ldi count,0x21
149
150         ; signal ready output
151         ldi uart_rxtx,0x68
152         rcall UART_TX
153
154         ; global interrupt enable
155         sei 
156
157 MAIN:
158
159         ; loop forever
160         rjmp MAIN
161
162
163 ; include subroutines
164 .include "port.asm"
165 .include "timer.asm"
166 .include "uart.asm"
167
168
169 ;
170 ; interrupt routines
171 ;
172
173 T1_OVF:
174
175         ;
176         ; write font into framebuffer sram
177         ;
178
179 .ifdef DEBUG_PORTS
180         ldi uart_rxtx,0x21
181         rcall UART_TX
182 .endif
183         ; calculate address
184
185         ; font base address
186         ldi ZL,low(FONTS*2)
187         ldi ZH,high(FONTS*2)
188 .ifdef DEBUG_PORTS
189         mov uart_rxtx,ZH
190         rcall UART_TX
191         mov uart_rxtx,ZL
192         rcall UART_TX
193         ldi uart_rxtx,0x2d
194         rcall UART_TX
195 .endif
196
197         ; framebuffer base address
198         ldi YL,low(FB_C)
199         ldi YH,high(FB_C)
200 .ifdef DEBUG_PORTS
201         mov uart_rxtx,YH
202         rcall UART_TX
203         mov uart_rxtx,YL
204         rcall UART_TX
205         ldi uart_rxtx,0x2d
206         rcall UART_TX
207 .endif
208
209         ; font offset
210         mov XL,count
211         ldi XH,0
212         ; times 8 == 3 left shifts 
213         rol XL
214         rol XH
215         rol XL
216         rol XH
217         rol XL
218         rol XH
219 .ifdef DEBUG_PORTS
220         mov uart_rxtx,XH
221         rcall UART_TX
222         mov uart_rxtx,XL
223         rcall UART_TX
224         ldi uart_rxtx,0x2d
225         rcall UART_TX
226 .endif
227
228         ; final font address
229         add ZL,XL
230         adc ZH,zero
231         add ZH,XH
232 .ifdef DEBUG_PORTS
233         mov uart_rxtx,ZH
234         rcall UART_TX
235         mov uart_rxtx,ZL
236         rcall UART_TX
237         ldi uart_rxtx,0x2d
238         rcall UART_TX
239         mov uart_rxtx,count
240         rcall UART_TX
241 .endif
242
243         ; move font data into framebuffer
244         lpm tmp1,Z+
245         st Y+,tmp1
246         lpm tmp1,Z+
247         st Y+,tmp1
248         lpm tmp1,Z+
249         st Y+,tmp1
250         lpm tmp1,Z+
251         st Y+,tmp1
252         lpm tmp1,Z+
253         st Y+,tmp1
254         lpm tmp1,Z+
255         st Y+,tmp1
256         lpm tmp1,Z+
257         st Y+,tmp1
258         lpm tmp1,Z
259         st Y,tmp1
260
261         ; TESTING
262         ; increment test counter
263         inc count
264         cpi count,0x80
265         brne EXIT_T1_OVF
266         ldi count,0x21
267
268 EXIT_T1_OVF:
269         
270         reti
271
272 T0_OVF:
273
274         ; debug output
275         cbi PORTD,3
276
277         ; shift source
278         rol hc595_source
279         inc column
280         cpi column,8
281         brne SINK_UPDATE
282         ldi column,0
283         ldi hc595_source,1
284
285         ; update sink
286 SINK_UPDATE:
287
288         ldi ZL,low(FB_C)
289         ldi ZH,high(FB_C)
290         add ZL,column
291         adc ZH,zero
292         ld hc595_sink,Z
293
294 .ifdef DEBUG_PORTS
295         ldi uart_rxtx,0x2d
296         rcall UART_TX
297         ldi uart_rxtx,0x3e
298         rcall UART_TX
299         ldi uart_rxtx,0x30
300         add uart_rxtx,column
301         rcall UART_TX
302         ldi uart_rxtx,0x3a
303         rcall UART_TX
304         ldi uart_rxtx,0x20
305         rcall UART_TX
306 .endif
307
308         ; set hc595
309         rcall SET_HC595
310
311 .ifdef DEBUG_PORTS
312         ldi uart_rxtx,0x0a
313         rcall UART_TX
314         ldi uart_rxtx,0x0d
315         rcall UART_TX
316 .endif
317
318         ; debug output
319         sbi PORTD,3
320         
321         reti
322
323 UART_RECEIVE:
324         reti
325
326 ; include fonts 
327 FONTS:
328 .include "fonts.asm"
329
330 ;
331 ; sram
332 ;
333
334 .dseg
335
336 ; current framebuffer content
337 FB_C: .byte 8
338
339