From 0a62ac52be5389e32425b4a7a7f625c378da678c Mon Sep 17 00:00:00 2001 From: david Date: Thu, 2 Jul 2009 11:24:12 +0200 Subject: [PATCH] add timer --- avr/usbload/timer.c | 63 ++++++++++++++++++++++++++++++++++++++++++ avr/usbload/timer.h | 5 ++++ avr/usbload/usb_bulk.c | 4 ++- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 avr/usbload/timer.c create mode 100644 avr/usbload/timer.h diff --git a/avr/usbload/timer.c b/avr/usbload/timer.c new file mode 100644 index 0000000..f8c041f --- /dev/null +++ b/avr/usbload/timer.c @@ -0,0 +1,63 @@ +#include +#include +#include + + +#ifndef OCR1A + #define OCR1A OCR1 // 2313 support +#endif + +#ifndef WGM12 + #define WGM12 CTC1 // 2313 support +#endif + +//#define XTAL 11059201L // nominal value +#define XTAL 20000000L + + +#define DEBOUNCE 256L // debounce clock (256Hz = 4msec) + +#define uint8_t unsigned char +#define uint unsigned int + +uint8_t prescaler; +uint8_t volatile second; // count seconds + + +SIGNAL (SIG_OUTPUT_COMPARE1A) +{ + +#if XTAL % DEBOUNCE // bei rest + OCR1A = XTAL / DEBOUNCE - 1; // compare DEBOUNCE - 1 times +#endif + if( --prescaler == 0 ){ + prescaler = (uint8_t)DEBOUNCE; + second++; // exact one second over +#if XTAL % DEBOUNCE // handle remainder + OCR1A = XTAL / DEBOUNCE + XTAL % DEBOUNCE - 1; // compare once per second +#endif + } +} + + +uint16_t timer_start( void ) +{ + TCCR1B = (1<