1 // Copyright (c) 2007, 2008 Martin Thomas - BSD-license
\r
2 // 10 ms timebase with
\r
3 // 8bit timer-counter #0 in CTC-mode on ATmega644
\r
7 #include <avr/interrupt.h>
\r
8 #include "timebase.h"
\r
10 #include "key_io.h" /* keys callback/debouce */
\r
12 #define COUNTER_IS_16BIT 0
\r
13 #define PRESCALER 1024UL
\r
15 #if TIMEBASE_DELTAT_MS != 10
\r
16 #warning "code prepared for 10ms tick"
\r
19 #define TIMERFREQ (1000/TIMEBASE_DELTAT_MS) /* Hz */
\r
20 #define OCRMATCHVAL ( (F_CPU*10/PRESCALER/TIMERFREQ+5)/10 - 1 )
\r
22 #if COUNTER_IS_16BIT
\r
23 /* 16 bit counter */
\r
24 #define OCR_MATCH_MAX 0xfffe
\r
27 #define OCR_MATCH_MAX 0xfe
\r
28 #endif /* COUNTER_IS_16BIT */
\r
29 #define OCR_MATCH_MIN 0x02
\r
31 #if ( OCRMATCHVAL > OCR_MATCH_MAX )
\r
32 #error "PRESCALER too small or F_CPU too high"
\r
34 #if ( OCRMATCHVAL < 2 )
\r
35 #error "PRESCALER too large or F_CPU too low"
\r
38 volatile uint16_t timebase_tick;
\r
40 ISR(TIMER0_COMPA_vect)
\r
47 void timebase_init(void)
\r
54 // init Timer 0 Mode 4 - CTC with interrupt on compare-match
\r
56 TCCR0A = (1<<WGM01);
\r
57 #if (PRESCALER == 1024)
\r
58 TCCR0B = (1<<CS02) | (1<<CS00);
\r
60 #error "PRESCALER settings not available"
\r
63 OCR0A = OCRMATCHVAL; // match-value
\r
65 TCNT0 = 0; // reset timer-counter
\r
66 TIFR0 = (1<<OCF0A); // clear interrupt
\r
67 TIMSK0 = (1<<OCIE0A); // enabled matchA interrupt
\r
74 uint16_t timebase_get_tick(void)
\r
82 res = timebase_tick;
\r