| Author |
Message |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 20 April 2007, 23:40 PM Post subject: Timer Accuracy and Tasks |
|
|
I'm experiencing a problem with timing while running multiple tasks on a ZX-40. In total, there are 7 tasks, most of which are running as fast as the processor will allow. One task in particular, the trusty ol' hour meter is losing about 1/4 second per minute, and I can't seem to figure out why. The code is a simple Call Sleep command. When not 'sleeping', a persistent variable is incremented by 1 every minute. Perhaps I'm seeing the EEPROM write cycle time, but it seems way too long. I'm currently isolating the hour meter code in order to run it by itself to see if the other tasks are causing the problem.
Any ideas/suggestions?
-Don
|
|
| Back to top |
|
 |
stevech
Joined: 23 Feb 2006
|
|
Posted: 21 April 2007, 3:14 AM Post subject: |
|
|
| the ZBasic system (VM) has an interrupt driven real time clock and calendar. Do you use that to follow elapsed time? It's correct no matter task latency, etc.
|
|
| Back to top |
|
 |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 21 April 2007, 13:12 PM Post subject: |
|
|
| Well, I wasn't, and that was the problem. I failed to take into account the execution time of the code. I've since modified it to look at the minute value of the RTC and run the code once per minute rather than just sleeping the task for 60 seconds.
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 21 April 2007, 14:55 PM Post subject: |
|
|
| Don_Kirby wrote: | | I've since modified it to look at the minute value of the RTC and run the code once per minute rather than just sleeping the task for 60 seconds. |
You could also employ WaitForInterval() to provide the timing:
| Code: | ...
Call SetInterval(60.0)
Do
Call WaitForInterval(0)
' this code will be executed once per minute
...
Loop |
|
|
| Back to top |
|
 |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 23 September 2007, 22:44 PM Post subject: |
|
|
I'm revisiting this issue due to an upcoming release deadline...
The code works fine using WaitForInterval(0). The interval is set at 1 second, at which time a few lines of code are executed, a counter is incremented and the task again waits for another interval. Every minute (60 iterations according to the counter) a few more lines of code are executed along with the other code.
However, I'm loosing about 1 second every 4 hours. The loss builds gradually, so the problem is not related to the code running inside the task, or at least I don't think it is. It's not 'missing' an interval at any point.
The accuracy is acceptable for the application, but I'm curious what is causing the loss. I imagine that the prime culprit would be the oscillator, although the resolution of WaitForInterval would be my next guess.
I've set up the logic analyzer with the hope that I can get some definitive timing information, but it seems that the floppy drive has seen better days and it refuses to boot. If anyone knows where I can get a floppy drive for an HP 1652B, please let me know.
-Don
|
|
| Back to top |
|
 |
GTBecker
Joined: 18 Jan 2006
Location: Cape Coral
|
|
Posted: 23 September 2007, 22:50 PM Post subject: Timer Accuracy and Tasks |
|
|
> ... a floppy drive for an HP 1652B...
FWIW, most older floppy drives die of a dry belt; you might resurrect it
with an appropriate o-ring. More recent floppies use direct-drive.
Tom
|
|
| Back to top |
|
 |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 23 September 2007, 23:02 PM Post subject: |
|
|
I haven't opened it up yet to see, but from the sounds coming from it, I suspect you're right.
-Don
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 24 September 2007, 20:30 PM Post subject: |
|
|
| Don_Kirby wrote: | | I'm loosing about 1 second every 4 hours. | How do you detect the loss? Could you send me some minimal code that reproduces the symptom?
|
|
| Back to top |
|
 |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 24 September 2007, 21:36 PM Post subject: |
|
|
The application sends a string of data via the serial port once per second. Something akin to an NMEA sentence. I'm watching that via HyperTerminal.
I timing it using XP synced to an atomic clock server. I expect to see +/- 1 second accuracy due to system latencies on both the PC and the ZX serial port transmission. The most recent test (still running) has lost 6 seconds over the last 30 hours. A bit less than I first suspected, but still about 1 second every 5 hours.
I've attached a test app. The module you see is 1 task of the several in the application. The only changes made were to define some variables to emulate the variables present in the rest of the application. It compiles fine, but I haven't tested on hardware yet.
-Don
| Description: |
|
 Download |
| Filename: |
TimingTest.zip |
| Filesize: |
3.34 KB |
| Downloaded: |
252 Time(s) |
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 24 September 2007, 22:11 PM Post subject: |
|
|
| Don_Kirby wrote: | | [It loses] about 1 second every 5 hours. | If you were to run the reporting code with nothing else going on that would indicate whether the problem is related solely to the RTC or if it is being caused by some other activity.
|
|
| Back to top |
|
 |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 25 September 2007, 0:59 AM Post subject: |
|
|
Just so I am sure I am understanding the WaitForInterval sub correctly, perhaps you can verify..
If the interval expires, but the task is not ready (in this case, it's outputting via com1 or writing the persitent to EEPROM or both), the fact that the interval expired is recorded and the task is scheduled to run as soon as possible. The interval counter is restarted regardless of whether or not the task has actually run yet.
If the code execution time is very slightly longer than one second, the task will eventually drop a second as WaitForInterval may have set the flag twice (the task is busy from the very end of second 00:01 to right after second 00:02) but there is only an indication that the flag has been set, not how many times.
This would cause a sudden 1 second skip near the 5 hour mark, rather than the gradual lag I am seeing here.
If my understanding is correct, then the only possibility is that the RTC is either running slow, or it's missing ticks, the latter of which I didn't think would be a problem as other functions or subs that use the RTC keep track of the missed ticks.
In either case, I'm running the reporting code alone, with nothing else. The clocks are synchronized as of now. Only time will tell...(bad pun intended)
-Don
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 25 September 2007, 2:29 AM Post subject: |
|
|
| Don_Kirby wrote: | | Just so I am sure I am understanding the WaitForInterval sub correctly, perhaps you can verify.. | Your conjecture regarding the operation is essentially correct. If a task awaiting the expiration of the interval cannot run before the interval expires a second time, it will effectively miss the first one.
It would seem that changing the interval to a larger value (say, 5 seconds) might confirm that the potential problem you mentioned is not an issue.
|
|
| Back to top |
|
 |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 25 September 2007, 20:20 PM Post subject: |
|
|
After testing for a little over 19 hours, I have a lag of about 5 seconds.
Better than before, but still not what I would call stellar accuracy.
I'll change the interval to 5 seconds and run the test again.
Don, what is the stated accuracy of the crystal supplied with the ZX-40 Interface Board?
|
|
| Back to top |
|
 |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 25 September 2007, 23:19 PM Post subject: |
|
|
The scope is up and running again, and it tells me that my crystal is not quite as accurate as I'd like. I'm seeing frequency variations +/- about .05 Mhz, with the trend being towards the slow side.
Does anyone have any benchmark figures for frequency accuracy using a ZX-24? When my boards get here, I'll be using the same crystal (almost) as it uses.
-Don
| Description: |
|
 Download |
| Filename: |
IMG00135.jpg |
| Filesize: |
224.77 KB |
| Downloaded: |
265 Time(s) |
|
|
| Back to top |
|
 |
Don_Kirby
Joined: 15 Oct 2006
|
|
Posted: 25 September 2007, 23:20 PM Post subject: Re: Timer Accuracy and Tasks |
|
|
| GTBecker wrote: | most older floppy drives die of a dry belt; you might resurrect it with an appropriate o-ring.
|
You hit the nail on the head Tom. Thanks.
-Don
|
|
| Back to top |
|
 |
|