Troubleshooting Timer Blocks Running Fast or Slow - S7_sampletime and Task Execution Cycles
In SIMATIC PCS 7, when custom or standard timer blocks (such as integration, pulse, or totalizer blocks) execute twice or four times as fast as expected, the root cause is a mismatch between the block's internal sample time parameter and the actual execution cycle of its assigned cyclic interrupt OB. This occurs when the parameter attribute s7_sampletime = 'true' is missing from the block definition, causing the compiler to fail to update the sample time, or when the block is inadvertently called multiple times within the same execution cycle across duplicated runtime groups. Declaring s7_sampletime = 'true' on the time parameter and ensuring proper OB scheduling resolves the timing discrepancy.
Table of Contents
- How Digital Timer and Integrator Blocks Measure Time in PCS 7
- The Role of the
s7_sampletimeSystem Attribute - Comparison Table - Timer Block Parameter Attributes and Behaviors
- Step-by-Step Diagnostic and Resolution Procedure
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
How Digital Timer and Integrator Blocks Measure Time in PCS 7
In standard PLC programming (like classic STEP 7 ladder logic), timers often reference the CPU's internal millisecond system clock (S_ODT, TON).
However, in SIMATIC PCS 7 Continuous Function Charts (CFC), technological blocks—including timers, ramp generators, totalizers, and PID controllers—calculate elapsed time mathematically using discrete integration:
$$\Delta t = ext{Sample Time} = T_s$$
$$ ext{Accumulated Time} = ext{Accumulated Time} + T_s$$
For this mathematical model to reflect real-world seconds, the value stored in the block's internal sample time parameter ($T_s$) must strictly match the real physical time that elapses between consecutive calls of that block.
If a block is scheduled in OB33 (which executes every 500 ms), but its internal sample time parameter is hardcoded to 1.0 s, the block assumes 1.0 second has passed every time it runs. Because it runs twice per second, it accumulates 2.0 seconds of virtual time every 1.0 physical second—running twice as fast as it should!
The Role of the s7_sampletime System Attribute
To eliminate manual mathematical errors, the PCS 7 CFC compiler incorporates an automated mechanism known as the s7_sampletime attribute:
* When a block is authored in SCL or STL, an input parameter (e.g., SAMPLE_T or T) is declared with the attribute:
scl
SAMPLE_T {s7_sampletime := 'true'} : REAL := 0.1;
* During CFC compilation, the compiler inspects the cyclic interrupt organization block (OB30 through OB38) where the block's runtime group is placed.
* The compiler automatically overwrites SAMPLE_T with the exact execution cycle configured in STEP 7 HW Config (e.g., 0.1 for OB35 at 100 ms, 0.5 for OB33 at 500 ms).
When the Mechanism Breaks
If an engineer creates a custom Function Block (e.g., FB 1550) or imports a third-party block that lacks the {s7_sampletime := 'true'} attribute:
1. The compiler ignores the parameter during compilation.
2. If the block is moved to a faster or slower OB, its internal time parameter remains static at its initial default value.
3. The timer runs wildly fast or slow relative to wall-clock time.
Comparison Table - Timer Block Parameter Attributes and Behaviors
The following table compares timer block execution behavior based on parameter configuration and runtime placement.
s7_sampletimeSAMPLE_Ttrue0.1true0.5false1.0false0.1true0.1, called 2x/cycleStep-by-Step Diagnostic and Resolution Procedure
Follow this procedure to correct fast-running timer blocks in your PCS 7 project:
-
Verify Block Runtime Placement in CFC:
* Open the CFC chart containing the faulted timer block.
* Right-click the block header and select Predecessor for Insertion Position or pressCtrl+F11to open the Run Sequence.
* Note the Organization Block (OB) and runtime group hosting the block (e.g.,OB33, cycle time 500 ms).
* Verify that the block is instantiated only once in the execution list. If the block was accidentally dragged into multiple runtime groups, delete the duplicate call. -
Add the
s7_sampletimeAttribute in SCL / Block Interface:
* If source code is available in theSourcesfolder:- Open the SCL source file.
- In the input variable declaration, find the sample time parameter (e.g.,
TorSampleTime). - Add the system attribute:
T {s7_sampletime := 'true'} : REAL := 0.1; - Recompile the SCL source.
- If source code is unavailable (compiled FB in
Blocksfolder): - In SIMATIC Manager Component View, open the Blocks folder.
- Double-click the FB to open it in LAD/FBD/STL editor.
- Under the IN declaration table, highlight parameter
T. - Right-click, select Object Properties, and navigate to the Attributes tab.
- Add attribute: Name =
s7_sampletime, Value =true. - Save the block.
-
Update Block Types and Recompile:
* In SIMATIC Manager, select Options -> Update Block Types.
* Check your project to propagate the updated attribute across all CFC instances.
* Open CFC. Select Chart -> Compile -> Charts as Program.
* Check Scope: Changes only. -
Verify Compiler Value Assignment:
* Open the CFC chart.
* Inspect the input pinT. Confirm that the compiler has automatically populatedTwith the exact execution period of the host OB (e.g.,0.5if in OB33).
* Download changes to the CPU in RUN mode.
Verification
Confirm that the timer block runs at true wall-clock speed:
- Benchmark Against Stopwatch:
* In CFC Test Mode, trigger the timer block with a preset time of60.0seconds.
* Start a physical stopwatch at the exact instant the trigger is asserted.
* Confirm that the timer's elapsed time output reaches60.0seconds at precisely one minute on the stopwatch. - Verify OB Cycle Alteration Testing:
* Manually override inputTwith an arbitrary value (e.g.,9.99).
* Perform an AS compile.
* Confirm that the CFC compiler automatically overwrites9.99back to the OB's true cycle time, provings7_sampletimeis actively enforced.
Limitations and Operational Precautions
- Avoid Misusing SFC 1 (
READ_CLK) Inside Cyclic Tasks: Calling system functions like SFC 1 in rapid cyclic OBs (e.g., OB35 at 100 ms) creates heavy CPU instruction overhead. If multiple unconditionedREAD_CLKcalls are added, the CPU may enter cycle overrun protection mode, halving interrupt frequencies. - Never Assign
s7_sampletimeto Output Pins: The attributes7_sampletimemust strictly be applied to Input parameters (VAR_INPUT), never to outputs (VAR_OUTPUT). - Predecessor Insertion Order: When inserting new blocks, ensure the green insertion pointer (
Predecessor) points to the correct runtime group to prevent placing blocks in unexpected OBs.
Engineer FAQ
Why did my timer run fast after I added system clock read blocks?
System clock reads (SFC 1) are computationally heavy. If multiple blocks execute unconditioned reads, the CPU cycle time can exceed limits, triggering operating system task delays or causing engineers to mistakenly alter OB interrupt parameters.
Can I use standard S7 timers (like S_ODT) in PCS 7 CFC?
While standard S7 timers can be used, they are not recommended in PCS 7 because they rely on hardware timer numbers (T0 to T255) rather than symbolic memory, creating collision risks during automatic compilation. Always use APL timer blocks like TimerP (FB 1810).
What is the standard APL block for timer functions?
The standard APL block is TimerP (FB 1810), which supports pulse, on-delay, and off-delay modes with built-in s7_sampletime synchronization.
Does S7-PLCSIM execute timers at real-world speed?
Yes, but if the host PC CPU is heavily overloaded, PLCSIM may throttle execution cycles, causing simulated time to run slightly slower than wall-clock time.
Technical References
- SIMATIC Process Control System PCS 7 CFC for S7 (V8.1) Manual
- SIMATIC Process Control System PCS 7 Advanced Process Library Function Manual
- SIMATIC S7-SCL for S7-300 and S7-400 Programming Manual
Related Articles
- Process Image Partition (PIP) Allocation Limits and Cycle Optimization in S7-400
- Proper Procedure to Clean Up Block Types in CFC and Why 'Compile Chart as Block Type' Fails with APL
- Managing Time Synchronization and Timestamping Batch Events in SIMATIC PCS 7
Related Tags
- CFC
- Timers
- s7_sampletime
- OB35
- Cycle-Time
- Runtime-Groups