; A simple multi-tasking program using Finite State Machines. ************************************************************************************ * NOTE: This example is fairly old. A MUCH more "modern" way of doing this is with * MultiTrack ************************************************************************************ ;Memory Task1State mEQU 0 Task2State mEQU 1 Task3State mEQU 2 Task1Counter mEQU 3 Task2Counter mEQU 4 Task3Counter mEQU 5 Tick50 mEQU 6 ;50mS timer tick GoSub Task1Init GoSub Task2Init GoSub Task3Init ******************************************************* ; The "Main loop" simply calls each task (FSM) in turn, plus any global services like ; the Tick generator. This is a basic task scheduler Main GoSub DoTimerTicks GoSub Task1Update GoSub Task2Update GoSub Task3Update GoTo Main ********************************************************************************************* ;Task1 rotates a light through front panel LEDs 13-15. Direction depends on the top push button ;The task is structured as a subroutine with two entery points: xxxUpdate and xxxInit Task1Update RetIfMZ Tick50 ;Do nothing except on a tick DMGNZ Task1Counter,GenRet ;Count down ticks Task1Init SetMem Task1Counter,5 BranchM Task1State Target Task1_State0 Target Task1_State1 Target Task1_State2 Task1_State0 GoIfInOn 12,SetState2 SetState1 Off 15 On 14 Off 13 SetMem Task1State,1 GenRet Return Task1_State1 GoIfInOn 12,SetState0 SetState2 Off 15 Off 14 On 13 SetMem Task1State,2 Return Task1_State2 GoIfInOn 12,SetState1 SetState0 On 15 Off 14 Off 13 SetMem Task1State,0 Return ********************************************************************************************* ;Task2 flashes the bottom LED at 150mS on, 60mS off Task2Update RetIfMZ Tick50 DMGNZ Task2Counter,GenRet BranchM Task2State Target Task2_State0 Target Task2_State1 Task2_State0 SetMem Task2Counter,3 ON 9 SetMem Task2State,1 Return Task2_State1 SetMem Task2Counter,12 Off 9 SetMem Task2State,0 Return Task2Init SetMem Task2Counter,1 ;A short initial value just to get things going Return ********************************************************************************************* ;Task3 ;Fill in your own task here. ; Suggestions: ;1. Bottom button flashes a LED for 1.5 Seconds. Use the global Tick50 for timing. ; Consider the accuracy of the output pulse relative to when the button is sensed. ;2. The bottom 2 buttons move a light through the 3 remaining front panel LEDs. ;3. Win a game by pressing one of the 3 bottom buttons when the corresponding ; LED from Task1 is ON together with the bottom LED. ;4. Elaborate the game so the player has to do all 3 button/LEDs. Count how many tries it takes and ; display on the LCD. Use a different button to reset the game. Task3Update Task3Init Return ********************************************************************************************* ;Update the global timer ticks (in this case there's only one) DoTimerTicks SetMem Tick50,0 Test 4 RetIfT SetTimer 4,5 SetMem Tick50,T Return