Topic: DMD0233

Help File Version: 2.9.4.37

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:

 

  1. a single pass through ladder logic that executes exactly one time.
     

  2. logic you can execute at a specific interval, for example: once a second, or once every 50ms, or every scan.

 

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 Task

The 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:

 

  1. Execute its Task one time on the leading edge of the input logic.
     

  2. While the input power-flow is ON, execute its Task recurrently at a specified time interval (in milliseconds).

Disabling a Task

Once a Task has been enabled, it will run on every PLC scan until one of the following happens:

 

  1. The Task will automatically be disabled once all of the ladder logic in the Task has been executed one time.
     

  2. Another Program or Task executes a Halt Program or Task (HALT) instruction that targets this Task.
     

  3. The Project Browser -> Debug Code Block -> HALT Program or Task option is used to disable the Task.

Termination Scan Behavior:

Termination behavior describes the actions that the Task code-block will perform on its termination scan.

 

  1. All of the Output Coil (OUT) instructions will be turned OFF.
     

  2. All Timers will be reset.
     

  3. All Counters will be reset.
     

  4. All instructions with an edge-triggered input will have their edge inputs reset.

 

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 Members

Each 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

  • is ON if the Task is running

  • is OFF if the Task is not running

 

.RanThisScan - a read-only Bit location that shows whether the Task ran as part of the current PLC scan

  • is ON if the Task ran on the current scan

  • is OFF if the Task did not run on the current scan

 

.Done - a read-only Bit location that shows whether the Task has completed

  • will be ON if the Task has run to completion

  • will be OFF if the Task is currently running or has never run

 

.FirstScan - a read-only Bit location that indicates whether this is the Task's first scan after a PROGRAM -to- RUN transition

  • is ON if the Task is executing it's first scan

  • is OFF if the Task has completed it's first scan or has never run

 

.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.

  • is ON if the Program is still executing it's first complete pass

  • is OFF if the Program has completed it's first full pass

 

.DoneThisScan - a read-only Bit location that indicates whether the Task has executed the final rung of ladder logic in the Task

  • is ON if the Task will stop on the current scan

  • is OFF if the Task will not stop execution on the current scan

 

.InstrSuspend - a read-only Bit location that indicates the Task is currently being suspended by a Suspend Program or Task (SUSPEND) instruction.

  • is ON if the Task is suspended by a SUSPEND instruction

  • is OFF if the Task is not suspended by a 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.

  • is ON if the Task is suspended by a debug operation

  • is OFF if the Task is suspended by a debug operation

 

.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) Instruction

The 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:

Once on Leading Edge - select this option to have the Task run to completion exactly one time. Depending on the contents of the Task, this may take more than one PLC scan, but the entire Task will be executed then it will be disabled.
 


Configured this way the Enable Task instruction will have a gray triangle at the right end of an input leg indicating the input is Edge TriggeredEach time the input logic transitions from OFF to ON this instruction will execute. With each execution, this instruction will run to completion even if the input logic transitions to OFF before the instruction completes.. Continuous on Power Flow - select this option to have the Task run as long as the ENTASK instruction has power flow. Once the end of the Task is reached, it will remain enabled and execution will start over at the top of the Task.

 

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) Instruction

Suppose, 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               < -- this rung is executed the 1st scan the Task is run

 

Ladder Code               < -- this rung is executed the 1st scan the Task is run

 

Yield

 

Ladder Code               < -- this rung is executed the 2nd scan the Task is run

 

Yield

 

Ladder Code               < -- this rung is executed the 3rd scan the Task is run

 

Ladder Code               < -- this rung is executed the 3rd scan the Task is run

 

Task End

 

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 Tasks

Inside 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               < -- this rung is executed the 1st scan the Task is run

 

Ladder Code               < -- this rung is executed the 1st scan the Task is run

 

While condition is true

 

Ladder Code               < -- this rung is executed as long as the loop condition is met

 

Ladder Code               < -- this rung is executed as long as the loop condition is met

 

Wend             < -- the Task always will yield at this point, and return here once the task is re-run

                      < -- on return, if While condition is TRUE, the while loop will be executed again

                      < -- on return, if While condition is FALSE, execution will move to the following rungs

 

Ladder Code               < -- this rung is executed if the While condition is false

 

Ladder Code               < -- this rung is executed if the While condition is false

 

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               < -- this rung is executed the 1st scan the Task is run

 

Ladder Code               < -- this rung is executed the 1st scan the Task is run

 

Yield

 

Ladder Code               < -- this rung is executed the 2nd scan the Task is run

 

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 Single Line:

 

Short Multi-Line (the default):

 

Long Single Line:

 

Long Multi-Line:

 


See Also:

Code Block Overview

 

Programs

RUN - Run Program

EXIT - EXIT This Program or Task

 

Tasks

ENTASK - Enable Task

 

Subroutines

CALL - Call Subroutine

RET - Return Back to Call

RETC - Conditional Return Back to Call

 

Interrupt Service Routines

Setup Interrupt Triggers

INTCONFIG - Interrupt Configuration Editor

INTDECONFIG - Deconfigure Interrupt

INTRESUME - Resume Interrupts

INTSUSPEND - Suspend Interrupt

 

Termination Scan Behavior

 


Related Topics:

Project Browser

Related Instruction Topics:

END - End Program or Task

 

ENDC - Conditional End of Code Block

 

HALT - Halt Program or Task

 

RESTART - Restart Program or Task

 

SUSPEND - Suspend Program or Task

 

YIELD - Yield Program or Task