Implementing IEC Timers (SFB 4 TON / SFB 5 TP) in SCL for SIMATIC PCS 7 - Variable Assignment and Instance DB Architecture
In SIMATIC PCS 7 SCL programming, implementing standard IEC timers—such as on-delay timer SFB 4 (TON) or pulse timer SFB 5 (TP)—within custom function blocks requires declaring the timer as an internal static multi-instance variable (under 'VAR') and correctly mapping output parameters. A frequent coding error occurs when programmers write 'CYCLIC_TON.Q := outTon' (attempting to assign an output into the timer); the assignment must flow from the timer to the block outputs: 'outTon := CYCLIC_TON.Q;' and 'ElapsedTime := CYCLIC_TON.ET;'. Furthermore, declaring timers as multi-instances embeds their memory directly within the parent block's instance DB, maintaining PCS 7 modularity.
Table of Contents
- IEC Timers Architecture in S7-400 and PCS 7 (SFB 4 / SFB 5)
- Common SCL Coding Errors - Assignment Direction and Global DB Calls
- Multi-Instance Declaration and Calling Conventions in SCL
- Standard IEC System Function Blocks (SFBs) Comparison Table
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
IEC Timers Architecture in S7-400 and PCS 7 (SFB 4 / SFB 5)
In SIMATIC STEP 7 and PCS 7, timing functions are executed using standardized IEC 61131-3 System Function Blocks (SFBs) embedded in CPU firmware:
- SFB 4 (TON) - On-Delay Timer: Output Q energizes only after input IN has remained TRUE continuously for preset duration PT.
- SFB 5 (TP) - Pulse Timer: Output Q generates a fixed-duration pulse PT immediately upon a rising edge on IN, regardless of how long IN remains high.
- SFB 3 (TOF) - Off-Delay Timer: Output Q drops to FALSE duration PT after IN falls low.
- Deterministic Timebase:
* Unlike legacy S5 timers (T 1 to T 256) that rely on BCD integer formats and limited 16-bit registers, IEC timers evaluate elapsed time using standard 32-bit TIME data types (millisecond resolution from T#1ms to T#24d_20h_31m_23s_647ms).
* In PCS 7, timers should always execute as multi-instances inside parent function blocks.
Common SCL Coding Errors - Assignment Direction and Global DB Calls
Two classic coding mistakes frequently cause timer failures in SCL:
1. Reversed Assignment Direction:
- A programmer writes:
scl
CYCLIC_TON(IN := TagStart, PT := Tag_PresetTime);
CYCLIC_TON.Q := outTon; // ERROR!
CYCLIC_TON.ET := ElapsedTime; // ERROR!
- In SCL, A := B; assigns the value of expression B into variable A.
- CYCLIC_TON.Q is an output generated by the timer block. Writing CYCLIC_TON.Q := outTon overwrites the timer's internal calculation with the uninitialized state of outTon, causing outTon to remain permanently FALSE!
- The correct assignment must read:
scl
outTon := CYCLIC_TON.Q;
ElapsedTime := CYCLIC_TON.ET;
2. Calling Global DBs Inside Function Blocks:
- Programmers migrating from LAD often attempt: Status_DB.Global_TON.TON(...).
- In S7 SCL, referencing external global DBs inside reusable FBs violates modularity, breaks multi-instance encapsulation, and causes compilation errors if the symbol is not in the project symbol table.
Multi-Instance Declaration and Calling Conventions in SCL
The professional, certified method to implement an on-delay timer inside an SCL function block:
-
Declare the Timer as an Internal Static Variable (
VAR):
- Inside the block declaration header, place the timer underVAR:
scl FUNCTION_BLOCK FB 1700 TITLE = 'Custom Motor On-Delay Starter' VAR_INPUT Start : BOOL; PresetTime : TIME := T#5s; END_VAR VAR_OUTPUT MotorRun : BOOL; RemainingTime : TIME; END_VAR VAR // Multi-instance IEC timer embedding: myTon : SFB4; // SFB 4 is the system on-delay timer TON END_VAR -
Call the Timer and Map Outputs in Executable Logic (
BEGIN):
- Inside the procedural logic:
```scl
BEGIN
// 1. Call the timer instance:
myTon(IN := Start, PT := PresetTime);// 2. Assign timer outputs to block outputs: MotorRun := myTon.Q; RemainingTime := myTon.ET;END_FUNCTION_BLOCK
`` - This approach ensures that every timeFB 1700` is instantiated in a CFC chart, its timer memory is automatically allocated within that specific instance's data block without managing external DBs.
Standard IEC System Function Blocks (SFBs) Comparison Table
The following table details standard IEC timer and counter system function blocks in S7-400:
SFB 4TONIN : BOOL, PT : TIMEQ : BOOL, ET : TIMESFB 5TPIN : BOOL, PT : TIMEQ : BOOL, ET : TIMESFB 3TOFIN : BOOL, PT : TIMEQ : BOOL, ET : TIMESFB 0CTUCU : BOOL, R : BOOL, PV : INTQ : BOOL, CV : INTSFB 1CTDCD : BOOL, LD : BOOL, PV : INTQ : BOOL, CV : INTSFB 2CTUDCU, CD, R, LD, PVQU, QD, CVVerification
To confirm that the SCL timer block functions accurately:
1. Compile the SCL source in STEP 7. Verify compilation finishes with 0 errors, 0 warnings.
2. Insert the FB into a CFC chart, set PresetTime := T#3s, and compile charts.
3. Switch CFC to Test Mode (Ctrl + T).
4. Force Start = 1:
- Verify that ElapsedTime begins counting up from T#0ms towards T#3s.
- Verify that MotorRun remains 0 while counting.
- At exactly 3000 ms, verify that MotorRun transitions to 1.
5. Set Start = 0: verify that MotorRun resets immediately to 0 and ElapsedTime clears to T#0ms.
Limitations and Operational Precautions
When deploying IEC timers in SIMATIC PCS 7:
1. Calling Frequency and Cycle Time: IEC timers calculate elapsed time by querying the S7-400 operating system clock ticks. However, output evaluation occurs only when the block executes. Calling a timer in OB32 (1000 ms cycle) means timer resolution is quantized to 1 second. For sub-second accuracy, execute in OB35 (100 ms) or OB34 (200 ms).
2. Never Call SFB Timers Conditionally: If a timer block is placed inside an IF Condition THEN myTon(...); END_IF block, the timer will stop evaluating when the condition is FALSE, freezing the elapsed time. Always execute the timer call unconditionally, controlling it via the IN parameter.
3. PCS 7 Library Alternatives: In standard APL engineering, prefer standard PCS 7 timer blocks (Timer_P FB 1848 or TimeTrig) which feature built-in simulation, faceplate visualization, and pause controls.
Engineer FAQ
Why did 'CYCLIC_TON.Q := outTon' compile without an error if it was wrong?
In STEP 7 SCL, structure outputs can technically be written to in memory. The syntax was legally valid SCL code, but logically flawed because it overwrote the timer's calculated result.
What does ET stand for in SFB 4 TON?
ET stands for Elapsed Time (Verstrichene Zeit), a 32-bit TIME output indicating how much time has passed since input IN became TRUE.
Can I use standard S5 timers (S_ODT) in SCL for PCS 7?
While S5 timers are supported in SCL, they are strongly discouraged in PCS 7 because they consume global S7 timer numbers (T 1 to T 256) and lack multi-instance encapsulation.
How do I format time constants in SCL?
Time constants use the IEC syntax T#<hours>h_<minutes>m_<seconds>s_<milliseconds>ms, for example T#500ms, T#2s_500ms, or T#1h.
Technical References
- SCL for SIMATIC S7-300/400 Programming Manual - Section: Calling Function Blocks
- System Software for S7-300/400 System and Standard Functions Reference Manual (SFB 4 / SFB 5)
- Siemens Industry Online Support Entry 170002 - Coding IEC Timers in SCL
Related Articles
- Developing SCL Custom Motor Blocks for SIMATIC PCS 7 - Byte-Swapping, Status DWORD Formatting, and APL Block Icon Integration
- Developing Custom Motor Temperature Tripping Logic in SCL for SIMATIC PCS 7 - 6-RTD Combinations vs Standard Interlock Blocks
- Configuring Dynamic SFC Step Execution Times from WinCC in SIMATIC PCS 7
Related Tags
- PCS7_SCL
- IEC_Timers
- SFB4_TON
- SFB5_TP
- Multi_Instance
- SCL_Syntax
- Timer_Programming