Time going slow

I am using an 08M2 and find that the time variable is taking much longer than a second to update at 4 mhz do the serout and serin commands affect the timer variable?
 

hippy

Ex-Staff (retired)
Yes, all commands that tie-up processing to get accurate timing will prevent the internal clock ticks that ultimately update the internal 'time' variable being processed. 'Time' will therefore consequently run slow.
 

mrburnette

Senior Member
@gbbickerton:

What Hippy is describing is what is often referred to in forum posts and technical documentation as 'blocking' and the serin command is a good example. You will find that many projects make use of external "time modules" to access accurate time/date information and blocking is the very reason (add a 2nd reason in that PICAXE is a non-crystal microcontroller except for the larger units in the product line.)

So, think about the firmware in the PICAXE reading your code sequentially and performing instructions. In the background the TIME variable is being updated, but then the firmware reads a blocking instruction in your code and just sits and waits until that instruction completes and during this time, the background tasks are suspended. Bad, Bad, Bad for timekeeping.

- Ray
 
My previous post seems to have failed Can I poke a value into the time register to alter its period, I do not need great accuracy.

Barry
 

MartinM57

Moderator
It's not the period that's the problem - it's the blocking commands, as has been noted.

...taking much longer than a second...
...I do not need great accuracy...

How about some numbers?
 
The time was running at about 180% but I have reduced the use of serout and serin since then, the program runs a loop and then waits for the time to roll over to the next second, I do not think that the serin and serout commands are taking longer than a second, so I should be able to compensate as the time lost each loop will be consistent if I can get under 5% error that should be OK.
I have made a power meter for a home made turbo trainer and am trying to add a calorie counter, as the efficiency of the human body varies between 20 and 26 % calorie consumption is a shot in the dark anyway I was hoping to avoid external circuitry as the circuit is quite compact but I may have to go down that route.

Barry
 

inglewoodpete

Senior Member
If the on-chip timer is not satisfactory and you can't compensate with different code structure, then you may need to move to an off-chip solution. Real-Time clocks are available in 8-pin chips, so do not take up much more space. Once configured, you can use i2c to read the time. Alternatively, most provide a 1Hz square wave which can drive an interrupt in the PICAXE.
 

mrburnette

Senior Member
@Barry:
Since you have calcs and output down to under a second, perhaps you can outfit something like this:
08M2-and-old-wall-clock-quartz-module

Essentially you just set up a partial second "wait" in the loop until the external timer trips the next second in the loop. It is a weird implementation, but works perfectly and is a very minimum of cost and components. You could also use another PICAXE 08M2 to create a synthetic 1 second clock, or a 555.

- Ray
 
Last edited:

Dave E

Senior Member
I just ran into this problem myself and found this thread when looking for confirmation of what I found to be the case when using SEROUT while also using TIMER.

My question is what are all of the commands that can cause the TIMER variable to not increment normally?
From this thread, we know that SEROUT and SERIN affect it. From Manual 2 we can read that the SERVO command will affect the TIMER. What other commands will affect TIMER? PAUSE maybe?

Dave E.
 

eclectic

Moderator
I just ran into this problem myself and found this thread when looking for confirmation of what I found to be the case when using SEROUT while also using TIMER.

My question is what are all of the commands that can cause the TIMER variable to not increment normally?
From this thread, we know that SEROUT and SERIN affect it. From Manual 2 we can read that the SERVO command will affect the TIMER. What other commands will affect TIMER? PAUSE maybe?

Dave E.
Until you get a more definitive answer,
appendix 4 page 268 in Manual 2 (above)

e
 

AllyCat

Senior Member
Hi,

I think it's basically all the commands which invoilve intensive internal "bit banging" or "bit testing" (including waiting for an input) by the Operating System/Interpreter.

PAUSE is probably alright, but not PAUSEUS, PULSEIN/OUT, IRIN/OUT, RFIN/OUT, KBIN, etc..

It will also depend on the PICaxe chip, this thread seems to be mainly discussing M2s, but isn't TIMER an "X" family command?

Cheers, Alan.
 

Dave E

Senior Member
Thanks eclectic and A Cat.
I did not mean to highjack this discussion but it sounded like exactly what I saw with my 28X2 B.3 @16MHz and just wanted a bit more of an answer to the original question.

A little background:
I had calculated a time between 2 inputs of a single sensor reading metal pins on a conveyor. My calculation result was for about 2.6 seconds between signals. During my testing, I had my program "SEROUT" the value of TIMER as the curcuit was waiting for the second signal. I kept getting 1.72 seconds. I verified the actual time between signals with an oscilloscope as well and came up with a few milliseconds under 2.6 seconds so I knew my calculation was correct.

My code was something like this:

DO WHILE INPUT = 0
SEROUT BLAH BLAH (TIMER)
LOOP

This was an infinite holding loop until the second signal caused the input to go high thus jumping out of the loop. However, the last printed TIMER value was for about 1.72 seconds not 2.6 seconds. In troubleshooting, I removed the SEROUT command from the loop and just placed it after the loop to print out the final TIMER value and then it started printing 2.58 seconds as it should have.

Thanks again for the added info.

Dave E
 

westaust55

Moderator
The SEROUT command is a "blocking" command and will stop some tasks while the SEROUT is being performed.
See PICAXE manual 2 appendix D (from memory)
Try start/clear the timer then run your loop to wait and then SEROUT the timer value when the loop ends.
What value do you get then?
 

Dave E

Senior Member
Thanks erco and westaust55.

west if I understand you correctly, your suggestion is what I describe above and that is how I figured out that SEROUT must affect the TIMER command.

Dave E
 
Top