MultiTrack (Advanced): Multiple simultaneous SuperTimers
With MultiTrack each task gets a “free” SuperTimer for setting intervals. This was covered in an earlier lesson. But what if you need to have several timers running simultaneously within a task? You will need to manually create additional SuperTimers.
Remember: When executed inside a MultiTrack task, instructions with built in timing (Pause, WaitOnT, WaitOffT and WaitOnKT) use the task’s SuperTimer.
To manually create SuperTimers within a task (or to use SuperTimers outside MultiTrack tasks) you need to set aside 3 bytes of normal RAM (Data Memory) for each timer. The directive for doing this is
MyTimer: defTIME24
Thereafter ‘MyTimer‘ can be used to refer to the specific SuperTimer. defTIME24 sets aside 3 bytes (24 bits) of RAM to hold the time value.
To start timing with a manual SuperTimer you use the instruction
STStart MyTimer
What this does is to grab the current system time value and save it in RAM in the 3 bytes of MyTimer.
To determine if a certain amount of time has elapsed since the STStart instruction was executed, you use the STTest instruction:
STTest MyTimer,1234567
This will return True in X if the interval (1234567 times 10mS) has not yet expired, i.e. it is still timing the interval. This is consistent with the legacy timers and the Test instruction.
Example:
This example is a little contrived, but it illustrates the point. When a start button is pressed, turn on output 7 and start timing a 1 minute interval. When the same button is pressed a second time, start flashing output 0 on for 500mS and off for 500mS for the remainder of the minute. At the end of the minute make sure outputs are off and repeat the process.
(Click here for some tips for working around problems with copy and paste out of Internet Explorer and HTML-help (.chm) files)
OverallTime EQU 6000 ;1 minute in 10mS increments
MyTime defTIME24 ;Memory for SuperTimer
Start:
ResetK ;Clear input latches
Off 7 ;Turn off 1 minute indicator
WaitOnK 0 ;Wait for 1st button press
On 7 ;Indicate that we are timing
STStart MyTime ;Start 1 minute over-arching timer
Loop1:
GoIfInK 0,StartFlashing ;Test for 2nd button press
STTest MyTime,OverallTime ;Test if the minute has expired
GoIfT Loop1 ;Keep trying if the minute is not up
GoTo Start ;Timed out ... end of the game
;Flash output 0 for the remainder of the minute.
StartFlashing:
On 0 ;Flash on
Pause 50 ;Time the ON period
Off 0 ;Flash off
STTest MyTime,OverallTime ;Test if the minute has expired
GoIfF Start ;G/ no longer timing
Pause 50 ;Time the OFF period
STTest MyTime,OverallTime ;Test if the minute has expired
GoIfF Start ;G/ no longer timing
GoTo StartFlashing
This program has been coded without using MultiTrack. You will notice there are several tests for expiration of the 1 minute time interval. This is necessary because there are several places in the program where the program is in a loop but the minute could expire.
Exercise 1:
Get the program running in the SPLat/PC simulator. Hint: Reduce the time intervals to speed it up. You can also close the Pause timer window to speed it up.
Exercise 2:
Convert the program to a MultiTrack task and get it running. You will most likely get an error message first time you run it. Fix the problem. Notice that the Pause timer window no longer appears. This is because when executed from inside a MultiTrack task Pause is generated by the SuperTimer mechanism, but when not in a MultiTrack task it is generated by an older mechanism (a single, global Pause timer).