A function block, unlike a function, may remember some values from the previous call(s), for example status information. These remembered values may be used in computations, and thus influence the result. The main difference between a function and a function block is, thus, the function block's capability to allocate memory for storage of values of some variables. Functions do not have this feature and their results are determined by the input parameters at the time a function is called. A function block may also return more than one result. (A function has one result only).
Function blocks can be compared to "black boxes" which perform a dedicated control functionality. They contain algorithms and data, so they can store information between the calls (this is where they differ from functions). They have a defined interface and inner variables hidden to the outside world. A typical example of a function block is a PID controller.
Function block declaration
FUNCTION_BLOCK name ... END_FUNCTION_BLOCK
The declaration part of a function block contains variable definitions necessary for the block's activity. The variables are used for information storage and processing. Each variable is defined by a variable name, and data type. The data type is specifying the size of the occupied place in the memory, and determines the way the variable is processed (to a certain extent). For variables definition, standard data types are available (BOOL, BYTE, INT, …). Usage of these types depends on what information is going to be stored in the variable (e.g. BOOL for yes/no information, INT for signed integer numbers, etc.). User is allowed to define his/her own data types. The location of variables in memory is managed by the Idekit Studio automatically. If necessary, the location in memory may be user-defined, too.
VAR_INPUT ... END_VAR
VAR_OUTPUT ... END_VAR
VAR_IN_OUT ... END_VAR
VAR ... END_VAR
VAR_EXTERNAL ... END_VAR
Assignment of initial values to the variables
VAR I: INT := 1; END_VAR;
The executive part of a function block follows the declaration, and contains commands and instructions.
Function block call
MY_FB( First_Index := 1, Last_Index := 5 );
MY_FB( 1, 5);
Function block example (DEBOUNCE):
VAR_INPUT //input variables
DB_TIME: TIME:= t#10ms; //default value of 10 ms
VAR_OUTPUT //output variables
VAR //local variables
DB_ON, DB_OFF: TON;
(* Function block body *)
DB_ON (IN:= IN, PT:= DB_TIME);
DB_OFF (IN:= NOT IN, PT:= DB_TIME);
DB_FF (S1:= DB_ON.Q, R:= DB_OFF.Q);
A user-specified function block inserted in a FUPLA program.
If the block used in program is linked to source code, which is in executable od library project, editor of this block can be opened by doubleclick on its instance. Editor of funkction block can be opened even during the debugging. Editor is then opened with automatically filled instance name.
Blocks created in FBD does not need any additional editing. However blocks defined in ST needs to contain correct definition of namespaces in ST code. Definition of namespaces needs to match resulting declaration of function block. This declaration can be found in properties of function block.
The variables en and eno are used to switch the function block to manual mode. By setting the input variable en to the value "False", the function block stops executing the computational algorithm and values can be written to the output variables regardless of the input variables. The variable "eno" copies the state of the input variable "en" and serves as an indication of automatic and manual mode.