03Getting started

Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK

The three program organization units of IEC 61131-3 and the one difference that matters: which of them remembers anything.

Lesson 3 of 3410 minIEC 61131-3

The POU

Everything you write for a PLC lives in a POU (program organization unit). The standard defines three, and the difference comes down to one question: does it keep its memory between scans?

Kind

Memory

Outputs

Example

FUNCTION

none

one value

SQRT, a 4-20 mA scaler

FUNCTION_BLOCK

kept per instance

several

TON, a motor block

PROGRAM

kept

bound to a task

the station's main program

FUNCTION

Values go in, one value comes out, and nothing is remembered. The same inputs always give the same result. Use it for maths.

FUNCTION_BLOCK

This is the key idea in IEC 61131-3. A block has memory per instance. When you write

VAR
  T1 : TON;
  T2 : TON;
END_VAR

you created two instances of TON, each with its own elapsed time and output. That is why a timer must be declared in VAR and cannot be used as a throwaway function. The instance is what remembers.

PROGRAM

The top level: bound to a task and executed cyclically. In this lab every example is one program, and its variables are wired to the process by name.

Declaration sections

VAR_INPUT  … END_VAR   (* comes from outside, read-only *)
VAR_OUTPUT … END_VAR   (* goes to the outside *)
VAR        … END_VAR   (* internal, kept between scans *)

In the lab, variables named Start Stop Level LSH LSL are wired to inputs, and Pump Valve Lamp HiAlm to outputs.

Try it in the lab

The lab runs in your browser: an ST editor, a virtual tank with a pump and a valve, a watch table, and export to Siemens SCL.

What to try

  1. Run the program and watch T1.ET and T2.ET in the watch table.
  2. They are independent even though they share a type: each instance has its own memory.
  3. Delete the declaration of T2 and run: you will get "undeclared variable".

Terms in this lesson

People find this lesson by searching

POU plcprogram organization unitfunction vs function blockFUNCTION_BLOCK structured textfunction block instanceVAR_INPUT VAR_OUTPUT

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