CODESYS Β· Track 02 Β· the program building blocks

POUs & the task configuration

A CODESYS program is built from POUs (Program Organization Units) of three types: PRG, FB and FUN. They differ in whether they retain data and how you create instances. The Task Configuration decides which POUs run, how often, and at what priority. Together, these settings determine how the application executes.

πŸ“‹ PRG🧠 FBβž— FUN
01 Β· The three POU types

Memory is the difference

Select each POU type to compare it. A FUNCTION takes inputs and returns a value without retaining memory between calls. A FUNCTION_BLOCK has its own data and can be instanced many times, each remembering its own state. A PROGRAM is like a single global FB instance: one copy, called by a task.

PRG
PROGRAM
A single global instance, called directly by a task. Holds state, but there's only one of it.
one instance
FB
FUNCTION_BLOCK
Behaviour + private data. Declare many instances; each keeps its own timers, counters, state.
many instances
FUN
FUNCTION
Stateless: the same inputs produce the same output, with no memory between calls. For example, SQRT().
no memory
02 Β· Task configuration

Choose the schedule and priority

A POU does nothing until a task calls it. The Task Configuration lists the tasks, each with a type, an interval and a priority (0 = highest). Higher-priority tasks preempt lower-priority ones, so keep the fast tasks short.

TaskTypeIntervalPriorityCalls POU
MotionTaskexternal eventon sync IRQ1PLC_Motion (PRG)
FastTaskcyclic4 ms5PLC_Fast (PRG)
MainTaskcyclic20 ms10PLC_PRG (PRG)
BackgroundTaskfreewheelingas fast as free15PLC_Bg (PRG)

Five task types you'll meet: cyclic (runs at a fixed interval), freewheeling (restarts as soon as it finishes, with no guaranteed rate), event (starts on a rising edge of a BOOL variable), external event (starts on a system event such as a fieldbus sync or hardware interrupt, if the device offers one), and status (runs while a flag is true). Each task can have a watchdog with its own time and sensitivity. When the task runs past the watchdog time for as many cycles as the sensitivity allows, the runtime raises an exception and halts the application, like the scan-time guard on other brands.

03 Β· Check yourself

Which POU type?

You need a reusable conveyor controller, dropped onto 10 conveyors, each holding its own state & timer. Which POU type?