aboutsummaryrefslogtreecommitdiff
path: root/timer.c
blob: 35657640351730f7cc1fb8134ac05447f2a54c9e (plain)
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
#include <avr/io.h>
#include <avr/interrupt.h>

#include "common.h"
#include "timer.h"

/* counts one second, off by +1s per 2m
 */

volatile uint16_t count = 0;
volatile unsigned char hit = 0;

ISR(TIMER1_COMPA_vect) {
	++hit;
}

bool timerHit () {
	bool ret = hit != 0;
	hit = 0;
	return ret;
}

void timerStart () {
	count = 0;
	/* reset timer value */
	TCNT1 = 0;
	/* set ctc (compare timer and clear) mode (part 1) */
	TCCR1A = 0;
	/* enable compare match interrupt */
	TIMSK1 = (1 << OCIE1A);
	/* set compare value */
#if F_CPU == 8000000
	OCR1A = 7812;
#else
#error "cpu speed not supported"
#endif
	/* io clock with 1024 prescaler; ctc (part 2) */
	TCCR1B = (1 << CS12) | (1 << CS10) | (1 << WGM12);
}

void timerStop () {
	/* zero clock source */
	TCCR1B = 0;
}