Tasks are special procedures the compiler breaks up into individual states. Like a Sub or Function, a Task can contain basic statements, assembly commands, and local variables. Unlike a Sub or Function, a Task cannot have parameters or return values.
A task is defined with the Task and End Task statement:
Task ADCReader()
End Task
Each time a task is called with the RunTasks statement, an individual states within that task will be executed. When the end of a state is reached the task will exit passing control back to the calling procedure. Statements like AllowOthers, WaitWhile, and WaitUntil are used to break the task up into states as follows.
Task ADCReader()
' state 0 starts here
call SetADCchanel(0) ' Point ADC to channel 0
TMR0=0: TMR0IF=0 ' Use TMR0 to time the acquisition period for AN0
WaitWhile TMR0IF=0 ' state 0 returns here.
' When this task is called the next time, the
' condition of TMR0IF will be checked. While it
' remains 0 this state will simply return.
' When the condition goes true the internal state
' pointer for this task will be advanced to the
' next state and then return.
' Internally this statement consumes 1 state
' state 2 starts here
GO=1 ' Start the ADC Conversion
WaitUntil GO=0 ' state 2 returns here.
' This is very similar to WaitWhile, except it
' waits until the condition is true.
' Like WaitWhile, this statement consumes 1 state.
' state 4 starts here
adcValue = ADRES ' Assign the ADC value to a global variable.
' state 4 ends here
' At the end of a task, the state pointer is set back
' to state 0 before exiting.
End Task
Before a task is called the state pointers should be initialized with the initTasks() statement. This will reset the tasks internal state pointer and resume any suspended tasks. Once tasks have been initialized, they are ready to be called through the RunTasks() statement.
A task will only execute when called using the RunTasks() statement, this usually takes place inside a main execution loop. The following program will execute the ADCReader task.
Dim adcValue as uInteger ' Used to store the adc reading from the
' ADCReader task
Sub main()
Call initialize() ' initialize the hardware here
InitTasks() ' initialize all tasks
While 1 ' beginning of the task execution loop
Clrwdt ' clear the watchdog timer
RunTasks() ' Run all tasks 1 cycle
' Do something with the dacValue here
End While ' end of the task execution loop
End Sub
Following is a brief description of task related commands, for more detail see Appendix A.
|
AllowOthers |
Exits the current task to give other tasks a chance to execute. Execution will resume after this command the next time the task is executed. Not allowed outside a task. |
|
InitTasks() |
Initialize all tasks. |
|
StopMe |
Suspends the execution of an individual task. Not allowed outside a task |
|
SuspendTask(task list) |
Suspend execution of an individual or list of tasks. |
|
RestartMe |
Resets the current task to the beginning of the task Not allowed outside a task |
|
ResumeTask(task list) |
Resume execution of an individual or list of tasks. |
|
RunTasks(task list) |
Execute all or a list of tasks one scan. Not allowed inside a task |
|
WaitUntil condition |
Exit the current task until condition is true, then continue execution with the next statement. |
|
WaitWhile condition |
Exit the current task while condition is true, then continue execution with the next statement. |