Learn › Instructions
CTU and CTD: Count Up and Count Down
Counters count events: boxes past a photo eye, cars over a sensor, presses of a button. CTU adds one each time its rung goes true, CTD subtracts one, and the done bit tells you when the count has reached the preset.
The parts of a counter
Both instructions write to a tag of type COUNTER. Like a timer, it's a small structure:
| Member | Meaning |
|---|---|
PRE (preset) | The target count. A new counter starts at 10. |
ACC (accumulator) | The current count. |
CU (count up enable) | Follows the rung of the CTU. Used to spot the false-to-true change. |
CD (count down enable) | Follows the rung of the CTD. |
DN (done) | On whenever ACC is greater than or equal to PRE. |
Read them with dotted names: XIC Boxes.DN, or use Boxes.ACC in a compare instruction.
Counting edges, not time
The most important thing to understand about counters: they count transitions, not scans. A CTU adds 1 only on the scan where its rung goes from false to true. If the rung stays true for a hundred scans, that's still one count. The rung has to go false and then true again to count again.
That's what the CU bit is for. The CTU remembers last scan's rung state in CU, and counts only when the rung is true now and CU was off. CTD does the same with CD. So holding a button down, or a box sitting in front of a photo eye, counts once, which is exactly what you want.
A timeline
A CTU named Boxes with a preset of 3, driven by a photo eye. Each box blocks the eye for about a second.
| Event | Rung (PE) | ACC | CU | DN |
|---|---|---|---|---|
| Start | false | 0 | 0 | 0 |
| Box 1 arrives | true | 1 | 1 | 0 |
| Box 1 still blocking | true | 1 | 1 | 0 |
| Box 1 leaves | false | 1 | 0 | 0 |
| Box 2 arrives | true | 2 | 1 | 0 |
| Box 3 arrives | true | 3 | 1 | 1 |
| Box 4 arrives | true | 4 | 1 | 1 |
Notice that the counter keeps counting past the preset. Reaching PRE turns DN on, but it doesn't stop the counter or reset it. To start a new batch, use a RES.
Counting a batch
Count boxes, stop the conveyor when the batch is full, and restart with a button:
| PE_Count |
|----] [------------------------------[CTU Boxes PRE 5]--|
| |
| Run Boxes.DN Conveyor |
|----] [---------]/[------------------------( )----|
| |
| NewBatch |
|----] [------------------------------------(RES Boxes)--|
The box counter tutorial builds this program step by step on the simulated conveyor.
Counting up and down on one tag
Put a CTU and a CTD on the same counter tag and you can track how many things are in an area: count up when something enters, count down when it leaves.
| PE_In |
|----] [------------------------------[CTU OnBelt PRE 4]--|
| |
| PE_Out |
|----] [------------------------------[CTD OnBelt PRE 4]--|
| |
| OnBelt.DN Full |
|----] [------------------------------------( )----|
Each instruction has its own enable bit (CU for the CTU, CD for the CTD), so the two never interfere with each other's edge detection. DN is worked out the same way by both: on whenever ACC is at least PRE. If the CTD counts below zero, ACC simply goes negative; this simulator doesn't have the overflow and underflow bits (OV and UN) that some PLCs add.
Try it in the Playground
- Open the Playground and add two push buttons:
UpandDown. - Build rung 0:
XIC UpthenCTU C1, and set its preset to 3. Build rung 1:XIC DownthenCTD C1. - Build rung 2:
XIC C1.DNthenOTE Lamp. - Press Run. Hold Up down and notice it counts once, not continuously. Tap it three times to light the lamp, then tap Down to bring the count back under 3.
Common mistakes
- Expecting the counter to stop or reset at the preset. It doesn't. Add a RES.
- Counting with a rung that never goes false. If something holds the rung true (a sealed-in bit, say), you'll only ever get one count. Count a real input or a pulse.
- Two CTUs on one tag. They share the CU bit, so they'll miss each other's edges. Combine both conditions on one rung with a branch (see AND, OR and NOT) instead.