Elementary Function Block
Elementary function blocks have internal states. If the inputs have the same values, the value on the output can have another value during the individual operations. For example, with a counter, the value on the output is incremented.
Function blocks can have several output values (outputs).
Derived Function Block
Derived function blocks (DFBs) have the same characteristics as elementary function blocks. The user can create them in the programming languages FBD, LD, IL, and/or ST.
Parameters
"Inputs and outputs" are required to transfer values to or from function blocks. These are called formal parameters.
The current process states are transferred to the formal parameters. They are called actual parameters.
The following can be used as actual parameters for function block inputs:
Variable
Address
Literal
The following can be used as actual parameters for function block outputs:
Variable
Address
The data type of the actual parameters must match the data type of the formal parameters. The only exceptions are generic formal parameters whose data type is determined by the actual parameter.
When dealing with generic ANY_BIT
formal parameters,
actual parameters of the INT
or DINT
(not UINT
and UDINT
) data types
can be used.
This is a supplement to IEC 61131-3 and must be enabled explicitly.
Example:
Allowed:
AND (AnyBitParam := IntVar1, AnyBitParam2 := IntVar2);
Not allowed:
AND_WORD (WordParam1 := IntVar1,
WordParam2 := IntVar2);
(In this case, AND_INT
must be used.)
AND_ARRAY_WORD (ArrayInt, ...);
(In this case an explicit type conversion must be carried
out using INT_ARR_TO_WORD_ARR (...);
.)
Not all formal parameters must be assigned with a value. The formal parameter types that must be assigned a value are in the following table:
Parameter type |
EDT |
STRING |
ARRAY |
ANY_ARRAY |
IODDT |
Device DDT |
STRUCT |
FB |
ANY |
---|---|---|---|---|---|---|---|---|---|
EFB: Input |
- |
- |
- |
- |
/ |
/ |
- |
/ |
- |
EFB: VAR_IN_OUT |
+ |
+ |
+ |
+ |
+ |
/ |
+ |
/ |
+ |
EFB: Output |
- |
- |
+ |
+ |
+ |
/ |
- |
/ |
+ |
DFB: Input |
- |
- |
- |
- |
/ |
+ |
- |
/ |
- |
DFB: VAR_IN_OUT |
+ |
+ |
+ |
+ |
+ |
+ |
+ |
/ |
+ |
DFB: Output |
- |
- |
+ |
/ |
/ |
/ |
- |
/ |
+ |
+ Actual parameter required |
|||||||||
- Actual parameter not required, it's the general rule, but there are exceptions for some FFBs, for instance when some parameters are used to characterize the information we want to be given by the FFB. |
|||||||||
/ not applicable |
If no value is allocated to a formal parameter, then the initial value will be used for executing the function block. If no initial value has been defined then the default value (0) is used.
If a formal parameter is not assigned with a value and the function block/DFB is instanced more than once, then the following instances are run with the old value.
Public Variables
In addition to inputs and outputs, some function blocks also provide public variables.
These variables transfer statistical values (values that are not influenced by the process) to the function block. They are used for setting parameters for the function block.
Public variables are a supplement to IEC 61131-3.
The assignment of values to public variables is made via their initial values or assignments.
Example:

Public variables are read via the instance name of the function block and the names of the public variables.
Example:

Private Variables
In addition to inputs, outputs and public variables, some function blocks also provide private variables.
Like public variables, private variables are used to transfer statistical values (values that are not influenced by the process) to the function block.
Private variables can not be accessed by user program. These type of variables can only be accessed by the animation table.
Private variables are a supplement to IEC 61131-3.
Programming Notes
Attention should be paid to the following programming notes:
Functions blocks are only executed if the input EN = 1 or is not used.
The assignment of variables to
ANY
orARRAY
output types must be made using the=>
operator.Assignments cannot be made outside of the function block call.
The instruction
My_Var := My_SAH.OUT;
is invalid, if the output
OUT
of theSAH
function block is of typeANY
.The instruction
Cal My_SAH (OUT=>My_Var);
is valid.
Special conditions apply when using VAR_IN_OUT variables.
The use of function blocks consists of two parts in ST:
calling the function block
There are two ways of calling a function block:
Formal call (calling a function with formal parameter names)
This way variables can be assigned to outputs using the
=>
operator.Informal call (call without formal parameter names)
Function block/DFB instances can be called multiple times; other than instances of communication EFBs, which can only be called once.
Declaration
Before calling a function block it must be declared in the variables editor.
Formal Call
With formal calls (calls with formal parameter names), the function block is called using an instruction sequence made from the function blocks instance names followed a bracketed list of actual parameter assignments to the formal parameters. Assign input formal parameters using := operator, and for output formal parameters using the => operator. The sequence in which the input formal parameters and output formal parameters are enumerated is not significant.
EN
and ENO
can be used for this type of
call.
Calling a function block with formal parameter names:

Calling the same function block in FBD:

Assigning the value of a function block output is made by entering the actual parameter name, followed by the assignment instruction := followed by the instance name of the function block and loading the formal parameter of the function block output (separated by a full-stop).
For example,
MY_COUNT (CU:=var1, R:=reset, PV:=100 + value);
Q := MY_COUNT.out ;
CV := MY_COUNT.current ;
It is not necessary to assign a value to all formal Parameters).
MY_COUNT (CU:=var1, R:=reset, Q=>out, CV=>current);
Calling the same function block in FBD:

Informal Call
With informal calls (call without Formal parameter names), the function block is called using an instruction made from the function block instance names, followed by a bracketed list of the actual parameters for the inputs and outputs. The order in which the actual parameters are listed in a function block call is significant.
EN
and ENO
cannot be used for this
type of call.
Calling a function block without formal parameter names:

Calling the same function block in FBD:

With informal calls it is not necessary to assign a value
to all formal Parameters). This does not apply
for VAR_IN_OUT
variables, for input parameters with
dynamic lengths and outputs of type ANY
. It must
always be assigned a variable.
This is a supplement to IEC 61131-3 and must be enabled explicitly.
An empty parameter field is used to skip a parameter.
Call with empty parameter field:
MY_COUNT (var1, , 100 + value, out, current) ;
Calling the same function block in FBD:

An empty parameter field does not have to be used if formal parameters are omitted at the end.
MY_COUNT (var1, reset) ;
Calling the same function block in FBD:

Calling a Function Block without Inputs
Even if the function block has no inputs or the inputs are not to be parameterized, the function block should be called before its outputs can be used. Otherwise the initial values of the outputs will be transferred, i.e. "0".
For example:
Calling the function block in ST:
MY_CLOCK () ;MY_COUNT (CU:=MY_CLOCK.CLK1, R:=reset, PV:=100,
Q=>out, CV=>current) ;
Calling the same function block in FBD:

Multiple Function Block Instance Call
Function block/DFB instances can be called multiple times; other than instances of communication EFBs, these can only be called once.
Calling the same function block/DFB instance more than once makes sense, for example, in the following cases:
If the function block/DFB has no internal value or it is not required for further processing.
In this case, memory is saved by calling the same function block/DFB instance more than once since the code for the function block/DFB is only loaded once.
The function block/DFB is then handled like a "Function".
If the function block/DFB has an internal value and this is supposed to influence various program segments, for example, the value of a counter should be increased in different parts of the program.
In this case, calling the same function block/DFB means that temporary results do not have to be saved for further processing in another part of the program.
EN and ENO
With all function blocks/DFBs,
an EN
input and an ENO
output can
be configured.
If the value of EN
is equal
to "0", when the function block/DFB is called, the algorithms defined
by the function block/DFB are not executed and ENO
is set to "0".
If the value of EN
is equal
to "1", when the function block/DFB is invoked, the algorithms which
are defined by the function block/DFB will be executed. After the
algorithms have been executed successfully, the value of ENO
is set to "1". If an error occurred while executing the algorithms, ENO
is set to "0".
If the EN
pin
is not assigned a value, when the FFB is invoked, the algorithm defined
by the FFB is executed (same as if EN
equals to "1").
If ENO
is set to "0" (results from EN
=0 or an error during execution), the outputs of the function block/DFB
retain the status from the last cycle in which they were correctly
executed.
The output behavior of the function blocks/DFBs does
not depend on whether the function blocks/DFBs are called without EN
/ENO
or with EN
=1.
If EN
/ENO
are used, the function
block call must be formal. The assignment of variables to ENO
must be made using the =>
operator.
MY_COUNT (EN:=1, CU:=var1, R:=reset, PV:=100 + value,
ENO=>error, Q=>out, CV=>current) ;
Calling the same function block in FBD:

VAR_IN_OUT-Variable
Function blocks are
often used to read a variable at an input (input variables), to process
it and to restate the altered values of the same variable (output
variables). This special type of input/output variable is also called
a VAR_IN_OUT
variable.
The following special
features are to be noted when using function blocks/DFBs with VAR_IN_OUT
variables.
All
VAR_IN_OUT
inputs must be assigned a variable.VAR_IN_OUT
inputs may not have literals or constants assigned to them.VAR_IN_OUT
outputs may not have values assigned to them.VAR_IN_OUT
variables cannot be used outside of the function block call.
Calling a function block with VAR_IN_OUT
variable
in ST:
MY_FBLOCK(IN1:=V1, IN2:=V2, IO1:=V3, OUT1=>V4, OUT2=>V5);
Calling the same function block in FBD:

VAR_IN_OUT
variables cannot be used
outside the function block call.
The following function block calls are therefore invalid:
Invalid call, example 1:
|
Assigning the variables Error: The operation cannot be executed since the |
Invalid call, example 2:
|
Assigning a Error: The operation cannot be executed since the |
The following function block calls are always valid:
Valid call, example 1:
|
Calling a function block with the |
Valid call, example 2:
|
Calling a function block with the |