31
|
1 |
.nolist
|
|
2 |
.include "m168def.inc" ; Same family as 328P
|
|
3 |
.list
|
|
4 |
|
|
5 |
|
|
6 |
;; Interrupt Vector
|
|
7 |
.org 0x00
|
|
8 |
rjmp init
|
|
9 |
|
33
|
10 |
.org OC1Aaddr
|
|
11 |
; Timer/Counter1 Compare Output A
|
|
12 |
rjmp Timer_OC1A
|
|
13 |
|
|
14 |
.org OC0Aaddr
|
|
15 |
; Timer/Counter0 Compare Output A
|
|
16 |
rjmp Timer_OC0A
|
|
17 |
|
|
18 |
.org SPIaddr
|
|
19 |
rjmp SPI_Interrupt
|
|
20 |
|
|
21 |
.org 0x40
|
|
22 |
|
31
|
23 |
;; Syntax
|
|
24 |
.include "macros.inc"
|
|
25 |
|
|
26 |
;; SPI
|
|
27 |
.include "spi.inc"
|
|
28 |
|
33
|
29 |
;; Matrix
|
|
30 |
.include "matrix.inc"
|
|
31 |
|
|
32 |
;; Timer
|
|
33 |
.set TIMER0_CB_A = Matrix_ScanCol
|
|
34 |
|
|
35 |
.include "timer.inc"
|
|
36 |
|
31
|
37 |
;; Utils
|
|
38 |
.include "delay.inc"
|
|
39 |
|
|
40 |
;; Scan through each pixel
|
33
|
41 |
Main_ScanRaw:
|
31
|
42 |
; init
|
|
43 |
ldi r18, 1
|
|
44 |
ldi r19, 1
|
|
45 |
|
|
46 |
scan_loop:
|
|
47 |
; write out to SPI
|
|
48 |
sts spi_outbuf + 0, r18 ; rows
|
|
49 |
sts spi_outbuf + 1, r19 ; columns
|
|
50 |
|
|
51 |
rcall SPI_SendRecv
|
|
52 |
|
|
53 |
; delay
|
|
54 |
ldi r20, 5 ; ~4M cycles
|
|
55 |
rcall VarDelay
|
|
56 |
|
33
|
57 |
; cols
|
|
58 |
lsl r19 ; next col
|
|
59 |
brcc scan_loop ; refresh if we didn't overflow
|
|
60 |
|
|
61 |
rol r19 ; shift back from C into r21.0
|
|
62 |
|
31
|
63 |
; rows
|
33
|
64 |
lsl r18 ; next row
|
31
|
65 |
brcc scan_loop ; refresh if we didn't overflow
|
|
66 |
|
|
67 |
rol r18 ; shift back from C into r20.0
|
|
68 |
|
|
69 |
; one scan completed
|
|
70 |
ret
|
|
71 |
|
33
|
72 |
;; Scan with test pattern
|
|
73 |
Main_ScanTest:
|
|
74 |
; Generate pattern
|
|
75 |
; end of buffer
|
|
76 |
ldi r17, MATRIX_COLS
|
|
77 |
ldi XL, low(matrix_rowbuf + MATRIX_COLS)
|
|
78 |
ldi XH, high(matrix_rowbuf + MATRIX_COLS)
|
|
79 |
|
|
80 |
; bit pattern
|
|
81 |
ldi r16, 0b11
|
|
82 |
|
|
83 |
st_loop:
|
|
84 |
; put
|
|
85 |
st -X, r16
|
|
86 |
|
|
87 |
; flip
|
|
88 |
rol r16
|
|
89 |
|
|
90 |
; loop until zero
|
|
91 |
dec r17
|
|
92 |
brne st_loop
|
|
93 |
|
|
94 |
st_scan:
|
|
95 |
; Scan repeatedly
|
|
96 |
;rcall Matrix_ScanCol
|
|
97 |
rjmp st_scan
|
|
98 |
|
31
|
99 |
Main:
|
|
100 |
init:
|
|
101 |
; Stack
|
|
102 |
ldi r16, high(RAMEND)
|
|
103 |
ldi r17, low(RAMEND)
|
|
104 |
out SPH, r16
|
|
105 |
out SPL, r17
|
|
106 |
|
|
107 |
; Enable interrupts
|
|
108 |
sei
|
|
109 |
|
33
|
110 |
; DEBUG
|
|
111 |
sbi DDRD, PORTD7
|
|
112 |
cbi PORTD, PORTD7
|
|
113 |
|
|
114 |
; Timer
|
|
115 |
rcall Timer_Init
|
|
116 |
|
31
|
117 |
; SPI
|
|
118 |
rcall SPI_Init
|
33
|
119 |
|
|
120 |
; Matrix
|
|
121 |
rcall Matrix_Init
|
31
|
122 |
|
|
123 |
; Run
|
33
|
124 |
; rcall Main_ScanRaw
|
31
|
125 |
|
33
|
126 |
rcall Main_ScanTest
|
31
|
127 |
|
|
128 |
end:
|
|
129 |
rjmp end
|
|
130 |
|