From 385867165613ad08207191b636d70cf82b467505 Mon Sep 17 00:00:00 2001 From: Andrew Makousky Date: Sun, 13 Sep 2020 18:38:44 -0500 Subject: [PATCH] Timer accuracy improvement: `+=` instead of `=`. --- firmware/rtc/MacRTC.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/firmware/rtc/MacRTC.c b/firmware/rtc/MacRTC.c index 8cb15ce..ed659f4 100644 --- a/firmware/rtc/MacRTC.c +++ b/firmware/rtc/MacRTC.c @@ -334,11 +334,13 @@ void oflowInterrupt(void) { numOflows++; if (numOflows == LIM_OFLOWS) { - // Configure a timer interrupt to handle the final remainder cycle - // wait. We simply subtract the value from 256, which is the same - // as going negative two's complement and using 8-bit wrap-around. + /* Configure a timer interrupt to handle the final remainder cycle + wait. We simply subtract the value from 256, which is the same + as going negative two's complement and using 8-bit wrap-around. + Also, since the timer may have ticked a few cycles since + wrap-around, accumulate via `+=`. */ fracRemain += NUMER_FRAC_REMAIN; - TCNT0 = (fracRemain >= DENOM_FRAC_REMAIN) ? + TCNT0 += (fracRemain >= DENOM_FRAC_REMAIN) ? -(LIM_REMAIN + 1) : -LIM_REMAIN; fracRemain &= MASK_FRAC_REMAIN; } else if (numOflows == LIM_OFLOWS + 1) {