TIA PRO2 08 FC FB Multi Instances
TIA PRO2 08 FC FB Multi Instances
Contents 8
Functions, Function Blocks and Multiple Instances ........................................... 8-2
8.1. Task Description: Programming Re-usable Blocks ............................................................. 8-3
8.2. Local and Global Operands .................................................................................................. 8-4
8.3. Structured Programming and Re-usable Blocks................................................................... 8-5
8.3.1. Block Properties: Compilation ............................................................................................... 8-6
8.4. Properties of Functions ......................................................................................................... 8-7
8.4.1. Properties of Function Blocks ............................................................................................... 8-8
8.5. Parameter Visibility / Predefinition (1) ................................................................................... 8-9
8.5.1. Parameter Visibility / Predefinition (2) ................................................................................. 8-10
8.6. Instance Formation of Function Blocks ............................................................................... 8-11
8.6.1. Creating Instances of IEC-Functions in a Global Data Block ............................................. 8-12
8.6.2. Passing an Instance as a Parameter .................................................................................. 8-13
8.7. Exercise 1: "FC_Fault": Using a Global-DB Variable for the IEC-Timer-Instance .............. 8-14
8.7.1. Exercise 2: Programming "FC_Fault" re-usable using a Parameter Instance .................... 8-15
8.8. Structure of the Multiple Instance Model ............................................................................ 8-16
8.9. Changing the Block Call ...................................................................................................... 8-17
8.10. Exercise 3: Programming "FB_Statistic" as Re-usable ...................................................... 8-18
8.10.1. Exercise 4: Programming "FB_Signal" as Re-usable ......................................................... 8-19
8.10.2. Exercise 5: Replacing the Call of "FC_Signal" with "FB_Signal" ........................................ 8-20
8.11. Additional Information ......................................................................................................... 8-21
8.11.1. Local, Temporary Variables ................................................................................................ 8-22
8.11.2. Local Data Stack ................................................................................................................. 8-23
Task Description
According to the IEC61131-3 standard, global operands must not be used within FCs and FBs,
that is, no inputs, outputs, memory bits, data block variables, SIMATIC timers / counters. Allowed
are only local operands, that is, only parameters and local variables (tags).
The communication of the block with its "outside world" occurs exclusively through the interface
of the block or only through the input, output and in/out parameters. This also means that the
block works with no other operands than with those that are passed formal operands during the
block call.
This ensures that a block is "re-usable", that is, can also be integrated in other programs
unchanged.
• Process image for • interface for data exchange between calling and called block
inputs and outputs • temporary storage in the L-stack for FCs or storage in the IDB for FBs
• can be used in FCs / FBs
• I / O peripherals
Temporary Variables (Temp)
• are overwritten after the block is executed
• Memory bits • temporary storage in the local data stack (L-stack)
• can be used in OBs / FCs / FBs
• Variables in DBs Static Variables (Static)
• retain their value after the block is executed
• S5-Timers and Counters • permanent storage in instance data block (IDB)
(not for S7-1200)
• can only be declared in FBs
Constants (Constant)
• Constants • read-only as well as only symbolic access
• no memory usage
• can be used in OBs / FCs / FBs
Global Operands
Global operands are valid throughout the entire S7 program. Accordingly, every code (logic) block
(OB, FC, FB) can access these operands.
Global operands include inputs, outputs, memory bits, SIMATIC timers, SIMATIC counters,
constants and variables which are declared in global data blocks.
Local Operands
Local operands are only valid in the block in which they were declared in the declaration part.
Accordingly, only this block can access them.
• Formal Parameters
Formal parameters form the interface between the calling and the called block (FC, FB).
They are used to realize a data exchange between the calling and the called block.
• Temporary Variables
Temporary variables can be declared in every code (logic) block (OB, FC, FB) and are
managed in the local data stack of the CPU. Accordingly, they only retain their values while
the block is being executed. For that reason, it is important that in the current cycle, a write
access must have taken place on the temporary variable in the block before a read access
can take place. They are, for example, unsuitable as auxiliary variables for edge evaluations
or to store quantities. They are, in fact, used to store intermediate results, such as, for
complex calculations or format conversions.
• Static Variables
Static variables can only be declared in FBs and are stored in the associated instance data
block. Accordingly, these variables retain their value even after the FB is executed.
• Constants
Constants are fixed values which have a read-only access and which do not take up any
memory space.
"iDB_Motor" "iDB_Controller"
Modularization of the entire task:
"FB_ "FB_
Partial tasks are solved in separate "TON"
Motor" Controller"
programs or blocks "OB_
Cyclic_A"
Parameter assignment enables "iDB_Valves"
flexible usage
"FB_ "FC_
"BLKMOV"
• Example: Drilling cycle with Valves" Limit"
changeable depth
Advantages
• The blocks for the partial tasks can be created and tested independent of one another.
• With the help of parameters, blocks can be designed so that they are flexible. That way, for
example, a drilling cycle can be created that has the coordinates and the depth of the drilling
hole passed on to it by means of parameters.
• Blocks can be called as often as is required in different locations with different parameter
sets, that is, they can be reused.
• "Re-usable" blocks for special tasks can be delivered in pre-designed libraries.
Re-usable
Cannot
nicht be re-used
Wiederverwendbar
In the Properties of FCs and FBs, you can check whether the block was programmed as re-
usable.
Object is Library-conformant:
The block is re-usable and so can be called several times without a problem. It can also be used
in other projects/devices without adjustments and can be implemented with the help of libraries.
Warning or Error:
The use of the block in other devices or projects can lead to problems or requires adjustments in
the new project or in the block itself. Calling the block several times in the same program may not
be possible.
Parameter-assignable blocks:
Function FC10
• As many input, output and
in/out parameters as required in On1 BOOL
• Without memory, that is, only in On2 BOOL
temporary variables out Off BOOL
temp …
• Constants Program const …
execution
...
IEC 61131-3-conforming:
FC 10 &
• As many input parameters as required Off #On1 IN2
%I 0.0 On1 %Q8.4
#On2 IN1 OUT #Off
• Only one output parameter RET_VAL %I 0.1 On2
• No access to global variables, global ...
constants and absolute operands
• Deliver the identical result for the
same input parameters
Overview
Functions represent parameter-assignable blocks without memory. In STEP 7 they can have as
many input parameters, output parameters and in/out parameters as are required. Additionally,
constants that are local in the block can be created for S7-1200/1500.
Functions have no memory; no separate, permanent data area for storing results exists.
Temporary results that occur during function execution can only be stored in the temporary
variables of the respective local data stack.
Application
Functions are primarily used when function values are to be returned to the calling blocks (for
example, mathematical functions, single control with binary logic operation).
IEC-61131-Conforming Functions
If IEC 61131-3-conforming functions are to be created, then the following rules must be observed:
• Functions can have as many input parameters as is required. They can, however, only return
one result to the output parameter RET_VAL.
• Global operands can neither be read nor written within functions.
• No instances of function blocks can be called within functions.
• Because of the missing "memory", the returned result of a norm-conforming function is solely
dependent on the values of the input parameter. For identical values of the input parameter, a
function also returns the identical result.
It is therefore up to the programming person to create norm-conforming functions or to do the
block programming and structuring in STEP 7 as it is in STEP 5.
Overview
Function blocks (FB) are blocks of the user program and represent logic blocks with memory
according to the IEC Standard 61131-3. They can be called by OBs, FBs and FCs.
In function blocks, as many input, output and in/out parameters, static and temporary variables as
are required can be declared, as well as constants that are local in the block for S7-1200/1500.
Unlike FCs, FBs are instantiated, that is, an FB is assigned its own private data area in which the
FB can "remember" process states from call to call, for example. In the simplest form, this private
data area is its own DB, the so-called instance DB.
"Memory"
The programming person has the opportunity to declare static variables in the declaration section
of a function block. The function block can "remember" information from call to call in these
variables.
The ability of a function block to "remember" information over several calls is the essential
difference to functions.
Application
With the help of this "memory", a function block can implement counter and timer functions or
control process units, such as processing stations, drives, boilers etc., for example.
In particular, function blocks are well suited for controlling all those process units whose
performance depends not only on outside influences but also on internal states, such as
processing step, speed, temperature etc.
When controlling such units, the internal status data of the process unit are then copied to the
static variables of the function block.
1 2
3
4
In the example you can see that the parameters "Sensor1" and "Sensor2" have the property
“Hide”. For that reason, they are not displayed during the call Picture (1). In addition, the
parameter "Sensor1" was preassigned with the actual parameter "B_Bay1" Picture (2) and given
the property “Show if parameter assigned on block call is not identical to the predefined actual
parameter”. Since the actual parameter of "Sensor_1" is changed in Picture (3) vis-à-vis Picture
(2), it is no longer hidden in Picture (4).
FB instance
FB x
Count
stat Bay_1_count FBx
stat Bay_2_count FBx values for
stat Bay_3_count FBx
FBy Bay 1
Count
• Multiple instances
values for
Bay 3
What is an Instance?
The concept of the instantiation of function blocks has importance and forms the fundamental
difference to functions. The creation of tags within a high level language, such as, "C" specifying
the tag name and the data type is called "instantiation" or "instance formation".
Just like tags, function blocks are also "instantiated". Only through the assignment of its own data
area in which the values of the block parameter as well as the static variables are stored does a
function block become an executable unit (FB instance).
The control of a physical process unit, such as, a drive or a boiler then takes place with the help
of an FB instance, that is, a function block with associated data area. The relevant data for this
process unit is then stored in this data area.
The formation of an instance, that is, the assignment of its own memory area during an FB call
can occur in several ways in STEP 7:
− by explicitly specifying a so-called instance data block during the call of the function block.
− by explicitly declaring instances of an IEC function within a global data block
− by explicitly declaring a parameter instance and transfer the instance as an in/out
parameter,
− by explicitly declaring instances of a function block within a higher-level function block
(multiple instance model).
Advantages
• In the call of FBs, no measures for saving and administrating local data are necessary except
for the assignment of instance DBs.
• A function block can be used several times due to the instance concept. If, for example,
several drives of the same type are to be controlled, then this takes place using several
instances of an FB. The status data of the individual drives are stored in the static variables of
the FB.
Application
With that, the instance data of IEC counters and timers used within a global data block (see
picture).
However, it must be noted that because of this the calling Block (in the example "FC_Counter") is
not IEC-conform and with that is not re-usable, since even the passing of IEC counter instances,
represents the use of global operands.
• Single instance
• Instance in Global–DB
• Multiple instance
• Parameter instance
Horizontally split
editor area
Task
Using drag & drop, you are to replace the separate single instance in "FC_Fault" with an instance
which was previously declared in the global "DB_Conveyor".
Note:
With this change, the block is not yet re-usable. This will be realized in the next exercise.
What to Do:
1. In "DB_Conveyor", create the variable "instance_IECTimer" shown in the picture.
2. Horizontally split the editor area (see picture) so that the two blocks "DB_Conveyor" and
"FC_Fault" are displayed simultaneously.
3 In "FC_Fault", using drag & drop, replace the separate single instance-DB with the Global-
DB-Instance which you previously declared in "DB_Conveyor".
4. Transfer all modified blocks into the CPU and test the program.
5. Save your project.
Update FC call
1xR
Task
In "FC_Fault", you are to replace the Global-DB-Instance with a Parameter-Instance which then is
once again assigned with the Global-DB-Instance during the call of "FC_Fault".
Realize an automatic parameter assignment of the parameter instance by setting the Properties
of the parameter.
Note:
With this change, the block is re-usable.
What to Do:
1. Open the "FC_Fault" block.
2. Through the context menu of the ON-Delay, open the "Call options" function via the "Change
instance…" function.
3. Change the single instance into a parameter instance with the name "instance_IECTimer".
4. Select the parameter instance in the interface of the block and open the Properties in the
Inspector window.
5. As predefined "Actual Parameter", specify the variable "DB_Conveyor".instance_IECTimer.
6. Update the call of the function in "OB_Cycle".
Note: The new parameter is automatically linked to the variable
"DB_Conveyor".instance_IECTimer.
7. Transfer all modified blocks into the CPU and test the program.
8. Save your project.
"Instance-x"
FBx FBy
...
OB, FB or FC stat Drill FBy Algorithms
for drilling
stat Motor_1 FBz Data for
stat Motor_2 FBz Drill
.
CALL FBx,
"instance-x" CALL Drill
. Data
. FBz for Motor_1
CALL Motor_1
. Control of a
motor
CALL Motor_2
Data
for Motor_2
• Single instance (DB)
• Instance in Global–DB
• Parameter instance
• Multiple instance also possible Instance of FBx
Advantages
The use of the multiple instance model has the following advantages:
• The individual instances do not require their own data block every time. Within a call hierarchy
of function blocks, only one instance DB is used in the call of the "outer" function block.
• The multiple instance model "welds" a function block and an instance data area into one
object (FB instance), that can also be handled as one unit. The programming person does not
have to take care of the management (creation, addressing) of the individual instance data
areas. He must simply provide an instance DB for the "outer" FB.
• The multiple instance model supports an object-oriented programming style.
In order to replace the call of a block with another block call, you can open a selection list of all
FCs and FBs at the calling point by double-clicking on the name of the already called block.
Advantage:
If the newly called block has the same formal parameters, it retains its actual parameters and
doesn’t have to be resupplied.
Change to Multi-Instance
1xR
Task
In "FB_Statistic", the counting of the parts transported to Bay 1, 2 or 3 is implemented with IEC
counters whose data is stored in individual instances.
As previously with the "FC_Fault" block, "FB_Statistic" is now also to be modified in such a way
that it fulfills the requirements of IEC61131-3, making it "re-usable".
For this, the instances used so far in the program, which were used to store the instance data of
the IEC-COUNTER, must be replaced with multiple instances.
What to Do
1. Open "FB_Statistic", right-click on the first IEC-Counter and select the function "Change
instance…" (see picture).
2. In the dialog that appears, select "Multi-Instance" and give it the name
instance_IECCounter_1.
3. Repeat this with the two other IEC-Counters and give them the names
instance_IECCounter_2 and instance_IECCounter_3
4. Additional: If you programmed the function block "FB_TP_Sync" in the last chapter, then you
change the instances of the IEC-Timers in the function block "FB_TP_Sync" also.
5. After you have changed all instances into multi-instances, compile the entire S7 program,
because the call of "FB_Statistic" in "OB_Cycle" and the instance-DB passed there to the FB
are thereby also updated.
6. Delete the instance-DBs of the IEC-Counters since they are now no longer required.
7. Download the entire S7 program into the CPU and check all program functions.
8. Save your project.
3
Multi-instance
Task
Up until now, "FC_Signal" has been responsible for energizing the indicator lights.
The new "FB_Signal" block is to replace the previous "FC_Signal" and is to be created in such a
way that it fulfills the requirements of IEC61131-3, making it "re-usable".
What to Do
1. Insert the new "FB_Signal" block into your S7 program and open it.
2. Split the working area and open the "FC_Signal" block in the second half.
3. Copy all parameters and local variables from the interface of "FC_Signal" into the interface of
"FB_Signal".
4. Copy all networks from the "FC_Signal" block into the "FB_Signal" block.
5. Change the instance of the "LGF_Frequency" function block into a multiple instance.
6. Save your project.
Task
The call of the "FC_Signal" block is to be replaced with the call of the "FB_Signal" block.
What to Do
1. Open "OB_Cycle" and go to the call of "FC_Signal".
2. Open the selection menu of the blocks by double-clicking on the name "FC_Signal".
3. Select the "FB_Signal" block.
4. Open the context menu of the block call and choose the function "Create instance…".
5. In the follow-up window, give the single instance the name "iDB_FB_Signal"
6. As backup, you can copy "FC_Signal" into your Project library and then delete it since it is no
longer required.
7. Save, compile, download and test your project.
1. Declaration
2. Assignment
3. Scan
Declaration
The variables are also defined in the declaration part of the block. The name of the variable and
the data type must be specified.
Access
With optimized blocks, all temporary variables are initialized with 0 at the beginning of block
execution.
With non-optimized blocks, all temporary variables have an undefined value at the beginning of
block execution. When working with temporary variables, you must therefore make sure that the
variable is first of all assigned a defined value before it is scanned.
In the example, the result of the Addition is assigned to the temporary variable #inRes before it is
then scanned during the Multiplication.
Note
Variables that begin with the # special character are local variables (parameters or local
variables) that must be declared in the declaration part of the block. Local variables are only valid
and usable in the block in which they were declared.
The Program Editor automatically inserts the # character.
Operating system
OB 1 2 FC 17 3 FC 20 Event 1 2 3 4 5 6 7