CTUD — Counter Up/Down

Pin layout

DirectionNameTypeDescription
InputCUBOOLCount Up — increment on rising edge
InputCDBOOLCount Down — decrement on rising edge
InputRBOOLReset — when TRUE, CV := 0 (priority over LD)
InputLDBOOLLoad — when TRUE, CV := PV
InputPVINTPreset Value used by both LD and QU
OutputQUBOOLTRUE when CV >= PV
OutputQDBOOLTRUE when CV <= 0
OutputCVINTCurrent Value

Semantics

R = TRUE              : CV := 0    (priority)
LD = TRUE             : CV := PV
CU rising and not R/LD: CV := CV + 1
CD rising and not R/LD: CV := CV - 1
QU := CV >= PV
QD := CV <= 0

If CU and CD both rise on the same scan, matiec applies CU first then CD — net effect is no change. Don’t rely on this ordering across other implementations.

Example

VAR
    netCount : CTUD;
END_VAR

netCount(CU := xUp,
         CD := xDown,
         R  := xReset,
         LD := xLoad,
         PV := 100);

iCurrent := netCount.CV;
xAtTop   := netCount.QU;
xAtBottom := netCount.QD;

IEC reference

IEC 61131-3 third edition (2013), Annex F.6.3.

matiec conformance

Implemented per the standard.