a500kbd
annotate 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 |
rev | line source |
---|---|
nuclear@2 | 1 #include <avr/io.h> |
nuclear@2 | 2 #include <avr/interrupt.h> |
nuclear@2 | 3 #include <avr/power.h> |
nuclear@2 | 4 #include "timer.h" |
nuclear@2 | 5 |
nuclear@2 | 6 #define PRESCL_256 4 |
nuclear@2 | 7 /* 256 ticks per interrupt, 256 clock divisor */ |
nuclear@2 | 8 #define TICKS_PER_SEC (F_CPU / 256 / 256) |
nuclear@2 | 9 |
nuclear@2 | 10 static volatile unsigned long ticks; |
nuclear@2 | 11 |
nuclear@2 | 12 void init_timer(void) |
nuclear@2 | 13 { |
nuclear@2 | 14 power_timer0_enable(); |
nuclear@2 | 15 |
nuclear@2 | 16 TCCR0A = 0; |
nuclear@2 | 17 TCCR0B = PRESCL_256; |
nuclear@2 | 18 |
nuclear@2 | 19 TIMSK0 |= (1 << TOIE0); /* enable ovf intr. */ |
nuclear@2 | 20 } |
nuclear@2 | 21 |
nuclear@2 | 22 void reset_timer(void) |
nuclear@2 | 23 { |
nuclear@2 | 24 ticks = 0; |
nuclear@2 | 25 } |
nuclear@2 | 26 |
nuclear@2 | 27 unsigned long get_msec(void) |
nuclear@2 | 28 { |
nuclear@2 | 29 return 1000 * ticks / TICKS_PER_SEC; |
nuclear@2 | 30 } |
nuclear@2 | 31 |
nuclear@2 | 32 ISR(TIMER0_OVF_vect) |
nuclear@2 | 33 { |
nuclear@2 | 34 ++ticks; |
nuclear@2 | 35 } |