-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer0.c
78 lines (67 loc) · 1.91 KB
/
timer0.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
/*
* timer0.c
*
* Author: Peter Sutton
*
* We setup timer0 to generate an interrupt every 1ms
* We update a global clock tick variable - whose value
* can be retrieved using the get_clock_ticks() function.
*/
#include "timer0.h"
#include <avr/io.h>
#include <avr/interrupt.h>
/* Our internal clock tick count - incremented every
* millisecond. Will overflow every ~49 days. */
static volatile uint32_t clock_ticks_ms;
/* Set up timer 0 to generate an interrupt every 1ms.
* We will divide the clock by 64 and count up to 124.
* We will therefore get an interrupt every 64 x 125
* clock cycles, i.e. every 1 milliseconds with an 8MHz
* clock.
* The counter will be reset to 0 when it reaches it's
* output compare value.
*/
void init_timer0(void) {
/* Reset clock tick count. L indicates a long (32 bit)
* constant.
*/
clock_ticks_ms = 0L;
/* Clear the timer */
TCNT0 = 0;
/* Set the output compare value to be 124 */
OCR0A = 124;
/* Set the timer to clear on compare match (CTC mode)
* and to divide the clock by 64. This starts the timer
* running.
*/
TCCR0A = (1 << WGM01);
TCCR0B = (1 << CS01) | (1 << CS00);
/* Enable an interrupt on output compare match.
* Note that interrupts have to be enabled globally
* before the interrupts will fire.
*/
TIMSK0 |= (1 << OCIE0A);
/* Make sure the interrupt flag is cleared by writing a
* 1 to it.
*/
TIFR0 = (1 << OCF0A);
}
uint32_t get_current_time(void) {
uint32_t return_value;
/* Disable interrupts so we can be sure that the interrupt
* doesn't fire when we've copied just a couple of bytes
* of the value. Interrupts are re-enabled if they were
* enabled at the start.
*/
uint8_t interrupts_were_enabled = bit_is_set(SREG, SREG_I);
cli();
return_value = clock_ticks_ms;
if (interrupts_were_enabled) {
sei();
}
return return_value;
}
ISR(TIMER0_COMPA_vect) {
/* Increment our clock tick count */
clock_ticks_ms++;
}