-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimer.c
89 lines (70 loc) · 1.75 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
#include "timer.h"
void (*intFunc)(void) = 0;
void (*intFunc2)(void) = 0;
int times = 0;
int times_init = 0;
void timer2_setup()
{
// Disable Timer2 interrupt.
timer2_stop();
// Clock source for timer2 is from internal clock not external (PG 145)
ASSR &= ~(1 << AS2);
// Set Timer2 to CTC mode.
// Clear timer on compare match
TCCR2A = (1 << WGM21);
// Set Timer2 prescaler to 1024 (64uS/count, 64uS - 14400us range).
TCCR2B = (1 << CS22) | (1 << CS21) | (1 << CS20);
// Reset Timer2 counter.
TCNT2 = 0;
}
void timer2_init_us(unsigned long frequency, void (*function)(void))
{
// Configure the timer interrupt.
timer2_setup();
// User's function to call when there's a timer event.
intFunc = function;
// Every count is 64uS, so divide by 64 (bitwise shift right 2) subtract one, then make sure we don't go over 255 limit.
OCR2A = min((frequency >> 6) - 1, 255);
// Enable Timer2 interrupt.
TIMSK2 |= (1 << OCIE2A);
}
void timer2_ms_countdown()
{
if (times == 0)
{
intFunc2();
times = times_init;
}
else
{
times--;
}
}
void timer2_init_ms(unsigned long frequency, void (*function)(void))
{
// Configure the timer interrupt.
timer2_setup();
// How often we should call the function
times = times_init = frequency;
// Call the countdown function every millisecond and count down
intFunc = timer2_ms_countdown;
intFunc2 = function;
// (16 - 1) * 64 = 1024 uS ... close enough
OCR2A = 15;
// Enable Timer2 interrupt
TIMSK2 |= (1 << OCIE2A);
}
void timer2_stop()
{
// Page 163 of documentation
// Disable OCIE2A (Timer/Counter2 Output Compare Match A Interrupt Enable)
TIMSK2 &= ~(1 << OCIE2A);
}
// ISR of timer 2 compare vector
ISR (TIMER2_COMPA_vect)
{
if (intFunc)
{
intFunc(); // If wrapped function is set, call it.
}
}