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