diff --git a/time.c b/time.c index f9a4295..16c9782 100644 --- a/time.c +++ b/time.c @@ -4,25 +4,34 @@ #include #include -volatile unsigned int _eigentime_ms; +volatile struct eigentime_s _eigentime_ms; void time_init(void) { - _eigentime_ms = 0; - TCCR1B = _BV(CS10); - OCR1A = TIMER1_COMPA_VALUE; - TIMSK |= _BV(OCIE1A); + _eigentime_ms.low = 0; + _eigentime_ms.high = 0; + // prescaler is 64. + TCCR0B = _BV(CS00) | _BV(CS01); + TIMSK0 |= _BV(TOIE0); + sei(); } -unsigned int -get_eigentime(void) +void +get_eigentime(struct eigentime_s * time) { - return _eigentime_ms; + time->low = _eigentime_ms.low; + time->high = _eigentime_ms.high; } -ISR(TIMER1_COMPA_vect) +ISR(TIMER0_OVF_vect) { - _eigentime_ms++; + unsigned int last_eigentime_low = _eigentime_ms.low; + _eigentime_ms.low += TIME_MILLIS_PER_OVERFLOW; + // Handle the overflow. + if(_eigentime_ms.low < last_eigentime_low) + { + _eigentime_ms.high++; + } } diff --git a/time.h b/time.h index 9f465c2..ef88cb9 100644 --- a/time.h +++ b/time.h @@ -2,13 +2,19 @@ #define _TIME_H_ #include "conf.h" -#define TIMER1_MAX_VALUE 0xffff -#define TIMER1_COMPA_VALUE (TIMER1_MAX_VALUE / F_CPU) +// This is 2.04 = 2ms +#define TIME_MILLIS_PER_OVERFLOW (0xff * 1000UL * 64) / F_CPU -extern volatile unsigned int _eigentime_ms; +// for 32bit eigentime +struct eigentime_s +{ + unsigned int low, high; +}; -unsigned int -get_eigentime(void); +extern volatile struct eigentime_s _eigentime_ms; + +void +get_eigentime(struct eigentime_s * time); void time_init(void);