CTUD — Count Up / Down
Structured Text up/down counter function block. Uses FBD_COUNTER; combines the roles of ladder CTU and CTD.
Operands
| Name | Type |
|---|---|
counter | FBD_COUNTER |
FBD_COUNTER Tag Members
| Member | Type | Meaning |
|---|---|---|
.EnableIn | BOOL | 1 lets the instruction run; 0 holds all outputs unchanged. Normally 1. |
.CUEnable | BOOL | Rising edge (0 to 1) increments ACC by 1 |
.CDEnable | BOOL | Rising edge (0 to 1) decrements ACC by 1 |
.PRE | DINT | Preset target count |
.Reset | BOOL | 1 clears CU, CD, DN, OV, UN and sets ACC to 0 |
.EnableOut | BOOL | 1 while the instruction is executing normally |
.ACC | DINT | Accumulated count |
.CU | BOOL | 1 while CUEnable is 1 and Reset is 0 |
.CD | BOOL | 1 while CDEnable is 1 and Reset is 0 |
.DN | BOOL | 1 when ACC is greater than or equal to PRE |
.OV | BOOL | 1 when ACC overflows past 2147483647 and wraps to -2147483648 |
.UN | BOOL | 1 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
.CUEnableincrements.ACCby one. - Each rising edge on
.CDEnabledecrements.ACCby one. .DNis1when.ACCis greater than or equal to.PRE..Resetclears.ACCand status bits.
Example
CarsInLot.CUEnable := EntrySensor;
CarsInLot.CDEnable := ExitSensor;
CarsInLot.PRE := 20;
CTUD(CarsInLot);
LotFull := CarsInLot.DN;
Common Mistakes
- Setting
.EnableIn := 0and expecting the counter to stop.EnableIn = 0freezes all outputs, including.ACC,.DN, and the status bits — the counter goes dormant. To stop counting, hold.CUEnableand.CDEnableat0instead and leave.EnableInat1.