Interaction between 'time' and 'count'?

fastgrandad

New Member
I am using a count gosub routine with a 124mS period in a do - loop program with a number of other sequential gosubs within the loop. This all works fine until I add an elapsed time gosub routine to the loop that uses 'time' to count the number of minutes that the program has been running. A word variable is incremented when time >59 to count seconds, followed by time=0 to start again. 'Time' appears to be running about 18s per minute slow unless I disable the 'count' gosub routine, in which case time is then correct. I assume that 'time' is being reset or delayed by the 'count' function in some way and hope someone can throw some light on the problem. If I set the count period to 1ms, time is correct, or at least very close to it.
Chip is a 14M2 running at 4MHz.

Paul
 

hippy

Technical Support
Staff member
That is correct. While the PICAXE is giving its full attention to commands which need that it cannot accurately track the elapsing time.

It is like a stop watch which you have to pause when the phone rings and can restart when the conversation is over. Measured time will be a little less than actual elapsed time.
 

AllyCat

Senior Member
Hi Paul,

I am using a count gosub routine with a 124mS period
As you know the period of the subroutine (when the "time" prescaler is disabled) you could potentially compensate for it. For example, on each occasion that the (and any other known blocking subroutine) is called, increase a "millisecond" word variable by say 125. Then whenever "time" is read, also read "millisecond", divide by 1,000 and adjust the time and millisecond variables accordingly.

This probably still won't work perfectly because the time interrupts are asynchronous, but perhaps can be improved further by also peeking the "Timer 1" SFR, which initiates the 20ms "ticks" for the time variable.

Cheers, Alan.
 

fastgrandad

New Member
Thanks to both, for the explanation and suggestions. I'll try adding a constant and then tweek it to get it reasonably close. It's a monitor for a marine diesel engine measuring rpm, battery volts and hours-run so the timer doesn't have to be particularly accurate.

Paul
 
Top