18
|
1 |
;; vim: filetype=avr
|
|
2 |
;;
|
|
3 |
;; Timer unit control and use
|
|
4 |
;;
|
|
5 |
|
|
6 |
.include "macros.inc"
|
|
7 |
|
|
8 |
; Waveform Generation Mode (nibble low/high)
|
|
9 |
.set TIMER_WGML = 0b00
|
|
10 |
.set TIMER_WGMH = 0b01
|
|
11 |
|
|
12 |
; Clock Source
|
|
13 |
.set TIMER_CS = 0b101
|
|
14 |
|
|
15 |
.equ TIMER_FLAGS = GPIOR0
|
|
16 |
.equ TIMER_BUSY = 1
|
|
17 |
|
|
18 |
Timer_Init:
|
|
19 |
; OC1A/B disconnected from output
|
|
20 |
; No PWM mode
|
|
21 |
poke [TCCR1A, r16, (0b00 << COM1A0) | (0b00 << COM1B0) | (TIMER_WGML << WGM10)]
|
|
22 |
|
|
23 |
; Clear
|
|
24 |
poke [TCCR1B, r16, 0]
|
|
25 |
poke [TCCR1C, r16, 0]
|
|
26 |
|
|
27 |
; Enable timer overflow interrupt
|
|
28 |
poke [TIMSK1, r16, (1 << OCIE1A)]
|
|
29 |
|
|
30 |
ret
|
|
31 |
|
|
32 |
Timer_Start:
|
|
33 |
; Initialize timer
|
|
34 |
poke [TCNT1H, r16, high(0)]
|
|
35 |
poke [TCNT1L, r16, low(0)]
|
|
36 |
|
|
37 |
; Set flag
|
|
38 |
sbi TIMER_FLAGS, TIMER_BUSY
|
|
39 |
|
|
40 |
; WGM
|
|
41 |
; Clock Source
|
|
42 |
poke [TCCR1B, r16, (TIMER_WGMH << WGM12) | (TIMER_CS << CS10)]
|
|
43 |
|
|
44 |
ret
|
|
45 |
|
|
46 |
Timer_Stop:
|
|
47 |
; WGM
|
|
48 |
; Clock off
|
|
49 |
poke [TCCR1B, r16, (TIMER_WGMH << WGM12) | (0b00 << CS10)]
|
|
50 |
|
|
51 |
; Clear flag
|
|
52 |
cbi TIMER_FLAGS, TIMER_BUSY
|
|
53 |
|
|
54 |
ret
|
|
55 |
|
|
56 |
;; Timer Compare 1A interrupt handler
|
|
57 |
Timer_OC1A:
|
|
58 |
in r0, SREG
|
|
59 |
|
|
60 |
; Stop timer
|
|
61 |
rcall Timer_Stop
|
|
62 |
|
|
63 |
out SREG, r0
|
|
64 |
reti
|
|
65 |
|
|
66 |
;; Count to X
|
|
67 |
Timer_Sleep:
|
|
68 |
; Set TOP
|
|
69 |
sts OCR1AH, XH
|
|
70 |
sts OCR1AL, XL
|
|
71 |
|
|
72 |
; Start timer
|
|
73 |
rcall Timer_Start
|
|
74 |
|
|
75 |
; Wait for timer to complete
|
|
76 |
_timer_sleep:
|
|
77 |
;sleep
|
|
78 |
|
|
79 |
sbic TIMER_FLAGS, TIMER_BUSY
|
|
80 |
rjmp _timer_sleep
|
|
81 |
|
|
82 |
; Done
|
|
83 |
ret
|
|
84 |
|
|
85 |
;; Prime the timer and sleep for 1s
|
|
86 |
Timer_Sleep_1s:
|
|
87 |
; Initialize counter to 16k cycles
|
|
88 |
ldi XH, high(16 * 1024)
|
|
89 |
ldi XL, low(16 * 1024)
|
|
90 |
|
|
91 |
; Start timer
|
|
92 |
rjmp Timer_Sleep
|
|
93 |
|