Measuring Elapsed Milliseconds Between Two Digital Pulses in SIMATIC PCS 7 CFC (SFC64 TIME_TCK)

Measuring Elapsed Milliseconds Between Two Digital Pulses in SIMATIC PCS 7 CFC (SFC64 TIME_TCK)

To measure elapsed milliseconds between two discrete digital pulses in SIMATIC PCS 7 CFC without specialized kernel blocks, use System Function SFC64 (TIME_TCK). On the rising edge of Pulse 1 (Start), call SFC64 and store the returned 32-bit millisecond tick counter in a static variable. On the rising edge of Pulse 2 (Stop), call SFC64 again and subtract the start tick from the stop tick. To handle the standard 32-bit tick counter overflow (which rolls over every ~24.8 days from 2147483647 to 0), apply rollover compensation logic: if (StopTick < StartTick), calculate ElapsedTime = (2147483647 - StartTick) + StopTick + 1. This provides exact millisecond precision bound only by the OB execution cycle.

Table of Contents

Industrial Applications for Pulse Interval Timing in Process Control

In process automation and packaging systems, engineers frequently must measure precise time intervals between physical events:

  • Calculating the flight time of a diverter flap or high-speed safety valve from fully open to fully closed limit switch.
  • Measuring product velocity on a conveyor between two photocell optical sensors.
  • Calculating flow meter pulse intervals for low-frequency positive displacement meters.

When specialized hardware counter modules (such as FM 350 or TM Count) are absent and standard kernel libraries (OSI_KERNEL) are unavailable, engineers must implement high-precision timing logic directly within Continuous Function Charts (CFC).

Why Standard Timers (S_PULSE, TimerP) Are Inadequate

Engineers often attempt to measure elapsed time using standard STEP 7 timers (such as S_PULSE or S_PEXT) or APL blocks like TimerP (FB1810). However, these blocks suffer from severe operational limitations:

  1. Resolution Bounds: Standard S5 timers use BCD time bases (10 ms, 100 ms, 1 s) with low resolution and non-linear decay.
  2. Cycle Accumulation Jitter: Counting execution OB cycles (e.g., adding 10 ms every OB35 run) introduces cumulative phase drift if cyclic interrupt execution is slightly delayed by higher-priority interrupt OBs (OB82, OB86).

To achieve millisecond-exact timing independent of cyclic drift, querying the CPU's internal hardware system clock is mandatory.

System Function SFC64 (TIME_TCK) Architecture and Rollover Dynamics

The Siemens CPU operating system maintains an internal millisecond hardware tick counter accessed via SFC64 (TIME_TCK):

  1. Data Format: SFC64 returns a 32-bit integer (TIME data type) representing elapsed milliseconds since CPU startup.
  2. Range and Rollover: The counter counts from T#0ms (0) up to T#24d20h31m23s647ms (2,147,483,647 ms). Upon reaching this maximum positive integer value, the counter rolls over to 0.

Any timing algorithm utilizing SFC64 must incorporate mathematical rollover compensation so that measurements bridging the 24.8-day rollover point do not produce negative or erroneous values.

Implementing the Measurement Algorithm in S7-SCL and CFC

The cleanest, most robust implementation is a lightweight custom Function Block authored in Structured Control Language (S7-SCL):

FUNCTION_BLOCK FB_PulseTimer
VAR_INPUT
    StartPulse : BOOL;   // Pulse 1: Starts measurement
    StopPulse  : BOOL;   // Pulse 2: Stops measurement
    Reset      : BOOL;   // Manual reset
END_VAR
VAR_OUTPUT
    Elapsed_ms : DINT;   // Measured time in milliseconds
    Valid      : BOOL;   // True when measurement completed
    Measuring  : BOOL;   // True while waiting for StopPulse
END_VAR
VAR
    StartTick  : TIME;
    StopTick   : TIME;
    OldStart   : BOOL;
    OldStop    : BOOL;
END_VAR

BEGIN
    // Detect rising edge of StartPulse
    IF StartPulse AND NOT OldStart THEN
        StartTick := TIME_TCK(); // Calls SFC64
        Measuring := TRUE;
        Valid := FALSE;
    END_IF;
    OldStart := StartPulse;

    // Detect rising edge of StopPulse while measuring
    IF StopPulse AND NOT OldStop AND Measuring THEN
        StopTick := TIME_TCK(); // Calls SFC64
        Measuring := FALSE;

        // Calculate elapsed time with rollover handling
        IF StopTick >= StartTick THEN
            Elapsed_ms := TIME_TO_DINT(StopTick - StartTick);
        ELSE
            Elapsed_ms := (2147483647 - TIME_TO_DINT(StartTick)) + TIME_TO_DINT(StopTick) + 1;
        END_IF;
        Valid := TRUE;
    END_IF;
    OldStop := StopPulse;

    IF Reset THEN
        Measuring := FALSE;
        Valid := FALSE;
        Elapsed_ms := 0;
    END_IF;
END_FUNCTION_BLOCK

Pulse Measurement Method Comparison Matrix

The following table compares different pulse timing techniques in SIMATIC PCS 7:

Timing TechniqueResolutionMax IntervalDrift Immune?Execution OverheadSFC64 TIME_TCK1 millisecond~24.8 daysYES (Hardware Clock)Extremely low (Single SFC call)SFC1 READ_CLK1 millisecond (BCD DT)DecadesYES (Real-Time Clock)Moderate (Requires date/time parsing)Cycle Counter in OB35Scan time (e.g. 100 ms)UnlimitedNO (Vulnerable to jitter)Low (Addition every cycle)Standard S5 Timers10 ms to 1000 ms~2.5 hoursNO (BCD decay)LowHardware Counter FM 350Microseconds (<1 ms)UnlimitedYES (FPGA Counter)Requires dedicated hardware module

Verification

Verify the timing block through the following test sequence:

  1. Instantiate FB_PulseTimer in a fast cyclic interrupt OB (e.g., OB35 at 10 ms or OB36 at 50 ms).
  2. Trigger StartPulse via a test bit. Confirm that Measuring = TRUE and Valid = FALSE.
  3. Exactly 2,500 ms later, trigger StopPulse. Confirm that Measuring drops to FALSE and Valid transitions to TRUE.
  4. Verify that Elapsed_ms displays 2500 (within +/- 1 scan cycle tolerance).
  5. Simulate a rollover condition by initializing StartTick to 2147483000 and StopTick to 500; confirm that Elapsed_ms correctly calculates 1148 ms without numeric underflow.

Limitations and Operational Precautions

Consider these operational rules when measuring pulse intervals in software:

  1. Execution Cycle Bound: While SFC64 provides 1 ms tick resolution, the digital inputs (StartPulse, StopPulse) are sampled only as fast as the OB in which the block executes (e.g., 10 ms in OB35). Events occurring faster than the OB scan require process interrupt OBs (OB40) or hardware counter modules.

  2. Process Interrupt OB40 for Sub-Scan Events: For high-speed valves where opening takes less than 20 ms, configure the digital input card to trigger hardware interrupt OB40 on rising edge, capturing TIME_TCK inside OB40 immediately upon hardware firing.

  3. SFC64 Reset on CPU Restart: TIME_TCK resets to zero whenever the CPU executes a complete restart (OB100); avoid relying on pre-shutdown tick values across power cycles.

Engineer FAQ

How do I call SFC64 directly in a CFC chart without writing SCL?

Place TIME_TCK (SFC64) directly from the standard library onto the CFC chart. Connect its output to a sample-and-hold block or arithmetic subtraction block.

What is the difference between SFC64 and SFC1?

SFC64 returns a simple 32-bit millisecond tick counter for duration timing, whereas SFC1 (READ_CLK) returns calendar Date and Time (Year, Month, Day, Hour, Minute, Second, Millisecond).

Can I capture pulse times in Process Interrupt OB40?

Yes, calling SFC64 inside OB40 provides the exact timestamp when a physical digital input edge tripped the hardware interrupt, bypassing cyclic OB scan delays.

Technical References

Related Articles

Related Tags

  • SFC64
  • TIME_TCK
  • Pulse Measurement
  • Millisecond Timing
  • CFC Engineering
  • SCL Function Block
6 respuestas