# CTD — Count Down

Source: https://rungs.dev/docs/ladder-logic/instructions/ctd

> CTD (Count Down) in rungs.dev decrements .ACC by 1 on each rising edge of the rung. Typically paired with CTU on the same COUNTER tag for bidirectional counting.

Decrements `.ACC` by 1 on each rising edge of the rung. Typically paired with `CTU` on the same `COUNTER` tag.

## Operands [#operands]

| Name      | Type      |
| --------- | --------- |
| `counter` | `COUNTER` |
| `preset`  | `DINT`    |
| `accum`   | `DINT`    |

`preset` and `accum` are pseudo-operands: they hold the value of `.PRE` and `.ACC` on the counter tag.

## How It Works [#how-it-works]

| Event                     | `.ACC`    | `.CD` | `.DN`                    |
| ------------------------- | --------- | ----- | ------------------------ |
| Rising edge of rung       | `-1`      | `1`   | `1` when `.ACC` ≥ `.PRE` |
| Rung stays true           | unchanged | `1`   | unchanged                |
| Rung false                | unchanged | `0`   | unchanged                |
| [`RES`](https://rungs.dev/docs/ladder-logic/instructions/res) on the tag | `0`       | `0`   | `0`                      |

`.ACC` can go negative. `.DN` tracks the preset comparison regardless of direction.

## Example — Remaining Slots [#example--remaining-slots]

A parking lot has 20 slots. A `CTU` counts cars entering; a `CTD` counts cars leaving. Both write to the same tag.

```ladder
XIC(EntrySensor)CTU(CarsInLot);XIC(ExitSensor)CTD(CarsInLot);XIC(CarsInLot.DN)OTE(LotFull)

CarsInLot.PRE = 20
CarsInLot.ACC = 0
```

> **IDEA**
>
> **Practice**
>
> Try counting both directions in the [Count Up/Down exercise](https://rungs.dev/exercises/count-up-down) — track a count that rises and falls on one tag.

## Common Mistakes [#common-mistakes]

- Expecting `CTD` to stop at zero — it does not. Use a comparison (`GE` / `LE`) if you need a floor.
- Pairing `CTU` and `CTD` on **different** tags — both instructions must target the same `COUNTER` tag for the counts to net out.

## Related [#related]

- [Counters overview](https://rungs.dev/docs/ladder-logic/instructions/counters)
- [CTU](https://rungs.dev/docs/ladder-logic/instructions/ctu) — count-up counterpart
- [RES](https://rungs.dev/docs/ladder-logic/instructions/res) — clears the accumulator
- [Compare instructions](https://rungs.dev/docs/ladder-logic/instructions/compare) — bound-checking