-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.ino
114 lines (104 loc) · 2.78 KB
/
timer.ino
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
void (*timer1_callback)(void);
void (*timer3_callback)(void);
void (*timer4_callback)(void);
void (*timer5_callback)(void);
void timer_init(timer_initstruct_t* InitStruct)
{
cli();//stop interrupts
uint16_t period;
// period = InitStruct->period;
period = (uint16_t)( ( 16000000 / (InitStruct->period)) / (1024));
switch(InitStruct->timer)
{
case TIMER_1:
{
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1kHzincrements
OCR1A = (uint16_t)period;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
timer1_callback = InitStruct->callback;
TIMSK1 |= (1 << OCIE1A);
break;
}
case TIMER_3:
{
TCCR3A = 0;// set entire TCCR1A register to 0
TCCR3B = 0;// same for TCCR1B
TCNT3 = 0;//initialize counter value to 0
// set compare match register for 1kHzincrements
OCR3A = (uint16_t)period;
// turn on CTC mode
TCCR3B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR3B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
timer3_callback = InitStruct->callback;
TIMSK3 |= (1 << OCIE1A);
break;
}
case TIMER_4:
{
TCCR4A = 0;// set entire TCCR1A register to 0
TCCR4B = 0;// same for TCCR1B
TCNT4 = 0;//initialize counter value to 0
// set compare match register for 1kHzincrements
OCR4A = (uint16_t)period;
// turn on CTC mode
TCCR4B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR4B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
timer4_callback = InitStruct->callback;
TIMSK4 |= (1 << OCIE1A);
break;
}
case TIMER_5:
{
TCCR5A = 0;// set entire TCCR1A register to 0
TCCR5B = 0;// same for TCCR1B
TCNT5 = 0;//initialize counter value to 0
// set compare match register for 1kHzincrements
OCR5A = (uint16_t)period;
// turn on CTC mode
TCCR5B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR5B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
timer5_callback = InitStruct->callback;
TIMSK5 |= (1 << OCIE1A);
break;
}
default: break;
}
sei();//allow interrupts
}
#ifndef TMR1
ISR(TIMER1_COMPA_vect)
{
timer1_callback();
}
#endif
#ifndef TMR3
ISR(TIMER3_COMPA_vect)
{
timer3_callback();
}
#endif
#ifndef TMR4
ISR(TIMER4_COMPA_vect)
{
timer4_callback();
}
#endif
#ifndef TMR5
ISR(TIMER5_COMPA_vect)
{
timer5_callback();
}
#endif