What am I doing wrong with settimer and toflag?

lbenson

Senior Member
On a 20x2, I'm trying to roughly replicate the "time" variable usage on the M2 parts (so I can interchange some M2 and X2 code).

The following code, should, to my mind, print the count of minutes followed by 60 "."s, but I get only the number "1". It appears that toflag is not being set. What am I missing?

Code:
' 20timertick ticks one second at 16mHz

  #picaxe   20X2
  #Terminal 19200

  symbol time=w27
  symbol minutes = b1

  SetFreq M16                 ; for X2 picaxe
  pause 8000 ' 2 seconda @ 16mHz
  settimer t1s_16
  toflag = 0
  time=60  ' set up for first minute
  sertxd("starting",cr,lf)

main: 
  do
    if toflag = 1 then ' a second has passed on 20X2 chip
      settimer t1s_16
      toflag = 0
      time = time + 1
      sertxd(".")
    endif
    if time > 59 then ' every minute: at 16mHz
      inc minutes
      sertxd(cr,lf,#minutes," ")
      time = 0
    endif
  loop
 

lbenson

Senior Member
Ok, answering my own question, and easier than I had thought. See this thread for the clues:
http://www.picaxeforum.co.uk/showthread.php?23285-Timer-Tangles-Resetting-the-TOFlag-for-X2-Interrupts

"toflag" is not set when the lower-order 16-bit counter overflows, but when the higher-order one (which is reflected in the "timer" variable) does. So I don't need toflag at all--just a query as to whether the value of "timer" differs from the last noted value. (Alternatively one could preset the value of timer so that it does overflow when desired, setting toflag.)
Code:
' 20timertick ticks one second at 16mHz

  #picaxe   20X2
  #Terminal 19200

  symbol time=timer
  symbol lasttime=w27
  symbol minutes = b1

  SetFreq M16                 ; for X2 picaxe
  pause 8000 ' 2 seconda @ 16mHz
  settimer t1s_16
  toflag = 0
  time=60
  sertxd("starting",cr,lf)

main: 
  do
    if lasttime <> time then
      lasttime = time
      sertxd(".")
    endif
    if time > 59 then ' every minute: at 16mHz
      inc minutes
      sertxd(cr,lf,#minutes," ")
      time = 0
    endif
  loop
This may seem clunky, but for my actual purposes, I don't need the "if lasttime=time" block, only need to know that I have a variable named "time" which counts seconds (and that variable is actually "timer").
 

lbenson

Senior Member
I got into this in part because there is no t1s_32 value for settimer (and then I confused myself). Is there a way to get "timer" to count seconds at 32mHz on the 20X2?
 

hippy

Technical Support
Staff member
I got into this in part because there is no t1s_32 value for settimer. Is there a way to get "timer" to count seconds at 32mHz on the 20X2?
Not directly because at 32MHz the on-chip hardware runs too quickly for there to be a 1 second period between timer increments.

But what you can do is set timer to $FFFE (-2) and two 500mS ticks later it will have incremented, rolled over to zero and set toflag.
 

lbenson

Senior Member
Not directly because at 32MHz the on-chip hardware runs too quickly
After printing the values of t1s_4, t1s_8, and t1s_16, looking to extrapolate, I thought that was probably the case.

... what you can do is set timer to $FFFE (-2) and two 500mS ticks later it will have incremented, rolled over to zero and set toflag.
Thank you. Good solution if I find I actually need the 32mHz speed. Doing fine now with 16mHz.
 
Top