MultiTrack (Advanced): Measuring elapsed time with a SuperTimer
There are times when you will want to measure how much time has elapsed since some event. Maybe you want to display an elapsed time on the fly or determine how long it took to get up to temperature. To achieve this you need to capture the start time using STStart (which means you need to declare a 3-byte timer variable using defTIME24), and subsequently get the amount of elapsed time using fSTTimeSince. What fSTTimeSince does is to calculate the amount of time that has elapsed since the moment captured by STStart and leave the result in floating point register W. The number in W will be in units of 10mS.
Example:
The following example is a stop watch written as a MultiTrack task to run in an MMi201 with LCD. The complete program file is located in the directory path Examples>MultTrk under SPLatPC in Program Files. The file name is MulTrk02.SPT.
There are a few interesting features in this program:
- The stop watch task is written as a Finite State Machine (SPLatMap).
- There are three states: Waiting for the start button, Timing (waiting for the stop button) and waiting for the reset button. The line labels reflect the state numbers, which is all part of a scheme for producing consistent labeling.
- In the timing state (state 1, SW_1) the display is updated every 100mS. This will produce enough display activity to satisfy the user that the watch is running, without burdening the processor too much. Cunning use is made of a
WaitOnKTinstruction to monitor the stop button and time the 100mS display refresh interval in one single instruction. ResetKis used in each state to guard against inadvertently stored, latched button presses.- The actual elapsed time determination and display is done in a subroutine. There is a simple floating point calculation to scale SuperTimer counts (10mS increments) into seconds.
(Click here for some tips for working around problems with copy and paste out of Internet Explorer and HTML-help (.chm) files)
LaunchTask StopWatch
RunTasksForever
;----- StopWatch task ---------
;I/O is set up for an MMi201
iSW_StartButton iEQU 12
iSW_StopButton iEQU 11
iSW_ResetButton iEQU 10
SW_Timer defTIME24 ;Reserve RAM for the stopwatch timer
StopWatch:
SW_Set0:
OBLCD_SetCur 0,0
OBLCD_Text " 0.00" ;Clear stopwatch display
SW_0:
ResetK ;Clear Key latches
WaitOnK iSW_StartButton ;Wait for start command
SW_Set1:
STStart SW_Timer ;Start timing
ResetK ;Clear Key latches
;Main timing..
;While the SuperTimer takes care of the actual timing, we monitor
;the stop button, breaking off every 100mS to update the display.
SW_1:
WaitOnKT iSW_StopButton,10 ;100mS
GoIfT SW_Set2 ;G/ button was pressed
GoSub SW_Display ;Update the display
GoTo SW_1 ;Keep watching the button
;Here when user hits the stop button
SW_Set2:
ResetK ;Clear Key latches
GoSub SW_Display ;Display final time
SW_2:
WaitOnK iSW_ResetButton ;Wait for reset
GoTo SW_Set0 ;Repeat
;Subroutine to grab and display elapsed stopwatch time
SW_Display:
fSTTimeSince SW_Timer ;Get time
fLoadQ 0.01 ;10mS increments
fMul ;Scale to seconds
OBLCD_SetCur 0,0
OBLCD_fDispW 6,2
Return