From 92eb4c97cae2b4773f86e6ba666dd4cb3af6ca19 Mon Sep 17 00:00:00 2001 From: Michael Moon Date: Thu, 16 Sep 2010 21:42:24 +1000 Subject: [PATCH] reset timer properly for more accurate timing --- timer.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/timer.c b/timer.c index 3b71b7d..19b358f 100644 --- a/timer.c +++ b/timer.c @@ -103,8 +103,12 @@ void setTimer(uint32_t delay) // we also then calculate the timer ceiling required. (ie what the counter counts to) // the result is the timer counts up to the appropriate time and then fires an interrupt. - setTimerCeiling(getTimerCeiling(delay)); - setTimerResolution(getTimerResolution(delay)); + setTimerResolution(0); // stop timer + TCNT1 = 0; // reset timer + GTCCR = MASK(PSRSYNC); // reset prescaler - affects timer 0 too but since it's doing PWM, it's not using the prescaler + + setTimerCeiling(getTimerCeiling(delay)); // set timeout + setTimerResolution(getTimerResolution(delay)); // restart timer with proper prescaler } // delay( microseconds )