-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.c
More file actions
240 lines (207 loc) · 5.42 KB
/
Copy pathtimer.c
File metadata and controls
240 lines (207 loc) · 5.42 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* ----------------------------------------------------------------------------
*
* "THE ANY BEVERAGE-WARE LICENSE" (Revision 42 - based on beer-ware license):
* <dev@layer128.net> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a be(ve)er(age) in return. (I don't
* like beer much.)
*
* Matthias Kleemann
*
* ----------------------------------------------------------------------------
*
* \file timer.c
*
* Note: It is assumed to use an ATmega8 here.
*
* \date Created: 28.11.2011 18:17:39
* \author Matthias Kleemann
**/
#include <avr/io.h>
#include "timer.h"
/***************************************************************************/
/* TIMER 0 */
/***************************************************************************/
/**
* @brief initializes Timer0
*/
void initTimer0()
{
TIMSK |= (1 << TOIE0); // overflow interrupt enable
TCCR0 = 0; // init and stop timer
}
/**
* @brief start Timer0
*/
void startTimer0()
{
TCCR0 |= TIMER0_PRESCALER;
}
/**
* @brief stop Timer0
*/
void stopTimer0()
{
TCCR0 &= ~(TIMER0_STOP);
}
/***************************************************************************/
/* TIMER 1 */
/***************************************************************************/
/**
* @brief initializes Timer1
* @param mode of Timer1 (overflow, compare, pwm, ...)
*
* \todo fill in initialization for PWM mode of Timer1
* \todo fill in initialization for Fast PWM mode of Timer1
*
*/
void initTimer1(eTimerMode mode)
{
uint8_t ctrlRegValA = 0;
uint8_t ctrlRegValB = 0;
switch(mode)
{
case TimerOverflow:
{
TIMSK |= (1 << TOIE1); // set overflow interrupt enable
break;
}
case TimerCompare:
{
// use Fast PWM and ICR for 16-bit compare mode (14) to get long periods
TIMSK |= (1 << TICIE1); // set input capture interrupt enable
ctrlRegValA |= (1 << WGM11); // set Fast PWM mode with ICR1 as compare register
ctrlRegValB |= (1 << WGM13) | (1 << WGM12); // set Fast PWM mode with ICR1 as compare register
ICR1H = (TIMER1_COMPARE_VALUE >> 8); // set compare value for interrupt
ICR1L = (TIMER1_COMPARE_VALUE & 0xFF); // set compare value for interrupt
break;
}
case TimerPwm:
{
// TODO: fill in initialization for PWM mode of Timer1
break;
}
case TimerFastPwm:
{
// TODO: fill in initialization for Fast PWM mode of Timer1
break;
}
default:
{
// nothing to do...yet.
break;
}
}
TCCR1A = ctrlRegValA; // init & stop timer
TCCR1B = ctrlRegValB; // init & stop timer
}
/**
* @brief start Timer1
*/
void startTimer1()
{
TCCR1B |= TIMER1_PRESCALER; // use defined prescaler value
}
/**
* @brief stop Timer1
*/
void stopTimer1()
{
TCCR1B &= ~(TIMER1_STOP);
}
/**
* @brief restart Timer1 (set counter register to 0)
*/
void restartTimer1()
{
stopTimer1();
setTimer1Count(0); // reset counter register
startTimer1();
}
/**
* @brief set timer1 counter register to value
* @param value of timer counter
*/
void setTimer1Count(uint16_t value)
{
TCNT1 = value;
}
/***************************************************************************/
/* TIMER 2 */
/***************************************************************************/
/**
* @brief initializes Timer2
* @param mode of Timer2 (overflow, compare, pwm, ...)
*
* \todo fill in initialization for PWM mode of Timer2
* \todo fill in initialization for Fast PWM mode of Timer2
*
*/
void initTimer2(eTimerMode mode)
{
uint8_t ctrlRegVal = 0;
switch(mode)
{
case TimerOverflow: // overflow interrupt
{
TIMSK |= (1 << TOIE2); // set overflow interrupt enable
break;
}
case TimerCompare: // CTC
{
// phase correct PWM
TIMSK |= (1 << OCIE2); // set output compare interrupt enable
ctrlRegVal |= (1 << WGM21); // set CTC mode
OCR2 = TIMER2_COMPARE_VALUE; // set compare value for interrupt
break;
}
case TimerPwm:
{
// TODO: fill in initialization for PWM mode of Timer2
break;
}
case TimerFastPwm: // Fast PWM
{
// TODO: fill in initialization for Fast PWM mode of Timer2
break;
}
default:
{
// nothing to do...yet.
break;
}
}
TCCR2 = ctrlRegVal; // init & stop timer
}
/**
* @brief start Timer2
*/
void startTimer2()
{
TCCR2 |= TIMER2_PRESCALER; // use defined prescaler value
}
/**
* @brief stop Timer2
*/
void stopTimer2()
{
TCCR2 &= ~(TIMER2_STOP);
}
/**
* @brief restart Timer2 (set counter register to 0)
*/
void restartTimer2()
{
stopTimer2();
setTimer2Count(0); // reset counter register
startTimer2();
}
/**
* @brief set timer2 counter register to value
* @param value of timer counter
*/
void setTimer2Count(uint8_t value)
{
TCNT2 = value;
}