a500kbd
view src/timer.c @ 2:a4fd9c5a6655
first working version
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 17 Oct 2017 15:25:33 +0300 |
parents | |
children |
line source
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <avr/power.h>
4 #include "timer.h"
6 #define PRESCL_256 4
7 /* 256 ticks per interrupt, 256 clock divisor */
8 #define TICKS_PER_SEC (F_CPU / 256 / 256)
10 static volatile unsigned long ticks;
12 void init_timer(void)
13 {
14 power_timer0_enable();
16 TCCR0A = 0;
17 TCCR0B = PRESCL_256;
19 TIMSK0 |= (1 << TOIE0); /* enable ovf intr. */
20 }
22 void reset_timer(void)
23 {
24 ticks = 0;
25 }
27 unsigned long get_msec(void)
28 {
29 return 1000 * ticks / TICKS_PER_SEC;
30 }
32 ISR(TIMER0_OVF_vect)
33 {
34 ++ticks;
35 }