toflag and settimer fail to reset timer?

GreenLeader

Senior Member
Hi everyone, I hope you might be able to clear this up for me:
I am trying to reset a timer in my code but it refuses to cooperate.
If I use toflag=0 it does not work in the PE simulator or in hardware.
If I use settimer=value it works in the simulator but not in the hardware.
My code is below.
I am using a 40X1, running at 8MHz on internal clock.
What am I missing?

Code:
#PICAXE 40X1
setfreq m8
hsersetup B115200_8, %10   
settimer 34285    'set timer for 1s increments @ 8MHz

for w0 = 0 to 100
    hserout 0,("count=",#w0," timer=",#timer,"s",CR,LF)    'Line 3 
    pause 2000    '=1s pause at 8MHz
    w1 = w0//5    'every 5th count reset timer
    if w1 = 0 then 'reset timer using either settimer or toflag
       'settimer 34285    'set timer for 1s increments
       toflag = 0
       hserout 0,("timer reset",CR,LF)    'Line 3        
    endif
next
 

Technical

Technical Support
Staff member
toflag is set when the timer variable overflows (65535-0) NOT at the end of every tick. Each tick simply increments the timer variable.
Setting toflag = 0 has no affect at all on the timer. To reset it to 0 simply use 'timer = 0'.
 
Last edited:

GreenLeader

Senior Member
Yes, understood (I think)....
I am trying to manually re-set the timer to zero every 5s in my example. Otherwise timer just counts up until the word variable timer overflows ie after 2^16 seconds.

According to the manual for the command "settimer":
"The toflag is automatically cleared upon the settimer
command, but can also be cleared manually via ‘let toflag = 0"

edit: OK I see you added a second sentence whilst I was busy replying... Thanks I will try that (seems obvious now that you mention it).
It works in the simulator, so will try hardware tomorrow - its bed time now:) The behaviour is not what I expected from reading the manual though.

 
Last edited:
Top