Decrements a counter on each rising edge of the count input and outputs TRUE when the count reaches zero.
The CTD (Count-Down) instruction implements a down-counter, as defined in IEC 61131-3. Each time the count-down input .CD has a rising edge (FALSE-to-TRUE transition), the current value .CV is decremented by 1. When .CV reaches 0, the output .Q becomes TRUE.
Key behavior:
.CD - .CV decrements by 1.CV <= 0 - output .Q becomes TRUE.CV is loaded with .PV, .Q becomes FALSEThis is commonly used for counting down remaining items, tracking remaining cycles, or implementing "N items remaining" logic.
{GIF:HERE} - Adding a CTD count-down counter action to a step with name "counter_name", description "Count down", qualifier set to P (pulse), instruction set to CTD, and preset value of 50
from automation_machine import ActionQualifier, ActionInstruction
step.add_action("counter_name", "Count down", qualifier=ActionQualifier.P, instruction=ActionInstruction.CTD, preset_value=50)
ActionQualifier.P to count once per step activationActionInstruction.CTD50)Each CTD counter instance exposes the following child variables:
| Variable | Type | Description |
|---|---|---|
.CD |
BOOL | Count-down input - rising edge decrements the counter |
.Q |
BOOL | Output signal - TRUE when .CV <= 0 |
.PV |
INT | Preset value - the starting count (loaded on reset) |
.CV |
INT | Current value - the current remaining count |