Newer
Older
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
// This module uses TIMER1 to keep track of time
#include "timer_1.h"
#include <avr/interrupt.h>
uint32_t overflow_count;
#define TIMER_MASK (1<<CS10)
inline void timer_init(void) {
// enable overflow interrupt
TIMSK1 |= (1<<TOIE1);
}
inline void timer_start(void) {
//disable interrupts globally
cli();
//clear out timer counting register
TCNT1 = 0;
//clear overflow count
overflow_count = 0;
// set up clock source
// we have a 16MHz Clock
// and a 16 Bit timer
// Use a prescaler of 256 -> 16/256 = 62.5kHz
TCCR1B |= TIMER_MASK;
//enable interrupts
sei();
}
inline uint32_t timer_ticks(void) {
return TCNT1 ^ (overflow_count << 16);
}
inline uint32_t timer_stop(void) {
// stop timer clock
TCCR1B &= ~(TIMER_MASK);
return timer_ticks();
}
ISR(TIMER1_OVF_vect) {
overflow_count++;
}