Code help "keeping time"
Bits
Posts: 414
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
If 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)
Mike 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
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).
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).