ST functions and instructions
A function returns a value inside an expression. An instruction runs as its own statement, ending with a semicolon.
Math functions
Arguments are positional. Use the same numeric type throughout, with the whole-number literal allowance for REAL. MOD is DINT only; SQRT takes REAL and returns REAL. A negative square root or a zero divisor faults the statement.
ABS: absolute value.
Count — DINT.Count := ABS(-5);SQRT: square root.
Speed — REAL.Speed := SQRT(9.0);MIN: smaller of two values.
Count — DINT.Count := MIN(3, 5);MAX: larger of two values.
Count — DINT.Count := MAX(3, 5);LIMIT: lower bound, value, upper bound. Clamps the value to the bounds.
Speed — REAL.Speed := LIMIT(0.0, Speed, 100.0);MOD: integer remainder. Also available as the MOD operator.
Count — DINT.Count := MOD(7, 3);RISING and FALLING
RISING returns TRUE for one scan on a false-to-true change. FALLING does so on a true-to-false change. Each written call site has its own memory. Call it every scan for predictable edge detection; moving it into a conditional or repeating it in a loop changes which inputs it observes.
Pulse — BOOL; StartPB — BOOL.Pulse := RISING(StartPB);Pulse — BOOL; StartPB — BOOL.Pulse := FALLING(StartPB);TON, TOF and RTO
The first argument names a TIMER tag. IN is BOOL; optional PRE is DINT milliseconds. If PRE is omitted the instruction uses the tag’s existing PRE. The instruction must execute to update its state.
TON: on-delay
Accumulates while IN is true. DN becomes true at PRE; false IN clears its accumulated time and done state.
FillTimer — TIMER; StartPB — BOOL.TON(FillTimer, IN := StartPB, PRE := T#5s);TOF: off-delay
DN turns on with IN and stays on for PRE after IN turns off.
FillTimer — TIMER; StartPB — BOOL.TOF(FillTimer, IN := StartPB, PRE := T#250ms);RTO: retentive on-delay
Accumulates while IN is true and keeps ACC when IN turns false. RES clears it.
FillTimer — TIMER; StartPB — BOOL.RTO(FillTimer, IN := StartPB, PRE := T#1m30s);CTU, CTD and RES
CTU adds one on a rising IN; CTD subtracts one. Holding IN true does not count repeatedly. Use a COUNTER tag and an optional DINT PRE. DN indicates ACC has reached or exceeded PRE.
Batch — COUNTER; StartPB — BOOL.CTU(Batch, IN := StartPB, PRE := 10);Batch — COUNTER; StartPB — BOOL.CTD(Batch, IN := StartPB, PRE := 10);RES takes one TIMER or COUNTER tag and resets its accumulated state. Put it in an IF when the reset should happen only on a condition.
ResetPB — BOOL; Batch — COUNTER.IF ResetPB THEN
RES(Batch);
END_IF;CALL_ROUTINE
Create a routine named Fill_Logic in the same program first. Use its name as the argument, without quotes. The call runs it at this point, then returns to the next statement. The routine must be reachable from the program’s Main Routine to run. Recursive calls are refused.
StartPB — BOOL.IF StartPB THEN
CALL_ROUTINE(Fill_Logic);
END_IF;