Problem Overview: Two PWMs on the Same CPU 1214 Drift Out of Phase
The CPU 1214 exposes multiple hardware pulse generators, each implemented as an independent timer tied to a digital output. When an engineer writes the same duty cycle to two PWM channels and verifies with an oscilloscope, the two square waves almost always show a small phase offset. The waveforms are identical in frequency and duty cycle, but the rising edges do not coincide. For motion synchronization, parallel power-converter stages, dual solenoid drive, or LED dimming applications, this phase offset is unacceptable.
The root cause is rarely a hardware defect. The PWM blocks themselves are driven by on-chip timers and run independently of OB1. The phase mismatch is introduced by the way the program updates the duty cycle: when the user program writes CTRL_PWM outputs at different points in the scan, or uses immediate-write bit syntax :P, the two channels reload their compare registers at slightly different times and the rising edges separate.
This reference walks through the exact configuration steps in TIA Portal V14 SP1 and later, the program-side update pattern that locks the two channels together, and the verification procedure that confirms zero phase error with a two-channel scope.
Hardware Architecture: Pulse Generators Inside the CPU 1214
The S7-1200 CPU 1214 contains a fixed number of high-speed pulse generators that can be configured as either PWM or PTO. These generators are tied to specific transistor outputs and run from dedicated hardware timers. Because the timers are independent, no two PWM channels are inherently phase-locked at power-up: each generator starts counting from zero the moment its first pulse train is enabled.
| CPU 1214 Variant | Available Pulse Generators | PWM Frequency Range | Supported Outputs |
|---|---|---|---|
| CPU 1214 DC/DC/DC | Up to 4 (PTO1/PWM1 ... PTO4/PWM4) | 1 Hz to 100 kHz | Q0.0, Q0.1, Q0.2, Q0.3 (transistor only) |
| CPU 1214 DC/DC/RLY | Up to 4 | 1 Hz to 100 kHz | Transistor outputs only; relay outputs cannot generate PWM |
| CPU 1214C AC/DC/RLY | Up to 4 | 1 Hz to 100 kHz | Transistor outputs only |
Each pulse generator is bound to one digital output at configuration time. The mapping is established in the device configuration, not in the program. Once the binding is fixed, the output cannot be used as a normal digital output until the pulse generator is disabled.
Root Cause: Why Two PWM Channels Show Phase Offset
Three distinct mechanisms produce phase offset between two PWM channels on the same CPU 1214. They are independent and can stack.
-
Independent counter reset. Each pulse generator has its own period counter. When
CTRL_PWMis called for channel 1, channel 1's counter starts from zero. When the call is made for channel 2 a few microseconds later, channel 2's counter starts from zero. The two counters therefore run from a slightly offset origin. -
Asynchronous duty-cycle reload. PWM hardware typically loads a new duty cycle at the end of the current period. If channel 1's duty cycle is updated at scan cycle time
Tand channel 2's duty cycle is updated atT + dt, the two compare registers are loaded in different periods and the rising edges drift apart bydt. -
Immediate-write bit
:Pwrites. Using the immediate-write bit forces the CPU to commit a value outside the normal process image update. When applied to one PWM control tag and not the other, it inserts a deterministic offset between the two reload events. The user's project is doing exactly this if the twoCTRL_PWMblocks are scattered across different networks or different OBs.
The first mechanism is unavoidable in hardware and is the source of the "initial" offset you observe on a cold start. The second and third mechanisms are programming patterns and can be eliminated.
Prerequisites
- SIMATIC S7-1200 CPU 1214C with transistor outputs (DC/DC/DC variant recommended).
- TIA Portal V14 SP1 or later. The configuration procedure is identical from V14 through V18.
- Firmware 4.2 or later on the CPU. Earlier firmware restricts PWM to a single channel per output pair.
- Two available transistor outputs (default Q0.0 and Q0.1).
- Two-channel oscilloscope or logic analyzer with bandwidth at least 10x the PWM frequency.
- The Siemens S7-1200 Pulse Generator (PTO/PWM) Configuration Manual open for reference.
Step-by-Step Configuration of Two Synchronized PWM Channels
- Open the project in TIA Portal and expand Devices & Networks.
- Select the CPU 1214 and open Device Configuration.
- In the device view, select Pulse Generator (PTO/PWM) from the Properties pane. A list of pulse channels appears. Enable the first two channels (PWM1 and PWM2).
- For each enabled channel, set the mode to PWM. Assign channel 1 to output Q0.0 and channel 2 to Q0.1. The output assignment is made from the drop-down next to the channel name.
- Set the base frequency and the minimum pulse width. For a 1 kHz, 50% duty-cycle square wave on both channels, use:
Parameter PWM1 PWM2 Output Q0.0 Q0.1 Time base Milliseconds Milliseconds Pulse duration format Percent Percent Cycle time 1 ms 1 ms Initial pulse width 50% 50% Hardware interrupt Disabled Disabled - Compile the hardware configuration and download to the CPU. The two outputs are now reserved as PWM channels and cannot be driven as normal digital outputs.
For the official configuration procedure refer to Configuring a pulse channel for PWM or PTO.
Programmatic Synchronization: One Block, One Scan, One Update
The configuration alone is not sufficient. The two channels must be started and updated from the same execution context so that the reload of the duty cycle happens within a single CPU scan. The pattern below uses a single ladder network to drive both CTRL_PWM instances simultaneously.
Ladder logic (TIA Portal, OB1):
Network 1: Synchronized PWM start/stop and duty update
|--[ M0.0 ]--[ CTRL_PWM_DB1 ]--( ENO )--|
| |
|--[ M0.0 ]--[ CTRL_PWM_DB2 ]--( ENO )--|
Network 2: Single-source duty cycle distribution
|--[ MW10 ]--[ MOVE ]--[ DB1.DutyCycle ]--|
|--[ MW10 ]--[ MOVE ]--[ DB2.DutyCycle ]--|
The same MW10 source feeds both MOVE blocks. Both writes commit in the same OB1 scan. Because the underlying hardware loads the new compare value at the next period boundary, both PWMs reload on the same period boundary and the rising edges remain aligned.
:P immediate-write suffix on either PWM control tag. The immediate write forces a write outside the process image update window and introduces a deterministic offset that defeats synchronization.If the duty cycle is computed in a function block, do not call CTRL_PWM twice with different instance DBs in different network branches. Both CTRL_PWM instances must execute in the same OB scan, in adjacent networks, and both DB1.DutyCycle and DB2.DutyCycle must be written from the same source tag before either block executes.
Correct Use of CTRL_PWM for Phase-Locked Channels
The CTRL_PWM extended instruction has the following pinout:
| Pin | Data Type | Description |
|---|---|---|
| EN | BOOL | Enable; on rising edge starts the PWM, on falling edge stops after the current period |
| PWM | HW_PWM | Pulse generator identifier (e.g. PWM1_2 for CPU 1214) |
| ENABLE | BOOL | Run bit; TRUE = continuous, FALSE = one period then stop |
| BUSY | BOOL | TRUE while the channel is active |
| STATUS | WORD | Error code; 0 = no error |
The duty cycle itself is not a pin of CTRL_PWM. It is written to the instance data block tag <DB>.DutyCycle as a normalized value. When the time base is Percent, write a value from 0 to 100 (REAL format). When the time base is Milliseconds or Microseconds, write the absolute on-time.
To synchronize two PWMs at start-up:
- Disable both PWMs (
ENABLE = FALSE). - Write the duty cycle to both instance DBs in the same network.
- Set
ENABLE = TRUEon bothCTRL_PWMinstances in the same network, on the same scan.
If the two ENABLE bits are set in different scans, the second channel starts one scan later than the first and its counter is offset by one period's worth of CPU time.
Verification with a Two-Channel Oscilloscope
Apply the following verification procedure before declaring the PWMs synchronized.
- Connect channel 1 of the scope to Q0.0 and channel 2 to Q0.1. Use identical probes and ground leads of equal length to avoid skew from the probes themselves.
- Set the scope to 2 ms/div with a 1 kHz PWM so at least three periods are visible on screen.
- Trigger on channel 1, rising edge.
- Measure the time between channel 1 rising edge and channel 2 rising edge. A synchronized pair shows delta-t < 50 ns (the jitter floor of the two timer domains).
- Change the duty cycle from 25% to 75% in a single scan and confirm both channels change on the same period boundary.
- Power-cycle the CPU. Within 200 ms of restart, both PWMs must be in phase. If they drift after a power cycle, the start sequence is being driven from two different sources.
Persistent offset of more than one period indicates that the two CTRL_PWM blocks are being called in different OBs or at different priorities. Move both calls into OB1.
Edge Cases and Field-Proven Caveats
| Condition | Symptom | Cause | Fix |
|---|---|---|---|
| PTO enabled on same channel | Channel 2 never starts | PTO and PWM are mutually exclusive on a single pulse generator | Disable PTO, leave only PWM enabled |
| Two PWMs at different cycle times | Phase offset appears to drift | Period boundaries do not align; rising edges cross | Set identical cycle times on both channels |
| Relay output assigned as PWM | Output never toggles or chatters | Relay contacts cannot switch at PWM rate | Reassign to a transistor output |
| High-speed counter configured on same pin | PWM output is overwritten by HSC | HSC takes priority over PWM on shared I/O | Move HSC to a different input; PWM output is output-only |
| Duty cycle written from HMI tag every 100 ms | Phase offset of up to 100 ms | Asynchronous duty update from HMI tag | Have the PLC apply the value from the HMI tag to both DBs in a single OB1 network |
| OB35 or OB1 priority conflict | One channel updates more often than the other | Different OBs call the two CTRL_PWM blocks | Consolidate into a single OB |
Troubleshooting Matrix
| Observed Behavior | Most Likely Cause | Diagnostic | Resolution |
|---|---|---|---|
| Both PWMs run, constant small offset | Two CTRL_PWM in different networks, same OB | Read DB1.DutyCycle and DB2.DutyCycle online; verify equal value | Move both CTRL_PWM into a single network with one common enable |
| Offset changes with scan time | Duty cycle written from different sources per scan | Cross-reference the duty write tag | Use a single source tag and a single MOVE block pair |
| One PWM off, other running | ENABLE bit not set or output reassigned | Inspect PWM configuration in device view | Verify channel 2 output assignment and CTRL_PWM STATUS word |
| STATUS = 16#8001 | Channel in use by PTO or HSC | Check device configuration for channel overlap | Disable conflicting function |
| STATUS = 16#8002 | Hardware fault on output driver | Check diagnostic buffer of CPU | Verify wiring and load |
| Offset equals one period | ENABLE bit latched one cycle late on channel 2 | Trace ENABLE bit back to source | Set ENABLE in same network for both channels |
Commissioning Checklist
- CPU 1214 firmware 4.2 or later confirmed in online & diagnostic view.
- Both pulse generators configured as PWM (not PTO) with identical cycle time.
- Outputs assigned to two distinct transistor outputs.
- Single source tag for duty cycle; both DBs updated in one network.
- No
:Pimmediate-write on any PWM control tag. - No HSC, PTO, or frequency-measurement function conflicting on either output.
- Oscilloscope verification performed with delta-t below the timer jitter floor.
- Power-cycle test passed with no post-restart drift.
FAQ
Why do my two S7-1200 PWM outputs show a phase offset even when I write the same duty cycle to both?
Each pulse generator has its own hardware counter. If your program calls CTRL_PWM for the two channels in different networks, different OBs, or at different priorities, the counters start at slightly different times and the duty-cycle registers reload in different periods. Consolidate both CTRL_PWM calls into a single network in OB1 and write the duty cycle to both instance DBs from a single source tag.
Can the CPU 1214 hardware-synchronize two PWM channels?
The CPU 1214 has no dedicated phase-lock line between its pulse generators. Synchronization is a software responsibility: keep the enable, duty write, and run flags of both channels in the same OB scan. The hardware timers themselves are not adjustable for phase, so software discipline is the only field-proven method.
Is the :P immediate-write suffix required for PWM control?
No. The :P suffix forces an immediate write outside the process image update window and will create a phase offset between two PWMs if applied to only one of them. Leave it off all PWM control tags and let the standard process image update handle the write.
What is the maximum PWM frequency on the CPU 1214?
The CPU 1214 supports PWM frequencies from 1 Hz up to 100 kHz on its transistor outputs. Above 100 kHz you must use a signal board or an external pulse-stretching stage.
Why does the second PWM never start when the first is already running?
Either the second output is not assigned as a pulse generator in the device configuration, the CTRL_PWM STATUS word reports a non-zero error code, or a PTO/HSC function is occupying the same output. Check device configuration and the diagnostic buffer.