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
- Why Standard Timers (S_PULSE, TimerP) Are Inadequate
- System Function SFC64 (TIME_TCK) Architecture and Rollover Dynamics
- Implementing the Measurement Algorithm in S7-SCL and CFC
- Pulse Measurement Method Comparison Matrix
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
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:
- Resolution Bounds: Standard S5 timers use BCD time bases (10 ms, 100 ms, 1 s) with low resolution and non-linear decay.
- 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):
- Data Format:
SFC64returns a 32-bit integer (TIMEdata type) representing elapsed milliseconds since CPU startup. - Range and Rollover: The counter counts from
T#0ms(0) up toT#24d20h31m23s647ms(2,147,483,647ms). Upon reaching this maximum positive integer value, the counter rolls over to0.
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:
SFC64 TIME_TCKSFC1 READ_CLKVerification
Verify the timing block through the following test sequence:
- Instantiate
FB_PulseTimerin a fast cyclic interrupt OB (e.g., OB35 at 10 ms or OB36 at 50 ms). - Trigger
StartPulsevia a test bit. Confirm thatMeasuring = TRUEandValid = FALSE. - Exactly 2,500 ms later, trigger
StopPulse. Confirm thatMeasuringdrops toFALSEandValidtransitions toTRUE. - Verify that
Elapsed_msdisplays2500(within +/- 1 scan cycle tolerance). - Simulate a rollover condition by initializing
StartTickto2147483000andStopTickto500; confirm thatElapsed_mscorrectly calculates1148ms without numeric underflow.
Limitations and Operational Precautions
Consider these operational rules when measuring pulse intervals in software:
-
Execution Cycle Bound: While
SFC64provides 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. -
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_TCKinside OB40 immediately upon hardware firing. -
SFC64 Reset on CPU Restart:
TIME_TCKresets 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
- STEP 7 V5.5 / V5.6 System and Standard Functions Manual: SFC64 TIME_TCK Reference (Entry ID: 109780813)
- How do you read out the system time of the CPU with SFC1 and SFC64? (Siemens Industry Support)
- SIMATIC Process Control System PCS 7 Advanced Process Library Manual
- S7-400 Programmable Controller: System Architecture and Real-Time Clock Principles
Related Articles
- Configuring First-In Detection (Feature Bit 31) and Interlock Diagnostics in PCS 7
- Troubleshooting S7-400 High CPU Load (95%) and Emergency Operation
- Fundamentals of Continuous PID Control in SIMATIC PCS 7: PV, SP, and LMN
Related Tags
- SFC64
- TIME_TCK
- Pulse Measurement
- Millisecond Timing
- CFC Engineering
- SCL Function Block