Module 12 · Structured Text basics

Timers & counters in ST

In Structured Text, timers, counters and edge detectors are function block instances that you declare and call. Learn the call syntax, the one mistake that breaks most beginners' timers, and how to turn any ladder rung from Level 2 into ST.

≈ 40 min6 lessons1 hands-on labQuiz · pass 80 %Free

By the end of this module you can

  • Declare, call and read TON, TOF, TP, CTU, CTD, R_TRIG and F_TRIG instances
  • Explain why a function block must be called every scan
  • Convert ladder rungs with timers, counters and edges into ST
  • Use timers and counters in ST on Siemens, Rockwell, Mitsubishi and Logivyx
VAR
    T_Delay : TON;                 // 1. declare an instance
END_VAR

T_Delay(IN := Start_Req, PT := T#5s);   // 2. call it with named inputs
Motor := T_Delay.Q;                     // 3. read its outputs
Elapsed := T_Delay.ET;
BlockInputsOutputs
TON / TOF / TPIN, PTQ, ET
CTUCU, R, PVQ, CV
CTDCD, LD, PVQ, CV
CTUD (IEC)CU, CD, R, LD, PVQU, QD, CV
R_TRIG / F_TRIGCLKQ

Each instance has its own memory, so two timers need two instances. Outputs read after the call show this scan's result; read before the call, they show last scan's.

A function block only does its work when it is called. Put a timer call inside an IF and, once the IF is false, the timer is simply frozen: Q and ET stay as they were. This is the most common ST timer bug.

RuleCall every timer and counter exactly once per scan, outside IF and CASE, and feed the condition into its input. The traffic light in module 11 is an exception that works because some branch calls the same timer on every scan.

Light := Light XOR Light_Edge.Q; is the whole toggle from module 07 in one line: when the edge is TRUE the light flips, otherwise it stays.

LadderStructured Text
XIC A, XIC B → OTE CC := A AND B;
branch (A / B), XIO D → OTE CC := (A OR B) AND NOT D;
XIC A → OTL C · XIC B → OTU CIF A THEN C := TRUE; END_IF;
IF B THEN C := FALSE; END_IF;
XIC A → TON T1 5 s · XIC T1.Q → OTE CT1(IN := A, PT := T#5s);
C := T1.Q;
OSR A (instance A_R) → raw ST N := N + 1A_R(CLK := A);
IF A_R.Q THEN N := N + 1; END_IF;
XIC P → CTU C1 PV 6, reset RC1(CU := P, R := R, PV := 6);

Every ladder simulator in Level 2 has a Show as Structured Text button that does this conversion for you, and the Logivyx Ladder Designer has View Generated ST…. Here is the start-up warning from module 08, written directly in ST, with a run-on fan added:

VAR T_Delay : TON; C_Batch : CTU; Edge : R_TRIG; END_VAR
T_Delay(IN := Start_Req, PT := T#5s);
C_Batch(CU := Part_PE, R := Reset_PB, PV := 6);
Edge(CLK := PB1);

Logivyx: TON, TOF, TP, CTU, CTD, R_TRIG, F_TRIG (and PID, motion and SQL blocks). Declare one instance per line in VAR. ET is in milliseconds; PT takes a TIME literal or a number of milliseconds. Pass every input in each call, e.g. T1(IN := FALSE, PT := T#5s); to reset.

// Multi-instance in an FB (Static: T_Delay : TON_TIME)
#T_Delay(IN := #Start_Req, PT := T#5s);
#Motor := #T_Delay.Q;
// Single instance with its own DB
"T_Delay_DB".TON(IN := "Start_Req", PT := T#5s);
#C_Batch(CU := #Part_PE, R := #Reset_PB, PV := 6);

In SCL, IEC timers are TON_TIME (shown as TON), TOF, TP, TONR; counters CTU, CTD, CTUD with selectable integer type. Edges: R_TRIG / F_TRIG instances. The instance is either a Static variable of the calling FB or a separate instance DB.

// T_Delay is a FBD_TIMER tag, C_Batch a FBD_COUNTER tag
T_Delay.TimerEnable := Start_Req;
T_Delay.PRE := 5000;                  // ms
TONR(T_Delay);
Motor := T_Delay.DN;

C_Batch.CUEnable := Part_PE;
C_Batch.PRE := 6;
C_Batch.Reset := Reset_PB;
CTUD(C_Batch);

Logix ST uses the function-block versions TONR, TOFR, RTOR and CTUD: set the members of the instance tag, then call the instruction with the tag. Edges: OSRI / OSFI with .InputBit and .OutputBit. The ladder TON/TOF/CTU with TIMER/COUNTER tags are not used in ST.

// IEC function blocks from the GX Works3 library, instances as labels
TON_Delay(IN := Start_Req, PT := T#5s, Q => Motor, ET => Elapsed);
CTU_Batch(CU := Part_PE, RESET := Reset_PB, PV := 6, Q => Batch_Full, CV => Count);

GX Works3 ST calls IEC function blocks with inputs := and outputs =>; check the exact pin names in the library (counter reset is RESET in some versions). Timer devices T can also be driven from ST with the OUT_T instruction. Edge FBs: R_TRIG, F_TRIG.

Use PLC_Trainer. Same signals as the module 08 and 09 labs: PB1 = START, SW1 = stop circuit OK, Q1 motor, Q2 horn, Q3 fan, PB2 = part, PB3 = reset, Q4 = batch full, N1 = count.

Lab

L12_ST_Timers

  1. New ST routine L12_ST_Timers. Declare inputs, outputs (Q1–Q4, N1) and in VAR: Run_Req : BOOL;, T_Warn : TON;, T_Fan : TOF;, C_Batch : CTU;
  2. Write the start-up warning and fan run-on from this module, using the trainer names.
  3. Add the batch counter: C_Batch(CU := PB2, R := PB3, PV := 6);, Q4 := C_Batch.Q;, N1 := C_Batch.CV;
  4. Validate, Auto-bind by Name, Save & Close. Call it from Main instead of the earlier labs. Save, F5.
  5. Check the behaviour matches your ladder version from module 08/09.
  6. Deliberately move T_Fan(...) inside an IF Q1 THEN … END_IF;, run, and watch the fan never stop. Put it back.