PLCcheck

SCL Counter Functions: CTU, CTD, CTUD — Complete Reference

Complete reference for IEC counter function blocks in SCL: CTU (count up), CTD (count down), CTUD (count up/down). Syntax, parameters, examples, and S5 ZV/ZR migration notes.

·8 min read
SCLcounterCTUCTDCTUDIEC 61131-3S7TIA PortalZVZRmigration

Diesen Artikel auf Deutsch lesen

SCL Counter Functions: CTU, CTD, CTUD — Complete Reference

The IEC 61131-3 standard defines three counter function blocks: CTU (count up), CTD (count down), and CTUD (count up and down). In Siemens SCL (TIA Portal), these replace the legacy S5/S7 counter functions (ZV, ZR, S_CU, S_CD) with a cleaner, more portable interface.

CTU — Count Up

CTU increments a counter value on each rising edge of the CU input. When the current value (CV) reaches or exceeds the preset value (PV), the output Q becomes TRUE.

Parameters:

ParameterDirectionTypeDescription
CUInputBOOLCount up — increments CV on rising edge
RInputBOOLReset — sets CV to 0 when TRUE
PVInputINTPreset value — Q becomes TRUE when CV ≥ PV
QOutputBOOLDone — TRUE when CV ≥ PV
CVOutputINTCurrent counter value

SCL Example:

VAR
    myCounter : CTU;
END_VAR

// Call the counter
myCounter(CU := "Count_Sensor",
          R  := "Reset_Button",
          PV := 100);

// Use the outputs
"Batch_Complete" := myCounter.Q;     // TRUE when 100 parts counted
"Parts_Count"    := myCounter.CV;    // Current count value

Behavior:

CTD — Count Down

CTD decrements a counter value on each rising edge of the CD input. When CV reaches 0, the output Q becomes TRUE.

Parameters:

ParameterDirectionTypeDescription
CDInputBOOLCount down — decrements CV on rising edge
LDInputBOOLLoad — sets CV to PV when TRUE
PVInputINTPreset value — loaded into CV when LD = TRUE
QOutputBOOLDone — TRUE when CV ≤ 0
CVOutputINTCurrent counter value

SCL Example:

VAR
    myDownCounter : CTD;
END_VAR

myDownCounter(CD := "Part_Ejected",
              LD := "Load_Batch",
              PV := 50);

"Batch_Empty" := myDownCounter.Q;       // TRUE when all 50 parts ejected
"Remaining"   := myDownCounter.CV;      // Remaining count

Behavior:

CTUD — Count Up and Down

CTUD combines both count directions in one function block. It has separate up and down inputs, separate done outputs for each direction, and both reset and load functions.

Parameters:

ParameterDirectionTypeDescription
CUInputBOOLCount up — increments CV on rising edge
CDInputBOOLCount down — decrements CV on rising edge
RInputBOOLReset — sets CV to 0
LDInputBOOLLoad — sets CV to PV
PVInputINTPreset value
QUOutputBOOLUp done — TRUE when CV ≥ PV
QDOutputBOOLDown done — TRUE when CV ≤ 0
CVOutputINTCurrent counter value

SCL Example:

VAR
    myBiCounter : CTUD;
END_VAR

myBiCounter(CU := "Part_In",
            CD := "Part_Out",
            R  := "Reset_Count",
            LD := FALSE,
            PV := 200);

"Buffer_Full"  := myBiCounter.QU;    // TRUE when 200 parts in buffer
"Buffer_Empty" := myBiCounter.QD;    // TRUE when buffer empty
"Buffer_Level" := myBiCounter.CV;    // Current buffer level

Migration from S5 ZV/ZR to IEC Counters

S5 LegacyIEC EquivalentKey Difference
ZV (Zähler vorwärts)CTUS5: BCD output, Q = CV > 0. IEC: INT output, Q = CV ≥ PV
ZR (Zähler rückwärts)CTDS5: Q = CV > 0. IEC: Q = CV ≤ 0
S_CU (S7 legacy)CTUFunctionally similar, S_CU uses BCD
S_CD (S7 legacy)CTDFunctionally similar, S_CD uses BCD

Critical difference: In S5, U Z n tests whether the counter is greater than zero (CV > 0). In IEC CTU, Q tests whether CV has reached the preset (CV ≥ PV). These are fundamentally different conditions. When migrating, you must adjust the logic accordingly.

For the detailed migration guide, see Migrating S5 Counter Programs to S7.

Availability by Platform

CounterS7-300/400S7-1200S7-1500
CTU
CTD
CTUD
DINT counters

Convert Your Counter Logic Automatically

PLCcheck Pro analyzes your S5/S7 counter code and generates IEC-compliant SCL equivalents with correct preset mapping and condition adjustments.

Upload code for counter conversion → | AWL to SCL Guide →

Part of the SCL Reference. Maintained by PLCcheck.ai. Not affiliated with Siemens AG.

Related Articles

Analyze your PLC code with AI

PLCcheck Pro explains, documents, optimizes, and migrates PLC code — automatically.

Try PLCcheck Pro →
← Back to Blog

Not affiliated with Siemens AG. S5, S7, STEP 5, STEP 7, and TIA Portal are trademarks of Siemens AG.