Troubleshooting Timer Blocks Running Fast or Slow - S7_sampletime and Task Execution Cycles

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

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.

Assigned OB Cycle in HW ConfigParameter Attribute s7_sampletimeParameter Value SAMPLE_TReal-World Timer SpeedResulting ConditionOB35 (100 ms)trueAutomatically set to 0.11.0x (Accurate)Normal OperationOB33 (500 ms)trueAutomatically set to 0.51.0x (Accurate)Normal OperationOB33 (500 ms)Missing / falseLeft at default 1.02.0x (Runs 2x Fast)Elapsed time counts 2s per 1sOB32 (1000 ms)Missing / falseLeft at default 0.10.1x (Runs 10x Slow)Elapsed time takes 10s per 1sDuplicated in OB35trueSet to 0.1, called 2x/cycle2.0x (Runs 2x Fast)Runtime group duplication fault

Step-by-Step Diagnostic and Resolution Procedure

Follow this procedure to correct fast-running timer blocks in your PCS 7 project:

  1. 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 press Ctrl+F11 to 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.

  2. Add the s7_sampletime Attribute in SCL / Block Interface:
    * If source code is available in the Sources folder:

    • Open the SCL source file.
    • In the input variable declaration, find the sample time parameter (e.g., T or SampleTime).
    • Add the system attribute:
      T {s7_sampletime := 'true'} : REAL := 0.1;
    • Recompile the SCL source.
    • If source code is unavailable (compiled FB in Blocks folder):
    • 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.
  3. 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.

  4. Verify Compiler Value Assignment:
    * Open the CFC chart.
    * Inspect the input pin T. Confirm that the compiler has automatically populated T with the exact execution period of the host OB (e.g., 0.5 if in OB33).
    * Download changes to the CPU in RUN mode.

Verification

Confirm that the timer block runs at true wall-clock speed:

  1. Benchmark Against Stopwatch:
    * In CFC Test Mode, trigger the timer block with a preset time of 60.0 seconds.
    * Start a physical stopwatch at the exact instant the trigger is asserted.
    * Confirm that the timer's elapsed time output reaches 60.0 seconds at precisely one minute on the stopwatch.
  2. Verify OB Cycle Alteration Testing:
    * Manually override input T with an arbitrary value (e.g., 9.99).
    * Perform an AS compile.
    * Confirm that the CFC compiler automatically overwrites 9.99 back to the OB's true cycle time, proving s7_sampletime is 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 unconditioned READ_CLK calls are added, the CPU may enter cycle overrun protection mode, halving interrupt frequencies.
  • Never Assign s7_sampletime to Output Pins: The attribute s7_sampletime must 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

Related Articles

Related Tags

  • CFC
  • Timers
  • s7_sampletime
  • OB35
  • Cycle-Time
  • Runtime-Groups
6 respuestas