Topic: DMD0233 Tasks |
|
Tasks are code-blocks that can be individually enabled to run as part of the current PLC scan. Tasks are the proper choice for these two types of behavior:
The key to effectively using Tasks is to configure their runtime behavior through a combination of the Code-Block Configuration and the Enable Task (ENTASK) instruction that initiates the Task's execution. Enabling a TaskThe programmer controls when Tasks are executed by using the Enable Task (ENTASK) instruction, which is configured to execute its Task in one of two ways:
Disabling a TaskOnce a Task has been enabled, it will run on every PLC scan until one of the following happens:
Termination Scan Behavior:Termination behavior describes the actions that the Task code-block will perform on its termination scan.
Refer to the Termination Behavior Help topic for more information about the termination scan and a for list of all the programming elements that have termination logic.
|
|
Task Structure MembersEach Task in a Do-more project has an associated structure that contains status information and control fields for the Task that are programmatically accessible. The values in the structure are updated each time the Task is executed. The syntax for using them is <task name>.<field name>.
.Once - a read-only Bit location that indicates that the Task is configured to run to completion one time.
.Continuous - a read-only Bit location that the Task is configured to run Continuously on Power Flow.
.Running - a read-only Bit location that indicates whether the Task is currently running
.RanThisScan - a read-only Bit location that shows whether the Task ran as part of the current PLC scan
.Done - a read-only Bit location that shows whether the Task has completed
.FirstScan - a read-only Bit location that indicates whether this is the Task's first scan after a PROGRAM -to- RUN transition
.FirstRun - a read-only Bit location that indicates whether the Task has completed one full pass through all of it's ladder code after a PROGRAM -to- RUN transition. If the Task contains looping instructions (FOR / NEXT, WHILE / WEND, REPEAT / UNTIL, GOTO / LABEL) the Task's first run may take multiple scans to complete.
.DoneThisScan - a read-only Bit location that indicates whether the Task has executed the final rung of ladder logic in the Task
.InstrSuspend - a read-only Bit location that indicates the Task is currently being suspended by a Suspend Program or Task (SUSPEND) instruction.
.DebugSuspend - a read-only Bit location that the Task is currently being suspended by a Debug View operation - a Task that is suspended will also have a yellow background in the Project Browser.
.TimeSlice - a read-only, unsigned 32-bit location that contains the amount of time (in microseconds) per PLC scan to allow any looping instructions in the Task to run.
.Interval - a read-only, signed 32-bit location that contains how much time to delay before the Task will run again (only valid if the Task is configured to run Continuously on Power Flow)
.ScanCounter - a read-only, signed 32-bit location that contains the number of PLC scans during which the Task has run. This value is reset to 0 when the Task is first enabled and will retain the last value until the Task is enabled to run again.
.RunCounter - a read-only, signed 32-bit location that contains the number of times the Task has run to completion. This value is reset to 0 when the Task is first enabled to run and will retain the last value until the Task is enabled to run again. If there are n loopinginstructions (FOR / NEXT, WHILE / WEND, REPEAT / UNTIL, GOTO / LABEL) the .RunCounter value and the .ScanCounter value will be the same.
.IntervalAcc - a read-only, unsigned 32-bit location that contains the amount of time remaining before the Task will run again. This field is only valid if the Task is configured to run "Continuously on Power Flow at xxx Interval".
|
|
The Enable Task (ENTASK) InstructionThe Enable Task (ENTASK) instruction is used to enable a user-defined Task as part of the subsequent controller scan.
This instruction does NOT cause the CPU to immediately begin execution of the Task, rather it configures the Task to run when the CPU gets to it as part of it's normal scan processing. If the Task exists later in the execution order the Task will begin execution on the same PLC scan. If the Task exists earlier in the execution order the Task will begin execution on the subsequent scan.
The Enable selection in the Enable Task (ENTASK) instruction determines both when, and how often the Task will run based on which of the following methods is selected:
at Interval - specifies how much time (in milliseconds) to wait before the Task is allowed to run. After the Task has initially run, if the ENTASK instruction still has power flow, the Task will remain enabled and will wait the specified amount of time before running again. The default value of 0 ms means the Task will run again on the next scan. This can be any constant value between 0 and 2,147,483,647, or any readable numeric location.
|
|
Beyond using Tasks to modularize a ladder logic program, and help with segmenting the ladder logic into functional blocks that can be enabled and./or disabled, utilizing the Task related instructions can help tailor the Project's execution to better fit the application's needs. Using the Yield Program or Task (YIELD) InstructionSuppose, for example, that the most pressing need is to minimize the scan time of the controller. If the Task has ladder logic that does not need to run every scan, one or more Yield Program or Task (YIELD) instructions can be embedded in the Task. YIELD instructions will cause the Task to suspend execution at that point in the Task, and begin processing to the next project element (this could be the next Program or the main ladder logic section). The next scan this Task is run, processing will begin with the programming element right after the YIELD instruction that was last executed. Refer to the following pseudo-code:
Task Start
Ladder Code
Ladder Code
Yield
Ladder Code
Yield
Ladder Code
Ladder Code
This process of segmenting the Task into subsections that will be run on subsequent executions allows the Do-more Controller to process fewer instructions per scan, which keeps the scan time as low as possible. Of course it is the responsibility of the developer to decide where to place the YIELD instructions so that the application still functions and the scan time is minimized.
|
|
Managing Looping Instructions within TasksInside a Task you can use looping instructions (FOR / NEXT, WHILE / WEND, REPEAT / UNTIL, GOTO / LABEL) that will process the ladder logic instructions inside the loop as long as the input condition is TRUE. Using a loop in your Task brings with it the potential to cause the scan time to get really long, or if something goes really wrong, it can cause the CPU's watchdog timer to fire. So in order to prevent this from happening, each Task has a .TimeSlice value that defines the amount of processing time (in microseconds) that you want to allow a Task to process looping instructions before it yields to other processes.
The default value for the .TimeSlice value is 100, which means that the Task will process looping instructions for 100 microseconds before it yields to the next ladder logic element.
Setting the .TimeSlice value at 0 effectively means you will get one pass through the loop each time the Task is allowed to run. Refer to the following pseudo-code:
Task Start
Ladder Code
Ladder Code
While condition is true
Ladder Code
Ladder Code
Wend
Ladder Code
Ladder Code
Task End
To allow the loop to run more than once per pass through the Task, set the .TimeSlice structure member to the number of micro-seconds you want to allow the Task to run, up to a maximum value of 65534 micro-seconds.
To allow the loop to complete on one scan, set the .TimeSlice structure member to the 65535 micro-seconds.
|
|
Using Restart Program or Task (RESTART)Another way to further control the processing of a task is through the Restart Program or Task (RESTART) instruction. If a Task containing one or more YIELD instructions has been allowed to run at least once so that the execution pointer - that is where the task will begin executing when it is allowed to run again - is not at the beginning of the Task, you can use the Restart Program or Task instruction to reset the execution pointer (and the task's flags) so that the next time the Task is allowed to run, it will begin at the first rung of the Task instead of at the point of the last YIELD instruction.
Task Start
Ladder Code
Ladder Code
Yield
Ladder Code
Yield
Restart Program or Task (RESTART) <--- execution pointer was reset to the first instruction in this Task, so the following two rungs were not processed.
Ladder Code
Ladder Code
Task End
The Halt Program or Task (HALT) instruction is used to stop the execution of a Task without setting it to restart on the next scan.
|
|
Data View Display Formats for Tasks:There are 4 variations of the Data View display of Task, each with different amounts of data and / or different display orientations. These formats are for display purposes only, the Enable Task variables cannot be edited when displayed using any of the formats.
Short Multi-Line (the default):
Long Multi-Line:
|
|
See Also:
EXIT - EXIT This Program or Task
RETC - Conditional Return Back to Call
INTCONFIG - Interrupt Configuration Editor INTDECONFIG - Deconfigure Interrupt INTSUSPEND - Suspend Interrupt
|
|
Related Topics:Related Instruction Topics:
ENDC - Conditional End of Code Block
RESTART - Restart Program or Task
SUSPEND - Suspend Program or Task
|
|