Code help "keeping time"
I am having a problem with time. After one hour my timing begins to loos a second and keeps loosing as time increases.
Any ideas?
Pub Master_Loop | mSec ,static
Timex := 0
Static := Clkfreq / 100
mSec := Cnt + Static
Repeat
IF (CNT - mSec => 0)
mSec := mSec + static
Get_mSeconds
IF (TimeX++ == 100)
TimeX := 0
Get_Seconds
Any ideas?

Comments
Static := Clkfreq / 100 lastTime := CNT time10ms := timeSeconds := 0 repeat if (CNT - lastTime) => Static lastTime += Static if ++time10ms == 100 time10ms := 0 timeSeconds++var long millis long seconds long minutes long hours pub main | t t := cnt repeat ' main loop code / calls waitcnt(t += (clkfreq >> 2)) if ((millis += 250) == 1000) millis := 0 if (++seconds == 60) secs := 0 if (++minutes == 60) minutes := 0 if (++hours == 24) hours := 0IF (CNT - mSec => 0) if (CNT - lastTime) => StaticIf timestamps were treated as unsigned then the first comparison would always return true (this is the case on the Arduino for instance).
The gotcha to be avoided is comparing two timestamps. Always compare the difference of timestamps with a time interval (and
be aware of signs where necessary). The interval must be less than 2^31 (or 2^32 for unsigned long in C)
if (CNT > target_time) ' broken comparison, fails at wraparound. target_time += StaticMike your new code works the same as my original code in that they both loose time after a hour. Am I missing something simple? Perhaps my functions are taking to long and I need to speed up the count by doing this.
Using mikes example
[b]Static := Clkfreq / 1000[/b] lastTime := CNT time10ms := timeSeconds := 0 repeat if (CNT - lastTime) => Static lastTime += Static if ++time10ms == [b]1000[/b] time10ms := 0 timeSeconds++I am going to test this now but as I test this can you think of anything that I may be forgetting?
I would post my code as soon as I can.
And the example above still counts off after the same hour of time.
Jonathan
This seems to be working -- give it a go (I lifted the logic from the bit timing of FullDuplexSerial).
var long seconds pub main | period, et1, et2 term.start(RX1, TX1, %0000, 115_200) pause(10) term.str(string(CLS, "Hello, World!", CR, CR)) period := clkfreq ' set et1 := cnt + period ' sync timer repeat { loop code here } ' update running time et2 := cnt ' capture elapsed ticks if ((et1 - et2) < 0) ' has full period elapsed? ++seconds ' yes, increment et1 += period ' and re-sync timer ' show running clock for testing (remove later) dec2(seconds / 3600) term.tx(":") dec2((seconds // 3600) / 60) term.tx(":") dec2(seconds // 60) term.tx(CR) pub dec2(value) if (value < 10) term.tx("0") else term.hex(value / 10, 1) ' faster than dec for 1 digit term.hex(value // 10, 1)Found and fixed a "gotcha" in the re-sync line
Update: I started the program and a stopwatch on my phone at the same time, then went out and ran errands for a couple hours. On my return the two were in perfect sync. I'm now going to use this code in my book! (how to create an RTC when you're out of cogs).