Skip to content

11Expressions and operators

Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX

The ready-made IEC 61131-3 functions that replace long IF blocks: absolute value, square root, clamping, selection and rounding.

Lesson 11 of 3411 minIEC 61131-3

Functions every controller already has

The standard ships a library you neither declare nor import. Using it is shorter and clearer than a hand-written IF, and faster to execute.

Numeric functions

Function

What it does

Example

ABS(x)

absolute value

ABS(-7.5) is 7.5

SQRT(x)

square root

SQRT(16.0) is 4.0

MIN(a, b)

the smaller one

MIN(80.0, Level)

MAX(a, b)

the larger one

MAX(0.0, Level)

LIMIT(mn, x, mx)

clamp between two bounds

LIMIT(0.0, Level, 100.0)

Mind the argument order of LIMIT: minimum first, then the value, then the maximum. It replaces four lines:

(* instead of this *)
IF Out > 100.0 THEN Out := 100.0;
ELSIF Out < 0.0 THEN Out := 0.0;
END_IF

(* write this *)
Out := LIMIT(0.0, Out, 100.0);

SQRT belongs in flow measurement: a differential pressure flow transmitter gives a signal proportional to the square of flow, so real flow is the root of the reading.

Flow := FlowMax * SQRT(LIMIT(0.0, DP, 100.0) / 100.0);

Careful: SQRT of a negative number is a runtime fault. A MAX(0.0, x) in front protects you from noise that pushes the reading slightly below zero.

Selection functions

Function

What it does

SEL(G, IN0, IN1)

returns IN0 when G is false, IN1 when true

MUX(K, IN0, IN1, ... )

returns argument number K

(* vendor code: this does NOT run in the lab on this site *)
SetPoint := SEL(NightMode, 70.0, 40.0);
Recipe   := MUX(RecipeNo, 0.0, 25.0, 50.0, 75.0);

Conversion and rounding

TRUNC cuts the fraction off, ROUND (called REAL_TO_INT by several vendors) goes to the nearest integer:

Expression

Result

TRUNC(7.8)

7

ROUND(7.8)

8

TRUNC(-7.8)

-7

To send a value to an HMI field with one decimal, the usual trick is Disp := ROUND(Level * 10.0); and the HMI places the point.

A word on string functions

When you build a message or a batch number you need the string library:

Function

What it does

LEN(s)

length of the string

CONCAT(a, b)

join two strings

FIND(a, b)

position of b inside a, zero if absent

LEFT RIGHT MID

take part of a string

String handling is expensive on a controller. Put it in a slow task, or move it to the HMI or SCADA where it belongs.

What runs in the lab

Runs

Does not run

ABS MIN MAX LIMIT SQRT

SEL MUX TRUNC ROUND and all string functions

This lesson's program uses only the five supported ones. The rest is here so you recognise it when you open your vendor's environment.

Try it in the lab

The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.

What to try

  1. Run the program, press START and watch Error and AbsError: the first changes sign, the second stays positive.
  2. The lamp lights while the level is within 5 of 50%.
  3. Watch Command: it never goes above 100 or below 0, whatever the error.
  4. Change SetPoint to 30.0 and download again.

People find this lesson by searching

structured text standard functionsLIMIT function plcMIN MAX structured textSQRT flow calculation plcSEL MUX structured textTRUNC ROUND plcplc string functions CONCAT LEN

Course outline

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers