# CTUD — Count Up / Down

Source: https://rungs.dev/docs/structured-text/instructions/ctud

> CTUD is the Structured Text up/down counter function block in rungs.dev. It uses FBD_COUNTER and combines the roles of ladder CTU and CTD instructions.

Structured Text up/down counter function block. Uses `FBD_COUNTER`; combines the roles of ladder [`CTU`](https://rungs.dev/docs/ladder-logic/instructions/ctu) and [`CTD`](https://rungs.dev/docs/ladder-logic/instructions/ctd).

## Operands [#operands]

| Name      | Type          |
| --------- | ------------- |
| `counter` | `FBD_COUNTER` |

## `FBD_COUNTER` Tag Members [#fbd_counter-tag-members]

| Member       | Type   | Meaning                                                                        |
| ------------ | ------ | ------------------------------------------------------------------------------ |
| `.EnableIn`  | `BOOL` | Forced to `1` each scan by the instruction call — setting it `0` has no effect |
| `.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 [#how-it-works]

`CTUD` is the Structured Text function-block counter. It combines the roles of ladder [`CTU`](https://rungs.dev/docs/ladder-logic/instructions/ctu) and [`CTD`](https://rungs.dev/docs/ladder-logic/instructions/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.
- An enable that is already `1` when the program starts does not count. The counter only counts a `0 → 1` change it sees while the program is running. ([Prescan](https://rungs.dev/docs/scan-cycle/the-scan-cycle#before-the-first-scan) sets this up before the first scan.)

## Example [#example]

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

LotFull := CarsInLot.DN;
```

> **IDEA**
>
> **Practice**
>
> Try `CTUD` in the [Count Up Down exercise](https://rungs.dev/exercises/count-up-down) — track a running balance with separate Up and Down inputs.

## Common Mistakes [#common-mistakes]

- Trying to stop the counter with `.EnableIn := 0`. The instruction call forces `.EnableIn` back to `1` every scan, so the assignment has no effect. To stop counting, hold `.CUEnable` and `.CDEnable` at `0` (no rising edges).
- Assigning `.ACC` from logic to reset it (`MyCounter.ACC := 0`). Structured Text recomputes `.ACC` every scan, so a runtime write is **ignored** (the initial `.ACC` default is still honored). Clear it with `.Reset := 1`. The same write _does_ take effect on the ladder [`CTU`](https://rungs.dev/docs/ladder-logic/instructions/ctu) / [`CTD`](https://rungs.dev/docs/ladder-logic/instructions/ctd) — so don't rely on it either way; it isn't portable.

## Related [#related]

- [CTU](https://rungs.dev/docs/ladder-logic/instructions/ctu), [CTD](https://rungs.dev/docs/ladder-logic/instructions/ctd) — the ladder-logic counterparts