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.

Tags to create: Count — DINT.
Count := ABS(-5);

SQRT: square root.

Tags to create: Speed — REAL.
Speed := SQRT(9.0);

MIN: smaller of two values.

Tags to create: Count — DINT.
Count := MIN(3, 5);

MAX: larger of two values.

Tags to create: Count — DINT.
Count := MAX(3, 5);

LIMIT: lower bound, value, upper bound. Clamps the value to the bounds.

Tags to create: Speed — REAL.
Speed := LIMIT(0.0, Speed, 100.0);

MOD: integer remainder. Also available as the MOD operator.

Tags to create: 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.

Tags to create: Pulse — BOOL; StartPB — BOOL.
Pulse := RISING(StartPB);
Tags to create: 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.

Tags to create: 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.

Tags to create: 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.

Tags to create: 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.

Tags to create: Batch — COUNTER; StartPB — BOOL.
CTU(Batch, IN := StartPB, PRE := 10);
Tags to create: 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.

Tags to create: 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.

Tags to create: StartPB — BOOL.
IF StartPB THEN
  CALL_ROUTINE(Fill_Logic);
END_IF;

Related