-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
114 lines (86 loc) · 2 KB
/
timer.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "timer.h"
#include "common.h"
#include "beep.h"
volatile uint16_t timers[TIMER_COUNT];
volatile uint32_t timer_seconds_total = 0;
volatile uint32_t timer_mseconds_total = 0;
volatile uint16_t timer_seconds_timeout = TIMER_TICS_PER_SECOND;
void do_timer(void);
void timer_init(void)
{
uint8_t idx;
for (idx = 0; idx < TIMER_COUNT; idx++)
timers[idx] = 0xFFFF;
SETBIT(TIMSK, OCIE0);
OCR0 = 115; // 1 ms @ 14.7456MHz
TCCR0 = _BV(CS00) | _BV(CS02) | _BV(WGM01); // CTC mode, 128 prescaler
}
ISR(TIMER0_COMP_vect)
//SIGNAL(SIG_OUTPUT_COMPARE0)
{
uint8_t idx;
for (idx = 0; idx < TIMER_COUNT; idx++)
if ((0 < timers[idx]) && (timers[idx] < 0xFFFF))
timers[idx]--;
#if defined _BEEP_INCLUDED
if (1 == timers[1])
BEEP_OFF;
#endif /* _BEEP_INCLUDED */
do_timer();
if (!timer_seconds_timeout--)
{
timer_seconds_total++;
timer_seconds_timeout = TIMER_TICS_PER_SECOND;
}
timer_mseconds_total++;
}
uint16_t timer_value(uint8_t timer_id)
{
uint16_t value;
uint8_t sreg;
sreg = SREG;
GLOBAL_INT_DISABLE;
value = (timer_id < TIMER_COUNT)?timers[timer_id]:0;
SREG = sreg;
return value;
}
uint8_t start_timer(uint16_t delay)
{
uint8_t timer_id;
uint8_t sreg;
sreg = SREG;
GLOBAL_INT_DISABLE;
for (timer_id = 2; timer_id < TIMER_COUNT; timer_id++)
if (0xFFFF == timers[timer_id])
{
timers[timer_id] = (delay < 0xFFFF)?delay:delay-1;
break;
}
SREG = sreg;
return timer_id;
}
void timer_based_delay_ms(uint16_t delay)
{
uint8_t sreg;
sreg = SREG;
GLOBAL_INT_DISABLE;
timers[0] = (delay < 0xFFFF) ? delay : delay - 1;
SREG = sreg;
while (timer_value(0));
}
void delay_ms(uint16_t delay)
{
while (delay--)
{
_delay_ms(1);
}
}
void stop_timer(uint8_t timer_id)
{
uint8_t sreg;
sreg = SREG;
GLOBAL_INT_DISABLE;
if ((1 < timer_id) && (timer_id < TIMER_COUNT))
timers[timer_id] = 0xFFFF;
SREG = sreg;
}