author | Tero Marttila <terom@paivola.fi> |
Thu, 03 Apr 2014 18:54:50 +0300 | |
changeset 50 | e4ac5a591dcd |
parent 49 | hello.c@f01fb659e54d |
child 52 | 237d1f5c1c32 |
permissions | -rw-r--r-- |
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
#include <avr/io.h> |
49 | 2 |
#include "stdlib.h" |
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
3 |
|
49 | 4 |
#include <avr/interrupt.h> |
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
5 |
|
49 | 6 |
#define TIMER_FLAGS GPIOR0 |
7 |
#define TIMER1_BUSY 1 |
|
8 |
||
9 |
/* |
|
10 |
* Setup timers. |
|
11 |
*/ |
|
12 |
void timer_init (void) |
|
13 |
{ |
|
14 |
TCCR1A = ( |
|
15 |
// OC1A output pin disconnected |
|
16 |
0b00 << COM1A0 |
|
17 |
// OC1B output pin disconnected |
|
18 |
| 0b00 << COM1B0 |
|
19 |
// no PWM |
|
20 |
| 0b00 << WGM10 |
|
21 |
); |
|
22 |
TCCR1B = ( |
|
23 |
// CTC mode |
|
24 |
0b01 << WGM12 |
|
25 |
); |
|
26 |
TCCR1C = 0; |
|
27 |
} |
|
28 |
||
29 |
void timer1_start (short cycles) |
|
30 |
{ |
|
31 |
// count up from zero... |
|
32 |
TCNT1 = 0; |
|
33 |
||
34 |
// ...to the given number of timer cycles |
|
35 |
OCR1A = cycles; |
|
36 |
||
37 |
// start! |
|
38 |
sbi(&TIMER_FLAGS, TIMER1_BUSY); |
|
39 |
TIMSK1 = ( |
|
40 |
// OCA interrupt |
|
41 |
0b1 << OCIE1A // enable |
|
42 |
); |
|
43 |
TCCR1B = ( |
|
44 |
// WGM |
|
45 |
0b01 << WGM12 // CTC |
|
46 |
||
47 |
// clocksource |
|
48 |
| 0b101 << CS10 // 1024'th |
|
49 |
); |
|
50 |
} |
|
51 |
||
52 |
static void timer1_stop () |
|
53 |
{ |
|
54 |
// WGM: normal |
|
55 |
// clocksource: stop |
|
56 |
TCCR1B = 0; |
|
57 |
||
58 |
cbi(&TIMER_FLAGS, TIMER1_BUSY); |
|
59 |
} |
|
60 |
||
61 |
ISR(TIMER1_COMPA_vect) |
|
62 |
{ |
|
63 |
timer1_stop(); |
|
64 |
||
65 |
// XXX: cpu will automatically wake up from sleep() |
|
66 |
} |
|
67 |
||
68 |
/* |
|
69 |
* Sleep on timer1 interrupt. |
|
70 |
*/ |
|
71 |
void timer_sleep (int cycles) |
|
72 |
{ |
|
73 |
// set timer |
|
74 |
timer1_start(cycles); |
|
75 |
||
76 |
// sleep |
|
77 |
// TODO: PRR |
|
78 |
SMCR = ( |
|
79 |
// idle sleep |
|
80 |
(0b00 << SM0) |
|
81 |
||
82 |
// enable sleep |
|
83 |
| (0b1 << SE) |
|
84 |
); |
|
85 |
||
86 |
// sleep |
|
87 |
while (tbi(&TIMER_FLAGS, TIMER1_BUSY)) { |
|
88 |
__asm__ ( "sleep" :: ); |
|
89 |
} |
|
90 |
||
91 |
// cleanup |
|
92 |
SMCR = 0; |
|
93 |
} |
|
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
94 |
|
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
95 |
int main (void) |
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
96 |
{ |
49 | 97 |
timer_init(); |
98 |
||
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
99 |
// LED |
49 | 100 |
sbi(&DDRB, DDB5); |
101 |
||
102 |
sei(); |
|
103 |
||
104 |
// blink |
|
105 |
short timeout = 1000; |
|
106 |
short delta = 10; |
|
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
107 |
|
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
108 |
while (true) { |
49 | 109 |
// bitflip |
110 |
xbi(&PORTB, PORTB5); |
|
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
111 |
|
49 | 112 |
timer_sleep(timeout); |
113 |
||
114 |
timeout += delta; |
|
47
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
115 |
} |
7f930a94ee1e
bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
116 |
} |