40X1 : timer and toflag question

vertigo

Member
Hello,
when I use this code (40X1 firmware A.6, external 16 MHz crystal, prog editor 5.2.7) :

Code:
#picaxe 40x1
#No_Table
#No_Data

setfreq em16

settimer t1s_16

high portc 5
pause 4000
low portc 5

main:
 gosub checkTimer
 pause 100
 goto main

checkTimer:
 if toflag = 1 then
  high portc 5 : pause 1000 : low portc 5
  toflag = 0
  timer = 0xffff
 endif
 return
I expect the LED connected to portC 5 to flash once a second... but it doesn't flash at all, except once at the very beginning of the program (high/low).

I can't use setintflgas because I already use and need setint.

Is the toflag variable only updated (= set when timer overflows) when using setintflags ?
What I have understood about timers is that, once set with settimer, the internal variable timer will count in the background, and when it exceeds 65536, the toflag variable will be set to 1.

Could someone clarify this, please ?

I have searched for and found some other threads about timers, but most use setintflags.

What I want to do is being able to know when about 1 second has elapsed (accuracy is not very important), when going to checkTimer.

Thank you for your help,

Christophe
 

hippy

Ex-Staff (retired)
It probably does work, just that the first time 'toFlag' gets set is 65536 seconds after the program starts :)

You need to add a 'timer = 0xffff" at the start to get the first 'toFlag' set a second later.

The 'timer' increments everytime the SETTIMER period overflow, 'toFlag' gets set when 'timer' overflows.
 

vertigo

Member
Thank you Hippy : it works !

For information, here is the modified and working as expected code :

Code:
#picaxe 40x1
#No_Table
#No_Data

setfreq em16

settimer t1s_16
[B]timer = 0xffff[/B] [COLOR=DarkGreen]'[B]<- this line added[/B][/COLOR]

high portc 5
pause 4000
low portc 5

main:
 gosub checkTimer
 pause 100
 goto main

checkTimer:
 if toflag = 1 then
  high portc 5 : pause 100 : low portc 5
  toflag = 0
  timer = 0xffff
 endif
 return
As always, this forum was very helpful. Thank you again,

Christophe
 
Top