Example (Basic): Setting and testing semaphores
The simplest (and most common) use for semaphores is simply to have your program remember some on/off or yes/no item of information for later action. If you are accustomed to relay logic, this is equivalent to using an internal relay to hold onto a bit of information or an intermediate result.
Semaphores use RAM memory, and memory must be set aside for them. The easiest way to do this is with the defSEM directive.
Example 1:
sCooling defSEM sFillLow defSEM sFillHigh defSEM sOverTemp defSEM
The following subroutine CheckLevel would return the state of a low, medium or high level probe depending on two of the above semaphores:
CheckLevel: GoIfST sFillLow,CheckLow ;Go to CheckLow if low semaphore set
GoIfST sFillHigh,CheckHigh ;Go to Checkhigh if high semaphore set
CheckMedium: Input iMediumProbe ;By default must medium
Return
CheckLow: Input iLowProbe
Return
CheckHigh: Input iHighProbe
Return
Example 2:
Semaphores can be set and cleared in several ways. They are directly set and cleared using SetS and ClrS instructions. For example, to set or clear the sFillHigh semaphore depending on whether the button on input iHighFill has been pressed or not:
SetS sHighFill ;Assume switch pressed
GoIfInK iHighFill,HighFill ;Test the switch, skip next inst if pressed
ClrS sHighFill ;Switch was not pressed ... reset semaphore again
HighFill:
Example 3:
You can also change a semaphore by storing the outcome of a previous operation (in the X register) to it. In that case any non-zero result will set the semaphore, and a zero result will clear it. Using the StoreS instruction, the previous example can be simplified:
InputK HighFill ;Read the switch
StoreS sHighFill ;Save result
If you are using MultiTrack, SPLat’s easy to use multitasking, you should also look at semaphores for task synchronization.