forked from codyps/libmuc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heart_led.c
99 lines (80 loc) · 1.83 KB
/
heart_led.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <avr/io.h>
/* PWM of LED */
static void timer0_init(void)
{
/* OC0A clear on compare match, set at bottom
* OC0B disconected
*/
TCCR0A = (1 << COM0A1) | (0 << COM0A0)
|(0 << COM0B1) | (0 << COM0B0)
}
/* Timer */
static void timer2_init(void)
{
power_timer2_enable();
/* Stop timer */
TCCR2B = 0;
OCR2A = 250;
OCR2B = 0;
TCNT2 = 0;
/* Enable OC2A (top) interrupt for timer generation */
TIMSK2 = (1 << OCIE2A);
/* OC2A disconected
* OC2B clear on compare match, set at bottom
* Fast PWM (top = OCRA)
*/
TCCR2A = (0 << COM2A1) | (0 << COM2A0)
|(1 << COM0B1) | (0 << COM0B0)
|(1 << WGM21 ) | (1 << WGM20 );
/* CS22 CS21 CS20 Description
* 0 0 0 No clock source (Timer/Counter stopped).
* 0 0 1 clkT2S/1 (No prescaling)
* 0 1 0 clkT2S/8 (From prescaler)
* 0 1 1 clkT2S/32 (From prescaler)
* 1 0 0 clkT2S/64 (From prescaler)
* 1 0 1 clkT2S/128 (From prescaler)
* 1 1 0 clkT2S/256 (From prescaler)
* 1 1 1 clkT2S/1024 (From prescaler)
*/
/* Fast PWM (top = OC2A)
* Clock selected; ps = 64
*/
#define T2_CSPS_8 ((0 << CS22) | (1 << CS21) | (1 << CS20))
#define T2_CSPS_64 ((1 << CS22) | (0 << CS21) | (0 << CS20))
TCCR2B = (1 << WGM22)
| T2_CSPS_64;
}
static uint32_t ms_counter;
ISR(TIMER2_COMPA_vect)
{
/* ms counter */
ms_counter ++;
}
#define timer2_compa_isr_lock() do { \
TIMSK2 &= ~(1 << OCIE2A); \
barrier(); \
} while(0)
#define timer2_compa_isr_unlock() do { \
barrier(); \
TIMSK2 |= (1 << OCIE2A); \
} while(0)
uint32_t time_ms(void) {
uint32_t s;
timer2_compa_isr_lock();
s = ms_counter;
timer2_compa_isr_unlock();
return s;
}
void heart_init(void)
{
timer2_init();
}
void heart_spin(void)
{
}
void heart_set_speed(uint8_t sp)
{
}
void heart_set_brightness(uint8_t br)
{
}