a500kbd
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/timer.c Tue Oct 17 15:25:33 2017 +0300 1.3 @@ -0,0 +1,35 @@ 1.4 +#include <avr/io.h> 1.5 +#include <avr/interrupt.h> 1.6 +#include <avr/power.h> 1.7 +#include "timer.h" 1.8 + 1.9 +#define PRESCL_256 4 1.10 +/* 256 ticks per interrupt, 256 clock divisor */ 1.11 +#define TICKS_PER_SEC (F_CPU / 256 / 256) 1.12 + 1.13 +static volatile unsigned long ticks; 1.14 + 1.15 +void init_timer(void) 1.16 +{ 1.17 + power_timer0_enable(); 1.18 + 1.19 + TCCR0A = 0; 1.20 + TCCR0B = PRESCL_256; 1.21 + 1.22 + TIMSK0 |= (1 << TOIE0); /* enable ovf intr. */ 1.23 +} 1.24 + 1.25 +void reset_timer(void) 1.26 +{ 1.27 + ticks = 0; 1.28 +} 1.29 + 1.30 +unsigned long get_msec(void) 1.31 +{ 1.32 + return 1000 * ticks / TICKS_PER_SEC; 1.33 +} 1.34 + 1.35 +ISR(TIMER0_OVF_vect) 1.36 +{ 1.37 + ++ticks; 1.38 +}