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 properties as elementary function blocks. The user can create them in the programming languages FBD, LD, IL, and/or ST.

Parameter

"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.

Exception:

When dealing with generic ANY_BIT formal parameters, actual INT or DINT (not UINT and UDINT) parameters 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 need be assigned 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

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 is 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 a value and the function block/DFB is instanced more than once, then the following instances are run with the old value.

NOTE: An ANY_ARRAY_xxx input pin not connected will create automatically an hidden array of 1 element.

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 via the load and save instructions.

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.

NOTE: Nested DFBs are declared as private variables of the parent DFB. So their variables are also not accessible through programming, but trough the animation table.

Private variables are a supplement to IEC 61131-3.

Programming Notes

Attention should be paid to the following programming notes:

Declaration

Before calling a function block it must be declared in the variables editor.

Formal Form of CAL with a List of Input Parameters

With this type of call (call with formal parameter names), the function block is called using a CAL instruction which follows the instance name of the function block and a bracketed list of actual parameter assignments to the formal parameters. The assignment of the input formal parameter is made using the := assignment and the output formal parameter is made using the => assignment. The sequence in which the input formal parameters and output formal parameters are enumerated is not significant. The list of actual parameters may be continued immediately following a comma.

EN and ENO can be used for this type of call.

Function block call in the formal form of CAL with a list of input parameters:

or

CAL MY_COUNT (CU:=var1,
              R:=reset,
              PV:=100,
              Q=>out,
              CV=>current)

Calling the same function block in FBD:

It is not necessary to assign a value to all formal parameters (see also Parameter).

CAL MY_COUNT (CU:=var1, R:=reset, Q=>out, CV=>current)

Calling the same function block in FBD:

The value of a function block output can be stored and then saved by loading the function block output (function block instance name and separated by a full stop or entering the formal parameter).

Loading and saving function block outputs:

Informal Form of CAL with a List of Input Parameters

With this type of call (call without formal parameter names), the function block is called using a CAL instruction, that follows the instance name of the function block and a bracketed list of actual parameter for the inputs and outputs. The order in which the actual parameters are listed in a function block call is significant. The list of actual parameters cannot be wrapped.

EN and ENO cannot be used for this type of call.

Function block call in the informal form of CAL with a list of input parameters:

Calling the same function block in FBD:

With informal calls it is not necessary to assign a value to all formal parameters (see also Parameter).

This is a supplement to IEC 61131-3 and must be enabled explicitly.

An empty parameter field is used to omit a parameter.

Call with empty parameter field:

CAL MY_COUNT (var1, , 100, 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:

CAL and Load/Save the Input Parameters

Function blocks may be called with an instruction list consisting of loading the actual parameters, followed by saving into the formal parameter, followed by the CAL instruction. The sequence of loading and saving the parameters is not significant.

Only load and save instructions for the function block currently being configured are allowed between the first load instruction for the actual parameters and the call of the function block. All other instructions are not allowed in this position.

It is not necessary to assign a value to all formal parameters (see also Parameter).

CAL with Load/Save the input parameters:

Use of the Input Operators

Function blocks can be called using an instruction list that consists of loading the actual parameters followed by saving them in the formal parameters followed by an input operator. The sequence of loading and saving the parameters is not significant.

Only load and save instructions for the function block currently being configured are allowed between the first load instruction for the actual parameters and the input operator of the function block. All other instructions are not allowed in this position.

EN and ENO cannot be used for this type of call.

It is not necessary to assign a value to all formal parameters (see also Parameter).

The possible input operators for the various function blocks can be found in the table. Additional input operators are not available.

Input Operator

FB type

S1, R

SR

S, R1

RS

CLK

R_TRIG

CLK

F_TRIG

CU, R, PV

CTU_INT, CTU_DINT, CTU_UINT, CTU_UDINT

CD, LD, PV

CTD_INT, CTD_DINT, CTD_UINT, CTD_UDINT

CU, CD, R, LD, PV

CTUD_INT, CTUD_DINT, CTUD_UINT, CTUD_UDINT

IN, PT

TP

IN, PT

TON

IN, PT

TOF

Use of the input operators:

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".

E.g.

Calling the function block in the IL programming language:

CAL MY_CLOCK ()CAL MY_COUNT (CU:=MY_CLOCK.CLK1, R:=reset, PV:=100)
LD MY_COUNT.Q
ST out
LD MY_COUNT.CV
ST 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 one time.

    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 occurs when executing these 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.

CAL MY_COUNT (EN:=1, CU:=var1, R:=reset, PV:=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 output the updated 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 the block call.

Calling a function block with a VAR_IN_OUT variable in IL:

CAL 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:

LD V1

Loading a V1 variable in the accumulator

CAL InOutFB

Calling a function block with the VAR_IN_OUT parameter.

The accumulator now contains a reference to a VAR_IN_OUT parameter.

AND V2

AND operation on accumulator contents and V2 variable.

Error: The operation cannot be performed since the VAR_IN_OUT parameter (accumulator contents) cannot be accessed from outside the function block call.

Invalid call, example 2:

LD V1

Loading a V1 variable in the accumulator

AND InOutFB.inout

AND operation on accumulator contents and a reference to a VAR_IN_OUT parameter.

Error: The operation cannot be performed since the VAR_IN_OUT parameter cannot be accessed from outside the function block call.

The following function block calls are always valid:

Valid call, example 1:

CAL InOutFB (IN1:=V1,inout:=V2

Calling a function block with the VAR_IN_OUT parameter and assigning the actual parameter within the function block call.

Valid call, example 2:

LD V1

Loading a V1 variable in the accumulator

ST InOutFB.IN1

Assigning the accumulator contents to the IN1 parameter of the IN1 function block.

CAL InOutFB(inout:=V2)

Calling the function block with assignment of the actual parameter (V2) to the VAR_IN_OUT parameter.