Pulse outputs (PWM, PTO) and high-speed counters
The two pulse outputs of the CPU 224 on Q0.0 and Q0.1: PWM for a DC motor, single- and multi-segment PTO for a stepper; the control bytes and registers; the PLS instruction; and high-speed counters for encoders (HDEF, HSC, 1× and 4× rates).
35 min
On this page
Ordinary outputs change once per scan: a few hundred hertz at best, with jitter. Motors need better. The CPU 224 has two hardware pulse generators, on Q0.0 and Q0.1 only, and six high-speed counters that count faster than the scan. TP 1, 3, 4 and 5 use them.
PWM: a DC motor's speed
Pulse-width modulation gives a fixed cycle time and a variable pulse width; the average voltage is with the duty cycle = width / cycle.
- Fundamental peak
- 226 V
- Switching frequency
- 750 Hz
- V/Hz command
- 400 V @ 50 Hz
For Q0.0 the registers are:
| Register | Q0.0 | Q0.1 | Content |
|---|---|---|---|
| Control byte | SMB67 | SMB77 | mode, time base, what to update, enable |
| Cycle time | SMW68 | SMW78 | in µs or ms (time base) |
| Pulse width | SMW70 | SMW80 | same unit |
| Pulse count (PTO) | SMD72 | SMD82 | number of pulses |
| Profile table offset (multi-segment PTO) | SMW168 | SMW178 | V address of the table |
The sequence is always: write the control byte, write the values, then execute PLS 0 (for Q0.0) or PLS 1. Changing the width later only needs the new value, the update bit in the control byte, and PLS again.
Control byte
Tick the bits you need; the value to move into the control byte appears below. Check each bit against the manual (Table 10-1.1).
MOVB 16#00, SMB67 (2#00000000 = 0)
PTO: a stepper's pulses
Pulse train output sends a given number of pulses at 50 % duty cycle: each pulse is one step of the stepper. A single-segment PTO has one cycle time and one pulse count. A multi-segment PTO reads a profile table in V memory: the number of segments, then for each segment a start cycle time (word), a cycle-time change per pulse (signed word) and a pulse count (double word). The cycle time changes linearly, so the frequency does not: plan your ramps with the calculator.
Multi-segment PTO profile
Each segment starts at a cycle time and changes it by a fixed number of µs per pulse. Enter the frequencies and pulse counts of your segments; the tool gives the table values and the travel time.
| # | Start (Hz) | End (Hz) | Pulses | Start cycle (µs) | Δ per pulse (µs) | Last pulse (Hz) | Time (s) | |
|---|---|---|---|---|---|---|---|---|
| 1 | 20000 | 0 | 50.0 | 1.920 |
96 pulses · 1.920 s
MOVB 1, VB500 MOVW +20000, VW501 MOVW +0, VW503 MOVD +96, VD505
The frequency does not change linearly: the cycle time does. Word values above +32767 are written in hexadecimal (16#…). Check the table layout against the manual before you use it.
When the pulse train is finished, the PTO idle bit (SM66.7 for Q0.0, SM76.7 for Q0.1) returns to 1. TP 4 waits for it before applying the brake.
High-speed counters
An encoder at 50 steps per second gives pulses every 20 ms, shorter than many scans. A high-speed counter counts them in hardware:
HDEF 0, 9defines HSC0 in mode 9 (A/B quadrature, no reset input) on I0.0 and I0.1.- The control byte SMB37 sets the direction, the counting rate (1× or 4× in quadrature modes) and what to update; SMD38 is a new current value, SMD42 a new preset.
HSC 0applies the settings. The count is read as HC0 (MOVD HC0, VD100).
ExplorerGo deeper: derivations and open questions
Why mode 9 and not mode 10? Mode 10 adds an external reset input on I0.2. On the trainer I0.2 is a call button in TP 3, so the booklet asks for mode 9 and a reset done by the program (write 0 into SMD38 and set the "update current value" bit).
Try it on the virtual bench
The simulator below wires the CPU to the trainer's moving parts: the DC motor with its pointer, proximity sensor and micro-switch, or the stepper belted to the encoder. Turn the potentiometer to change the PWM duty; press UP/DOWN to send one turn of pulses; set a frequency above 120 Hz and watch the stepper stall; then run the ramp that reaches 160 Hz without losing a step. Pull a lead to see what your program would see.
- Duty cycle
- 0 %
- Speed
- 0.0 rpm
- C0
- 0
No errors: the program compiles.
The motor turns at about 1 rev/s at full duty, not at all below about 12 % (friction), and ripples with a long PWM period. PS sees the pointer at 0°, the micro-switch at 180°: a crank lifts the barrier arm between them.