-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.cpp
95 lines (78 loc) · 1.62 KB
/
timer.cpp
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
/*
timer.cpp
timer class
Stefan Seegel, [email protected]
feb 2011
http://opensource.org/licenses/gpl-license.php GNU Public License
*/
#include "timer.h"
#include <avr/interrupt.h>
volatile static bool temp_idle = true;
volatile bool timers_idle = true;
static struct timer_struct_t *timer_vector = 0;
Timer::Timer(void)
{
item = new(struct timer_struct_t);
item->flag = 0;
item->cnt = 0;
if (timer_vector == 0)
{//this is the first timer object, init hardware timer
//Init timer for timerobjects, timer IRQ has to be called once per ms
TCCR0A = 1<<WGM01; //CTC mode
#if (F_CPU / 256 > 1000)
#if (F_CPU / 256 / 8 > 1000)
#if (F_CPU / 256 / 64 > 1000)
TCCR0B = 1<<CS02;//clk / 256
OCR0A = F_CPU / 1000 / 256 - 1;
#else
TCCR0B = 1<<CS01 | 1<<CS00; //clk / 64
OCR0A = F_CPU / 1000 / 64 - 1;
#endif
#else
TCCR0B = 1<<CS01; //clk / 8
OCR0A = F_CPU / 1000 / 8 - 1;
#endif
#else
TCCR0B = 1<<CS00; //clk / 1
OCR0A = F_CPU / 1000 - 1;
#endif
TIMSK0 = 1<<OCIE0A;
}
//put in list
item->next = timer_vector;
timer_vector = item;
}
bool Timer::IsFlagged(void)
{
if (item->flag)
{
item->flag = false;
return true;
}
return false;
}
void Timer::SetTime(uint16_t time)
{
TIMSK0 &= ~(1<<OCIE0A);
item->cnt = time;
if (time)
timers_idle = false;
item->flag = 0;
TIMSK0 |= 1<<OCIE0A;
}
ISR(SIG_OUTPUT_COMPARE0A)
{
temp_idle = true;
struct timer_struct_t *item = timer_vector;
while (item)
{
if (item->cnt)
{
temp_idle = false;
if (--item->cnt == 0)
item->flag = true;
}
item = item->next;
}
timers_idle = temp_idle;
}