Troubleshooting
Use the PLC Coder timer block correctly in TIA Portal
Short answer
A Stateflow delay generated with PLC Coder follows wall-clock time only when the timing implementation and the PLC task call interval agree with the model sample time. Pick the target IDE timer for real elapsed time; use the Target-Independent Counter only when the generated code runs at a fixed periodic interval equal to the model step.
On this page
A Stateflow delay that behaved correctly in simulation does not start following wall-clock time because a function block named PLC Coder Timer appeared next to your generated code after the import into TIA Portal. That block is part of the timing implementation the generator selected; which time base it actually uses was fixed in the PLC Coder settings before export, and the delay behaves correctly only if the task calling the generated code runs at the interval the model assumes. This article walks the three decisions in order: choose the timing implementation, know how your delay was translated into step counts, and set the task call interval to match. Get those three right and the delay on the PLC matches the one you saw in Simulink.
What the PLC Coder Timer block is and is not #
The block appears because your model contains time-based logic, typically a Stateflow temporal operator such as after(5,sec) on a transition. It holds the counter or elapsed-time state that logic needs between executions. It is not a self-running delay: with a counter-based implementation it does not read a clock at all, it counts calls of the generated code. The block name alone tells you nothing about the calling convention it expects, so do not wire it as if it were a stand-alone on-delay timer.
Choose the timing implementation before you export #
From release R2020b, the PLC Coder interface settings offer a choice between the target IDE timer implementation and the Simulink timer implementation, identified as Target-Independent Counter. The target IDE is the engineering software of the destination controller, which here is TIA Portal. This selection decides the time base of everything generated from the model, including the timer block, so make it deliberately:
- Target IDE timer. The generated timer uses the target environment's timer implementation and tracks real elapsed time. Choose this whenever the delay must follow machine time, or whenever you cannot guarantee that the generated code is called at a strictly fixed interval.
- Target-Independent Counter. The generated logic counts executions instead of reading the target timer. Choose this only when the generated code is called at a fixed periodic interval equal to the model sample time; the counter is then a valid proxy for seconds.
Releases earlier than R2020b have neither setting; check that release's PLC Coder documentation for what its generated timers use. Record the release and the implementation you selected, and change the selection by regenerating, never by editing the imported blocks.
How a Stateflow delay becomes a step count #
A Stateflow transition condition such as after(5,sec) expresses model time. In a fixed-step model with a sample time of 0.1 s, model time advances one step per execution of the generated logic, so the generated comparison threshold is 5 s ÷ 0.1 s/step = 50 executions. The real delay on the PLC is therefore threshold × actual call interval, and that is where imported code goes wrong:
- Called in a cyclic interrupt OB configured at 100 ms: 50 × 0.1 s = 5.0 s. Correct.
- Called in a 10 ms task: 50 × 0.01 s = 0.5 s. The delay fires 9.5 s early.
- Called in a free-running cyclic OB: the interval varies with program load, so the delay drifts and never repeats exactly.
Read the generated comparison rather than assuming its semantics: confirm that the counter increments once per call, and check whether the threshold comparison happens before or after the increment, because that shifts the effective delay by one step.
Call the generated code at the interval the model assumes #
The scheduling contract is simple: model step time must equal the actual interval between calls of the generated model entry point. A free-running cyclic task repeats according to its workload, so its interval moves. A cyclic interrupt OB gives a configured interval, subject to the controller's execution limits — the same fixed-interval discipline behind running Logix 5000 PID loops in a periodic task. Watch the task diagnostics: on an S7-1200 the time error interrupt OB80 executes when the scan cycle exceeds the maximum cycle time, and an overrun that leaves no headroom around your model step means the interval assumption has already broken.
One more limit applies even with the timer correct: a state machine reacts only when it executes, so a timer expiry becomes visible at the next call boundary, not at the exact second it elapses. That is the same reason a fast PLC scan can still miss short input pulses, and it matters when an output must change within a few milliseconds of the delay ending.
Troubleshooting a delay that fires early or late #
Work down this list; each line names the symptom, the cause and the fix:
- Fires early. Target-Independent Counter with a call interval shorter than the model step. Move the call into a cyclic interrupt OB set to the model sample time, or regenerate with the target IDE timer.
- Fires late or never completes. Initialization of the timer instance data on every scan, a duplicate call advancing state faster than intended, or a conditional call that skips steps. Give the model exactly one call path per step and initialize at startup only.
- Drifts between machines or between days. The call sits in a free-running cyclic task. Move it to a periodic OB.
- Correct in simulation, wrong on the PLC. Implementation and call structure do not match what the model assumed. Record the release and implementation, regenerate, and import a complete matching run.
For verification, measure the call interval from the task diagnostics independently of the timer being tested — an incorrect time base can otherwise appear to validate itself — and check the generated behavior against a model run, as described in the page on model-based testing with Simulink PLC Coder. Test the corrected timing with the driven outputs safely isolated from the actuator, because an early transition can start motion nobody ordered.
The decision, in one line: if the delay must track real elapsed time and your task structure cannot guarantee a fixed call interval, regenerate with the target IDE timer implementation; if you can run the generated code in a cyclic interrupt OB set exactly to the model sample time, the Target-Independent Counter is valid and keeps the code target-independent. What never works is importing the timer block and trusting its name to count seconds.
Questions asked
Why does my five second Stateflow delay fire after half a second on the PLC?
The model ran at a fixed step of 0.1 s, so the generated Target-Independent Counter compares a threshold of 5 / 0.1 = 50 calls. If the generated code is called every 10 ms, those 50 calls elapse in 50 × 0.01 s = 0.5 s. Move the call into a cyclic interrupt OB configured at 100 ms, or regenerate with the target IDE timer implementation so the delay reads elapsed time instead of counting calls.
Does the choice between target IDE timer and Target-Independent Counter exist before R2020b?
No. R2020b is the release that added the option in the PLC Coder interface settings to specify whether the generated code uses the target IDE timer implementation or the Simulink timer implementation, named Target-Independent Counter. On an older release, check that release's PLC Coder documentation for what its generated timer actually uses, and plan a regeneration with a release that offers the selectable implementation if you need to change the time base.
Can I edit the generated timer function block in TIA Portal to fix the timing?
Editing the generated block desynchronizes the PLC code from the model, and the next export overwrites the edit. Change the timing implementation in the PLC Coder interface settings or change the model timing itself, then regenerate and import one complete matching generation run, so the timer block, the state machine logic and the call structure all come from the same run.
How do I measure the real call interval of the generated code?
Read the task and cycle-time diagnostics of the configured OB in the TIA Portal project instead of trusting the timer under test. Measure the interval between successive calls of the generated model entry point and compare it with the model sample time. On an S7-1200, also configure the time error interrupt OB80: it executes when the scan cycle exceeds the maximum cycle time, which flags a task that can no longer keep the assumed interval.