X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;ds=sidebyside;f=BaseMegaFirmware%2FGCC%2Ftimebase.c;fp=BaseMegaFirmware%2FGCC%2Ftimebase.c;h=92bf0152d76cab12401172ff790ea866ff7ccad9;hb=89173ec9cd6f33843bf27c45be0f2f9be3f5f0ce;hp=0000000000000000000000000000000000000000;hpb=7997f136b12114035265bd082c854bff95fd73dc;p=avr_bc100.git diff --git a/BaseMegaFirmware/GCC/timebase.c b/BaseMegaFirmware/GCC/timebase.c new file mode 100644 index 0000000..92bf015 --- /dev/null +++ b/BaseMegaFirmware/GCC/timebase.c @@ -0,0 +1,87 @@ +// Copyright (c) 2007, 2008 Martin Thomas - BSD-license +// 10 ms timebase with +// 8bit timer-counter #0 in CTC-mode on ATmega644 + +#include +#include +#include +#include "timebase.h" + +#include "key_io.h" /* keys callback/debouce */ + +#define COUNTER_IS_16BIT 0 +#define PRESCALER 1024UL + +#if TIMEBASE_DELTAT_MS != 10 +#warning "code prepared for 10ms tick" +#endif + +#define TIMERFREQ (1000/TIMEBASE_DELTAT_MS) /* Hz */ +#define OCRMATCHVAL ( (F_CPU*10/PRESCALER/TIMERFREQ+5)/10 - 1 ) + +#if COUNTER_IS_16BIT + /* 16 bit counter */ +#define OCR_MATCH_MAX 0xfffe +#else +/* 8 bit counter */ +#define OCR_MATCH_MAX 0xfe +#endif /* COUNTER_IS_16BIT */ +#define OCR_MATCH_MIN 0x02 + +#if ( OCRMATCHVAL > OCR_MATCH_MAX ) +#error "PRESCALER too small or F_CPU too high" +#endif +#if ( OCRMATCHVAL < 2 ) +#error "PRESCALER too large or F_CPU too low" +#endif + +volatile uint16_t timebase_tick; + +ISR(TIMER0_COMPA_vect) +{ + timebase_tick++; + + key_io_callback(); +} + +void timebase_init(void) +{ + uint8_t sreg; + + sreg=SREG; + cli(); + + // init Timer 0 Mode 4 - CTC with interrupt on compare-match + + TCCR0A = (1<