Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Revision:
31:f6ff7fdb9c67
Parent:
30:212ca42b8779
Child:
33:b9e3c06e7dab
diff -r 212ca42b8779 -r f6ff7fdb9c67 timer.c
--- a/timer.c	Fri Feb 16 17:30:46 2018 +0000
+++ b/timer.c	Thu Nov 29 16:51:19 2018 +0000
@@ -47,13 +47,25 @@
     return (value * fraction) >> 32;
 }
 
-bool TimerTicked = false;
+bool TimerHadSecond = false;
+
+//Counts from zero to 2^32 and wraps around after:
+// 13.7 years if  10 per second - scan time must be less than 100mS
+// 1.37 years if 100 per second - scan time must be less than  10mS
+uint32_t TimerTicks = 0;
 
 void TimerMain()
 {
-    TimerTicked = TimerCountSinceLastSecond() > TIMER_COUNT_PER_SECOND;
+    TimerHadSecond = TimerCountSinceLastSecond() > TIMER_COUNT_PER_SECOND;
+    if (TimerHadSecond) secondsBaseCount        += TIMER_COUNT_PER_SECOND;
+    
+    static uint32_t tickBaseCount = 0;
     
-    if (TimerTicked) secondsBaseCount += TIMER_COUNT_PER_SECOND;
+    if (TC - tickBaseCount > TIMER_COUNT_PER_SECOND / TIMER_TICKS_PER_SECOND)
+    {
+        TimerTicks++;
+        tickBaseCount += TIMER_COUNT_PER_SECOND / TIMER_TICKS_PER_SECOND;
+    }
 }
 void TimerInit()
 {