DriveLab
Account

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

Show me

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 D⋅VdcD \cdot V_{dc} with the duty cycle DD = width / cycle.

Try it: how an inverter makes a sine wave
Fundamental peak
226 V
Switching frequency
750 Hz
V/Hz command
400 V @ 50 Hz

V / f = 400 V / 50 Hz = 8 V/Hz → 50 Hz : 400 V

For Q0.0 the registers are:

RegisterQ0.0Q0.1Content
Control byteSMB67SMB77mode, time base, what to update, enable
Cycle timeSMW68SMW78in µs or ms (time base)
Pulse widthSMW70SMW80same unit
Pulse count (PTO)SMD72SMD82number of pulses
Profile table offset (multi-segment PTO)SMW168SMW178V 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)PulsesStart cycle (µs)Δ per pulse (µs)Last pulse (Hz)Time (s)
120000050.01.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, 9 defines 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 0 applies 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.

Virtual PLC-200 bench: stepper, encoder and DC motor
PS · I1.1Micro-switch · I1.0Barrier arm (crank)
Duty cycle
0 %
Speed
0.0 rpm
C0
0
Pull a lead

No errors: the program compiles.

SIMATIC S7-200 · CPU 224SFRUNSTOP
I0.0RUN_SW
I0.7CLEAR
I1.0MICROsensor
I1.1PROXsensor
Q0.0MOTOR0
Timet = 0.00 s · 0 scans

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.