Keeping "time" using "IF"
Why is this not working? I want to run code but keep 1 second time
var second Pub time | TimeNow TimeNow := Cnt Second := 0 Repeat IF ((TimeNow += clkfreq) == 1000) ' one second TimeNow := Cnt Second += 1 usb.str(string("DEBUG $F0")) else 'run other code
Comments
You time it to 1/1000 of a second, so their is big chance that you miss the window.
So try >1000
But the next TimeNow have to be adjusted by how far over 1000 you were.
So better of starting out from scratch with Phil's code below.
As the right way is to take current cnt and "pre calculate" slots that are extactly a clfreq's apart.
You don't want to update next_time until it increments up to or past the target value. Also, in order to keep your seconds counter from "slipping", you want to increment next_time by clkfreq rather than restarting it. The above code will do that.
-Phil
- when i first start the code its sending debug string several times out of sync
- its always over the time so my clock will begin to drift
I think this code is rollover safe.
Duane
You may have copied a bad version of my program before I corrected it. If your IF statement includes "next_time - cnt", it's the wrong version.
Here's my test program, which works:
BTW, if the "other stuff" takes longer than one second, the stuff after the IF will play catch-up to keep from losing time.
-Phil
... So far it works awesome and if I could buy a round of brews I would do do. Thanks again Phil
If you're ever in Port Townsend, I'll see to it that you get that chance!
-Phil
-Phil
At first I thought there might be a Counter roll-over issue, but the test code below doesn't show that.
Basically the code is the same as what Phil posted, but it allows you to 'tail' the Phsa accumulator.
at 80MHz, it should report a number close to 80000000... the fluctuations that you see are due to conditional processing branches. If there were a roll-over issue the fluctuation would be significant.
could you clarify how you derived the numbers on this? I want to make sure I learn and not just copy and paste. How did you come up with 53 ppm and what in turn lead you to come up with 5_000_269?
As a side note: I am using a x-tal that parallax recommends for capacitance and frequency. Its a +/- 50ppm if I recall correctly.
Beau -
Do you think that the string I am sending (after the second is incremented) to the computer is causing or influencing the delay? Frankly I don't think it is but I am not an expert yet.:)
+53.763 ppm x 5 MHz = +268.8 Hz
5_000_000 + 268.8 = 5_000_269 (rounded up)
-Phil
The string that you are sending should not have any effect, unless it takes longer than 1 second to send the entire string. The 'time' reference is relative to the counter, so anything that happens in-between is 're-synced' to the counter to within a reasonable time frame. meaning that from second to second, the amount of error does not compound. The difference in speed that you see is the tolerance error of the 5MHz crystal itself. There is nothing wrong with the crystal either, it's simply not spec'd to function as a precision timepiece over extended lengths of time. For that there are specialized IC's that require a precision crystal.... See this guy for as example ... DS1302 ... and the 32.768kHz crystal to go with it.
-Phil
Duane
I think I have to set it as
instead of
Testing it today so ill let you all know.
-Phil
-Phil
Do this:
-Phil
-Phil