Background timing on 20M2

Tvmender

Member
Hi All

I am struggling to get background timing to work on the 20M2. It needs to decrement a variable every 1 second.

I basically need a timer running in the background which decrements a given value once per second and its checked by the main program to see if a set time has elapsed. The main program is off doing what I call housekeeping tasks such as checking inputs, sending serial data to an IR LED, updating an LCD and checking on to see if the timer has reached zero.

I know there is the "time" function which I am struggling to get to work.

Whats the best direction for creating a background timer which is unaffected by the main program? Parallel tasks are not working for me due to the changes in processing speed when the main program performs certain housekeeping tasks (it either speeds up or slows down).

Still getting to grips with PICAXE, be gentle!
 

PhilHornby

Senior Member
If it was something non-critical, I would use the TIME variable :-
Rich (BB code):
#picaxe 20m2

symbol TimeDelay = 20         ;detect when (say) 20 seconds has elapsed

      TIME = 0                ;reset timer to zero
      
do
      if TIME >= TimeDelay then
            ;
            ; Time is up - add code to be executed here
            ;
            
            ;
            ; When we're done:-
            ;
            TIME = 0    ;reset timer to zero
      endif
      ;
      ; 'Normal' processing goes here...
      ;
loop
Note - that some Picaxe statements impact the TIME variable and cause it to run slow (Serial comms is one of them, but hserout can be used instead). Note also, that at some frequencies, the TIME variable increments by two, instead of one, every second. (Which gives you finer granularity ;) )
 

lbenson

Senior Member
For once a second (approximately) you can say:
Code:
symbol lastTime = s_w1 ' check the manual if you're unfamiliar with the "system" variables

do ' main loop
  if lastTime <> time then
    lastTime = time
    ' do stuff
  endif
  ' do other stuff
loop
If you miss a second because your "do other stuff" takes too long, well, how were you going to detect that second anyway? So this assumes that "do other stuff" takes less than a second (and that's a lot of PICAXE instructions--approximately 2,000; more at higher speeds).
 

inglewoodpete

Senior Member
Yes, from my experience with the 08M2 and 14M2, the sytstem Time variable in PICAXE M2s is remarkably accurate - to within about 3 minutes a day (about 0.2%).

For details of how changing the clock speed affects how Time increments, refer to the command descriptions for EnableTime and DisableTime.
 
Last edited:

AllyCat

Senior Member
Hi,
I .. need a timer running in the background which decrements a given value once per second and its checked by the main program ............... I call [a] housekeeping task ....... to see if the timer has reached zero.
A true "background task" would use interrupts, but it appears that you are polling the "timer" as a "housekeeping" task, so (as others have said) the time variable is probably the best approach. You've asked for a decrementing value (whilst time increments), but that can be easily handled by the type of "conditional test" in the main program polling code.

However, you must be careful about certain commands which disable interrupts (the same would apply if you were generating your own interrupts) such as SEROUT, SERTXD, SERIN and DEBUG, etc.. "Workarounds", if you need these commands, are for example to use HSERIN/OUT and/or for the main program to keep a record of the approximate amount of time "lost" whenever the commands are used (e.g. from the number of characters sent and the baud rate).
Parallel tasks are not working for me due to the changes in processing speed when the main program performs certain housekeeping tasks (it either speeds up or slows down).
As mentioned elsewhere, the M2's "multitasking" (i.e. multiple Start:s) feature is not intended for "serious" program-writing, particularly in time-critical applications. Also, if you are using commands which switch the clock to 4 MHz (e.g. IRIN), then the only "safe" method is it use 4 MHz throughout the whole program (i.e. never use SETFREQ). In most cases, it should be possible to avoid the need to use higher clock frequencies, by good program structure, particularly if using HSERIN/OUTs, but if you encounter any specific issues then we can probably find a solution.

Cheers, Alan.
 
Top