author | Tero Marttila <terom@fixme.fi> |
Sat, 08 May 2010 21:27:44 +0300 | |
changeset 28 | 51344df466ca |
parent 21 | 95549ce0e3da |
child 29 | 453550e69e07 |
permissions | -rw-r--r-- |
18 | 1 |
;; vim: filetype=avr |
2 |
;; |
|
3 |
;; Timer unit control and use |
|
4 |
;; |
|
5 |
||
6 |
; Waveform Generation Mode (nibble low/high) |
|
7 |
.set TIMER_WGML = 0b00 |
|
8 |
.set TIMER_WGMH = 0b01 |
|
9 |
||
10 |
; Clock Source |
|
11 |
.set TIMER_CS = 0b101 |
|
12 |
||
21
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
13 |
.equ TIMER_FLAGS = GPIOR0 |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
14 |
.equ TIMER_BUSY = 1 |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
15 |
|
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
16 |
.set SLEEP_MODE = 0b000 ; Idle |
18 | 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)] |
|
21
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
29 |
|
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
30 |
Sleep_init: |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
31 |
; Select sleep mode |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
32 |
; Enable `sleep` |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
33 |
poke [SMCR, r16, (SLEEP_MODE << SM0) | (1 << SE)] |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
34 |
|
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
35 |
; Disable ADC |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
36 |
poke [SMCR, r16, (1 << PRTWI) | (1 << PRUSART0) | (1 << PRADC)] |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
37 |
|
18 | 38 |
ret |
39 |
||
40 |
Timer_Start: |
|
41 |
; Initialize timer |
|
42 |
poke [TCNT1H, r16, high(0)] |
|
43 |
poke [TCNT1L, r16, low(0)] |
|
44 |
||
45 |
; Set flag |
|
46 |
sbi TIMER_FLAGS, TIMER_BUSY |
|
47 |
||
48 |
; WGM |
|
49 |
; Clock Source |
|
50 |
poke [TCCR1B, r16, (TIMER_WGMH << WGM12) | (TIMER_CS << CS10)] |
|
51 |
||
52 |
ret |
|
53 |
||
54 |
Timer_Stop: |
|
55 |
; WGM |
|
56 |
; Clock off |
|
57 |
poke [TCCR1B, r16, (TIMER_WGMH << WGM12) | (0b00 << CS10)] |
|
58 |
||
59 |
; Clear flag |
|
60 |
cbi TIMER_FLAGS, TIMER_BUSY |
|
61 |
||
62 |
ret |
|
63 |
||
64 |
;; Timer Compare 1A interrupt handler |
|
65 |
Timer_OC1A: |
|
66 |
in r0, SREG |
|
67 |
||
68 |
; Stop timer |
|
69 |
rcall Timer_Stop |
|
70 |
||
71 |
out SREG, r0 |
|
72 |
reti |
|
73 |
||
74 |
;; Count to X |
|
75 |
Timer_Sleep: |
|
76 |
; Set TOP |
|
77 |
sts OCR1AH, XH |
|
78 |
sts OCR1AL, XL |
|
79 |
||
80 |
; Start timer |
|
81 |
rcall Timer_Start |
|
82 |
||
83 |
; Wait for timer to complete |
|
84 |
_timer_sleep: |
|
21
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
85 |
sleep |
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
86 |
|
18 | 87 |
sbic TIMER_FLAGS, TIMER_BUSY |
88 |
rjmp _timer_sleep |
|
21
95549ce0e3da
timer: try and actually sleep
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
89 |
|
18 | 90 |
; Done |
91 |
ret |
|
92 |
||
93 |
;; Prime the timer and sleep for 1s |
|
94 |
Timer_Sleep_1s: |
|
95 |
; Initialize counter to 16k cycles |
|
96 |
ldi XH, high(16 * 1024) |
|
97 |
ldi XL, low(16 * 1024) |
|
98 |
||
99 |
; Start timer |
|
100 |
rjmp Timer_Sleep |
|
101 |