From 00fc05a49b0bdffe55932426c940d0ae394932dc Mon Sep 17 00:00:00 2001 From: Michael Moon Date: Tue, 8 Feb 2011 09:43:50 +1100 Subject: [PATCH] fix timer bug- if delay is smaller than TICK_TIME but counter has passed delay when timer is set, it will wait a whole TICK_TIME before firing instead of setting comparator to fire at correct time check next_step_time instead of delay in comparison Reported-by: Markus Amsler --- timer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/timer.c b/timer.c index 6ac461d..dd2bbbf 100644 --- a/timer.c +++ b/timer.c @@ -31,9 +31,9 @@ ISR(TIMER1_CAPT_vect) { clock_counter_10ms -= 10; clock_flag |= CLOCK_FLAG_10MS; - clock_counter_250ms += 10; - if (clock_counter_250ms >= 250) { - clock_counter_250ms -= 250; + clock_counter_250ms += 1; + if (clock_counter_250ms >= 25) { + clock_counter_250ms -= 25; clock_flag |= CLOCK_FLAG_250MS; clock_counter_1s += 1; @@ -99,7 +99,7 @@ void setTimer(uint32_t delay) // "fire" ISR- maybe it sets a new timeout timer1_compa_isr(); } - else if (delay <= TICK_TIME) { + else if (next_step_time <= TICK_TIME) { OCR1A = next_step_time & 0xFFFF; TIMSK1 |= MASK(OCIE1A); }