-

Instantiation: Program and FB Instances

This topic contains the following sections:

Function block instantiation

The IEC 61131-3 standard defines function block instantiation. Instantiation means, a function block type is defined once and can be called (instantiated) several times. This applies to all FBs (user-defined POUs as well as library FBs, such as IEC standard FBs, firmware FBs, user library FBs).

Why FB instantiation? A function block has an internal memory where it stores its own processing data (local variables). As a consequence, the output values calculated by the FB depend on the internally stored values. The same input values applied to an FB instance do not necessarily deliver the same results in another FB instance. Therefore, it is necessary to store the internal data of the FB to a separated memory area each time the function block is processed, i.e., for each FB instance. To uniquely identify each FB instance and to clearly separate its memory area, instance names are used. The instance name of the function block has to be declared in the local 'Variables' table of the POU where the FB is called.

The following applies:

  • Function blocks can be instantiated in other function blocks or in program POUs. Calling FBs in function POUs is not possible.
  • Safety-related FBs can only be called in safety-related code.

Example for the instantiation of a user-defined function block

Note
In standard POUs, FB instance-related I/O variables can be realized as 'Instance' variables. Refer to topic "Instance-related Located Variables".

Program instantiation

A program POU is only executed on the controller if it has been assigned to a task. When assigning a program to a task, a program instance name has to be defined. Using this instance name, the instance and its controller memory area, where the program stores its instance-related data, can be uniquely identified.
The scheduling for the program execution is determined by the properties of the task to which the program instance belongs.

The instantiation of a program is done by assigning it to a task in the 'Tasks and Events' editor. To open the 'Tasks and Events' editor, double-click the 'PLCnext' node in the PLANT.

Procedures:
Safety-related and standard (non-safety-related) code is strictly distinguished in PLCnext Engineer. If a Safety PLC is included in your project, the following applies:

Note
PLCnext Engineer supports program-global variables. A program-global variable is declared in a program POU using the keyword 'Program'. The difference between a local I/O variable and a program-global I/O variable is that the program-global variable is also accessible from FB instances that are called from the program POU. For that purpose, the variable must be declared with the 'External' keyword in the called FB. Refer to the topic "Program-global variable becomes instance-related 'Program' variable" for details.

Example for the instantiation of a non-safety-related IEC program in the standard controller

Overview: relation of types and instances

The figure below illustrates the relationship between program and function block types which are defined the COMPONENTS area and their instances in the project.

  • Double-clicking a POU type in the 'Programming' category of the COMPONENTS area opens the code editors (textual and/or graphical) and the variables table (arrows 1).
  • Function block POUs are instantiated by calling the FB via its instance name in another FB or program (dashed arrows 2).
  • Double-clicking the 'PLCnext' node in the PLANT opens the PLC runtime editors used to configure the tasks and events (arrow 3).
    In the 'Tasks and Events' editor, program POUs can be instantiated by assigning them to a task (dashed arrow 4).
    By the assignment, a program instance with a program instance name is created (dashed arrow 5).

For the safety-related application executed by the integrated Safety PLC, the following applies: The Safety PLC executes exactly one task in which exactly one safety-related program must be instantiated. Both the safety-related task and program instance are created automatically on insertion of a controller with a Safety PLC in the PLANT. Further safety-related programs can be created in the COMPONENTS area but they are not executed. Safety-related FBs can only be called in other safety-related programs/FBs.