Skip to main content

CTUD — Count Up / Down

Structured Text up/down counter function block. Uses FBD_COUNTER; combines the roles of ladder CTU and CTD.

Operands

NameType
counterFBD_COUNTER

FBD_COUNTER Tag Members

MemberTypeMeaning
.EnableInBOOL1 lets the instruction run; 0 holds all outputs unchanged. Normally 1.
.CUEnableBOOLRising edge (0 to 1) increments ACC by 1
.CDEnableBOOLRising edge (0 to 1) decrements ACC by 1
.PREDINTPreset target count
.ResetBOOL1 clears CU, CD, DN, OV, UN and sets ACC to 0
.EnableOutBOOL1 while the instruction is executing normally
.ACCDINTAccumulated count
.CUBOOL1 while CUEnable is 1 and Reset is 0
.CDBOOL1 while CDEnable is 1 and Reset is 0
.DNBOOL1 when ACC is greater than or equal to PRE
.OVBOOL1 when ACC overflows past 2147483647 and wraps to -2147483648
.UNBOOL1 when ACC underflows past -2147483648 and wraps to 2147483647

How It Works

CTUD is the Structured Text function-block counter. It combines the roles of ladder CTU and CTD on a single FBD_COUNTER tag.

  • Each rising edge on .CUEnable increments .ACC by one.
  • Each rising edge on .CDEnable decrements .ACC by one.
  • .DN is 1 when .ACC is greater than or equal to .PRE.
  • .Reset clears .ACC and status bits.

Example

CarsInLot.CUEnable := EntrySensor;
CarsInLot.CDEnable := ExitSensor;
CarsInLot.PRE := 20;
CTUD(CarsInLot);

LotFull := CarsInLot.DN;

Common Mistakes

  • Setting .EnableIn := 0 and expecting the counter to stop. EnableIn = 0 freezes all outputs, including .ACC, .DN, and the status bits — the counter goes dormant. To stop counting, hold .CUEnable and .CDEnable at 0 instead and leave .EnableIn at 1.
  • CTU, CTD — the ladder-logic counterparts