TIA Portal: Functions & Blocks Guide
TIA Portal: Functions & Blocks Guide
Contents 9
AR
9. Functions and Function Blocks ............................................................................ 9-2
9.1. Task Description: Fault Evaluation with Parameter-assignable Blocks ............................... 9-3
9.2. Structured Programming ....................................................................................................... 9-4
9.2.1. Modular and Re-usable Blocks ............................................................................................. 9-5
9.2.2. Local and Global Operands .................................................................................................. 9-6
9.3. Solution with Parameter-assignable Block ........................................................................... 9-7
9.3.1. Declaration of Formal Parameters ........................................................................................ 9-8
9.3.2. Editing a Parameter-assignable Block .................................................................................. 9-9
9.4.
9.4.1.
9.5.
9.6.
N
Local, Temporary Variables ................................................................................................ 9-10
Local Data Stack ................................................................................................................. 9-11
Calling a Parameter-assignable Block ................................................................................ 9-12
Task Description: Fault Evaluation with a Function (FC) .................................................... 9-13
AI
9.6.1. Fault Evaluation .................................................................................................................. 9-14
9.6.2. Exercise 1: Creating the Function "FaultEvaluationFC" ..................................................... 9-15
9.6.3. Exercise 2: Calling and Parameterizing "FaultEvaluationFC" ............................................ 9-16
9.7. Task Description: Fault Evaluation with a Function Block (FB) .......................................... 9-17
9.7.1. Instantiating Function Blocks .............................................................................................. 9-18
9.7.2. FB - Declaration Section ..................................................................................................... 9-19
9.7.3. Generating Instance Data Blocks ....................................................................................... 9-20
TR
AR
… be familiar with the purpose of temporary variables
N
AI
TR
SI
"Fault"
Network 1
AR
Fault 1
"FaultEvaluationFC"
"P_fault1" %Q0.2 "P_fault1" (%Q0.2)
Fault 2
%Q0.3
"P_fault2"
Network 2
Fault1 Fault 3
%I0.4 "P_fault3" %Q0.4 "FaultEvaluationFB"
"S_fault1"
"P_fault2" (%Q0.3)
Fault2
%I0.5
"S_fault2" iDBx
Fault3 Network 3
%I0.6
"S_fault3"
"FaultEvaluationFB"
Acknowledge fault
%I0.7
″S_acknowledge″ "P_fault3" (%Q0.4)
Task Description
N iDBy
AI
Independent of the conveyor model functions so far, 3 different faults are to be evaluated as
follows.
If a fault is triggered at the simulator inputs "S_fault1" (%I0.4) to "S_fault3" (%I0.6), the
associated simulator LEDs "P_fault1" (%Q0.2) to "P_fault3" (%Q0.4) begin to flash.
A group acknowledgement for all faults takes place using the simulator input "S_acknowledge"
(%I0.7). If the fault still exists after acknowledgement, the LED changes to constant light; if the
TR
Block-structured
Linear Program Structured Program
Program
Recipe A
Pump
AR
Recipe B
Cycle Cycle Cycle
OB OB OB
Mixer Outlet
Outlet
Linear Program
N
The entire program is found in one continuous program block (Program cycle OB) which is
automatically called by the system. This model resembles a hard-wired relay control that was
AI
replaced by an automation system (programmable logic controller). The CPU processes the
individual instructions one after the other.
Block-structured Program
The program is divided into blocks, whereby every block only contains the program for solving a
partial task. Further structuring through networks is possible within a block. You can generate
TR
network templates for networks of the same type. Normally, a cyclically called Organization block
contains instructions which call the other blocks in a defined sequence.
Structured Program
A structured program contains parameter-assignable blocks that are set up in such a way that
they can be used universally. When a parameter-assignable block is called, it is passed current
parameters (for example, the specific addresses of inputs and outputs as well as parameter
values).
Example:
SI
"DB_Motor" "DB_Controller"
Modularization of the entire task:
• Partial tasks are solved in separate "Motor" "Modulator" "TON"
programs or blocks "Cycle_A"
AR
Re-usability of blocks: "Cycle_B"
"InstMotor"
data for solving the partial problems are stored. STEP7 blocks such as functions (FC) and
function blocks (FB) can be assigned parameters so that the concepts of structured programming
can be implemented with them. This means:
• Blocks for solving partial tasks implement their own data management with the help of local
variables.
• Blocks communicate with the "outside world", that is, with the sensors and actuators of the
process control or with other blocks of the user program, exclusively through their block
parameters.
• No access to global operands such as inputs, outputs, memory bits or variables in DBs can
SI
Advantages
• The blocks for the partial tasks can be created and tested independent of one another.
• 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.
AR
Temporary Variables (Temp)
• Memory bits • are overwritten after the block is executed
• temporary storage in the local data stack (L-stack)
• Variables in DBs • can be used in OBs / FCs / FBs
(Chapter Data Blocks)
Static Variables (Static)
• retain their value after the block is executed
• S5-Timers and • permanent storage in instance data block (IDB)
Counters • can only be declared in FBs
(not for S7-1200)
Constants (Constant)
• Constants • read-only as well as only symbolic access
• no memory usage
Global Operands
N
• can be used in OBs / FCs / FBs
Global operands are valid throughout the entire S7 program. Accordingly, every code (logic) block
(OB, FC, FB) can access these operands.
AI
Global operands include inputs, outputs, memory bits, SIMATIC timers, SIMATIC counters,
constants and variables which are declared in global data blocks (Chapter: 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.
R
• 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
T
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
SI
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.
AR
display
%M10.3 flashFreq
%M17.1 storedFault
%M17.2 oldFault
Formal parameters
Actual parameters
Application
N
You can program parameter-assignable blocks for frequently recurring program functions. This
has the following advantages:
AI
• The program only has to be created once, which significantly reduces programming effort.
• The block is only stored in the user memory once, which significantly reduces the amount of
memory used.
• The block or the functionality implemented with the block can be called as often as you like,
each time with different operands. For this, the formal parameters (input, output, or in/out
TR
parameters) are supplied with different actual parameters every time they are called.
Program Execution
When the block is executed, the formal parameters are replaced with the actual parameters
passed during the call.
If, as in the example, during the call of the block, the memory byte %M17.1 is passed as the
actual parameter for the formal parameter #storedFault, then, at runtime, the memory byte
%M17.1 is set or reset and its signal status is scanned etc.
SI
Parameter-assignability
You can program FC or FB blocks as parameter-assignable. You cannot program organization
blocks as parameter-assignable since they are called by the operating system and so the call
cannot be programmed and also no actual parameters can be specified.
Our Example
Even if the function is required repeatedly in the system, you only have to program the FC
"FaultEvaluation" once as parameter-assignable.
The FC "FaultEvaluation" is then called several times for the different fault evaluations and is
assigned a different actual operand each time.
Input parameter Input Read only To the left of the block box
Output parameter Output Write only To the right of the block box
In/Out parameter InOut Read / Write To the left of the block box
AR
Inputs
Output
InOuts
Return
Formal Operands
N
Before you can create the program in the parameter-assignable block, you have to define the
formal parameters in the declaration part.
AI
Type of Parameter
In the table in the picture, you can see the three possible types of parameters and their use.
Please note that formal operands that have a reading and a writing access have to be declared
as ‘In/Out’ parameters.
TR
Interface
The Input, Output and InOut parameters as well as the Return parameter form the interface of a
block. The Return parameter is an additional Output parameter and, defined according to IEC
61131-3, the Return value of the function. This parameter only exists for FCs. If it has the data
type VOID, it is not used and also does not appear as a parameter when the function is called.
The variables Temp and Constant are – even though they are listed in the declaration section of
the interface – not components of the block interface, since they do not become visible when the
block is called.
Example:
SI
The picture shows the declaration section, that is, the interface of a block. Since the formal
parameters #storedFault and #oldFault are to be accessed both reading and writing (see next
page), they are declared as InOut parameters.
Caution!
The declared formal parameters (Input, Output, InOut and Return) of a block are its
interface to the "outside". That is, they are "visible" or relevant to other blocks that call
this block. If the interface of a block is changed by deleting or adding formal parameters
later on, then the calls of the modified block have to be updated or corrected in all calling
blocks.
AR
Notes
N
It doesn‘t matter whether the names of the formal parameters are written with capital or small
letters. The "#" character in front of the name is automatically inserted by the PG. The character
AI
is used to indicate that the parameter is a local variable that was defined in the variable (tag)
declaration table of this block.
It is possible, that when you write the program in KOP or FUP, that the name is not completely
displayed in one line. This depends on how you have customized the settings in:
OptionsSettingsPLC programmingLAD/FBDOperand fieldMaximum width
TR
Symbols
1. If you use a symbolic name when you edit a block, the Editor first of all searches through the
interface of the block. If the symbolic name is there, the symbol with # in front of it is accepted
in the program as a local operand.
2. If a symbol cannot be found as a local operand, the Editor searches through the PLC tags for
the global symbol. If the symbol is found there, the symbol is placed in quotation marks and is
accepted in the program as a global operand.
3. If you specified the same symbolic name globally (in the PLC tags) as well as locally (in the
variable (tag) declaration table) the Editor will always insert the local operand.
SI
If, however, you want to work with the global symbol, you must select the relevant operand
when you make the entry, place the symbol name in quotation marks or change it later on.
1. Declaration
AR
2. Assignment
3. Scan
Declaration
N
The variables are also defined in the declaration part of the block. The name of the variable and
the data type must be specified.
AI
Access
With optimized blocks, all temporary variables are initialized with 0 at the beginning of block
execution.
With not-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
TR
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.
SI
Operating system
OB 1 2 FC 17 3 FC 20 Event 1 2 3 4 5 6 7
AR
the Local FC20
5 FC30
FC 30 data stack
7 FC17 FC17 FC17 FC17 FC17
6 with temp. OB 1 OB 1 OB 1 OB 1 OB 1 OB 1 OB 1
variables
AR
Block Call
N
A block can be called by dragging it from the "Program blocks" folder & dropping (inserting) it in
the statement (code) part of the calling block.
AI
Note
When a parameter-assignable function (FC) is called, an actual parameter must be passed for
every formal parameter.
Exception:
TR
In the graphic programming languages LAD and FBD, the assignment of the EN and ENO
parameters, which are automatically added by the Editor, is optional.
Parameter Assignment
All global and local operands whose data type corresponds to the formal parameters of the called
block can be passed as actual parameters.
The actual parameters can be passed with an absolute address or with a symbolic name - as
declared in the PLC tags or in the declaration part of the calling block.
SI
Passing On of Parameters
Basically, a "passing on of parameters" is also possible. That is, formal parameters of the calling
block are passed on as actual parameters to the called block. For parameters of complex data
types (see chapter "Data Blocks") this is however only possible with limitations.
"Fault"
Network 1
"FaultEvaluationFC"
AR
Fault 1 %Q0.2
"P_fault1" "P_fault1" (%Q0.2)
Fault 2 %Q0.3
"P_fault2"
Network 2
Fault1
%I 0.4
"S_fault1"
"FaultEvaluationFC"
"P_fault2" (%Q0.3)
%I 0.5
Fault2
"S_fault2"
%I 0.6
%I 0.7
Acknowledge fault
″S_acknowledge″
Task Description
N
AI
If a fault is triggered at the inputs "S_fault1" (%I0.4) or "S_fault2" (%I0.5), the associated LED
"P_fault1" (%Q0.2) or "P_fault2" (%Q0.3) begins to flash.
The input %I0.7 "S_acknowledge" is a group acknowledgement for all faults.
If the fault still exists after acknowledgement, the LED changes to constant light; if the fault no
longer exists, the LED goes dark.
First, a function "Fault" is to be created. Then, the required function is to be programmed in the
TR
Task
faultInput
acknowledge
storedFault
AR
display
Possible storedFault
faultInput
Solution SR
P S
storedFault &
display
faultInput =
Task
N
Faults that occur are to be displayed by an indicator light on the operator console. When there is
a signal change from 'FALSE' 'TRUE' at the input, the output shows a 2Hz flashing light.
AI
After the fault is acknowledged but still exists, the output (light) switches to a constant light. When
the fault no longer exists, the light at the output goes dark.
Possible Solution
An edge evaluation of the fault (faultInput) is required since the message buffer (storedFault)
would otherwise immediately be set again after an acknowledgement (acknowledge) and a still
TR
existing fault, thus making the display (display) flash once more.
When an acknowledgement (acknowledge) has not yet occurred, that is, the message buffer
(storedFault) still exists, the upper AND logic operation with the linked flash frequency
(flashFreq) causes the display (display) to flash.
When acknowledgement has already occurred (acknowledge) and therefore the message buffer
(storedFault) no longer exists, but the fault input (faultInput) still exists, the lower AND logic
operation causes a constant light at the display (display).
SI
AR
Task
N
You are to create the program for the fault evaluation in the parameter-assignable
"FaultEvaluationFC".
AI
What to Do
1. Insert the new function "FaultEvaluationFC" in the "Program blocks" folder.
2. Declare the formal parameters as shown in the picture.
3. Create the program as shown in the picture.
R
Fault 1
"P_fault1" %Q0.2
AR
Fault 2
%Q0.3
"P_fault2"
Fault1
%I0.4
"S_fault1"
Fault2
%I0.5
"S_fault2"
%I0.6
Acknowledge fault
%I0.7
″S_acknowledge″
Task
N
You are to create the new function "Fault" which will process the fault handling and fault
evaluation in later exercises.
AI
In the new block, 2 faults from the process (signals of the two simulator switches) are to be
evaluated. For this, the previously programmed "FaultEvaluationFC" must be called twice.
What to Do
1. Create the new function "Fault".
TR
2. In the FC "Fault", program the two calls of the previously created "FaultEvaluationFC" block
as shown in the picture.
3. Call the FC "Fault" in the OB "Program Cycle".
4. Save the change and transfer the program into the CPU.
5. Check your program to see whether it fulfills the described functions for fault evaluation.
Note
SI
The %MB10 memory byte was already parameterized as a clock memory byte in the device
configuration.
The "Clock_2Hz" (%M10.3) memory bit has a flashing frequency of 2Hz and was already created
as a PLC tag.
"Fault"
Network 1
Fault 1
"FaultEvaluationFC"
"P_fault1" %Q0.2 "P_fault1" (%Q0.2)
AR
Fault 2
%Q0.3
"P_fault2"
Network 2
Fault1 Fault 3
%I 0.4 "P_fault3" %Q0.4 "FaultEvaluationFB"
"S_fault1"
"P_fault2" (%Q0.3)
Fault2
%I 0.5
"S_fault2" iDBx
Fault3 Network 3
%I 0.6
"S_fault3"
"FaultEvaluationFB"
Acknowledge fault
%I 0.7
″S_acknowledge″ "P_fault3" (%Q0.4)
iDBy
Task Description
N
The previously described fault evaluation is now to be implemented with an FB instead of an FC.
This offers the advantage that for the internally required edge evaluation of the fault and as stored
AI
fault, the FB doesn’t have to be passed any global operands from outside since local, static
variables can be used.
Fault 1 is to continue to be evaluated by the already existing function "FaultEvaluationFC". The
evaluation of Fault 2 and 3 is to be carried out by the "FaultEvaluationFB" block which is now to
be created.
TR
SI
AR
Special Features
N
Unlike functions (FCs), function blocks (FBs) have a (recall) memory. That means that a local
data block is assigned to the function block. This data block is known as an instance data block.
AI
When you call an FB, you must also specify the Instance-DB which is then automatically used as
an instance for this FB call.
An instance DB is used to save static variables, among other things. These local variables can
only be used in the FB, in whose declaration table they were declared. When the block is exited,
they are retained.
TR
FB Advantages
• For the FC programming, the user must search for free memory areas and maintain them
himself. The static variables of an FB, on the other hand, are maintained by the STEP 7
software.
• The known danger of memory bit double assignments with an FC call is avoided with the use
of static instead of INOUT variables.
• Instead of the InOut formal parameters "storedFault" and "oldFault" of the function
"FaultEvaluationFC", static variables are used in the function block "FaultEvaluationFB". This
makes the block call simpler since the two formal parameters are dropped and, consequently,
SI
Instance_DB
Input
AR
Output
InOut
Static
L - Stack
Temp
Parameters
N
When the function block is called, the values of the actual parameters are stored in the instance
data block. If no actual parameter was assigned to one of these formal parameters in a block call,
AI
then the last value stored in the instance DB for this parameter is used in the program execution.
One exception is InOut parameters whose data types are not elementary. These must be
assigned since the values of the actual parameters are not stored in the instance DB but rather
the information about the storage location of the actual parameter.
Just as for a function, different actual parameters can be passed for each FB call. When the
function block is exited, the data is retained in the instance data block.
TR
A R
Generating
IN
There are two ways of generating an instance data block:
A
• Create a new block (data block) and select "Function block XY" as Type.
• For an FB call, the user specifies with which instance DB the FB is to work.
A dialog automatically opens in which the symbolic name and, if desired, a manual number of
the instance DB can be preset.
Already existing instance data blocks can also be selected here.
T R Caution!
If you modify the FB (by adding additional parameters or static variables), you must then
also generate the instance DB again.
S I
TIA-PRO1 - Functions and Function Blocks
9-20 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1
AR
Existing actual parameters
are adopted
N
In order to replace the call of a block with another block call, a selection list of all FCs and FBs
can be opened at the calling point by double-clicking on the name of the already called block.
AI
Advantage:
If both blocks have the same formal parameters, then they retain their actual parameters and do
not have to be supplied with new actual parameters.
TR
SI
AR
Task
N
You are to create the new function block "FaultEvaluationFB" for the subsequent evaluation of
Fault 2 and 3.
AI
What to Do
1. Insert the new function block "FaultEvaluationFB".
2. Declare the formal parameters and the static variables of the block as shown in the picture.
For this, you can copy the required variables from the already programmed function
"FaultEvaluationFC".
TR
3. Program the function block "FaultEvaluationFB". For this, you can copy the required program
parts from the already programmed function "FaultEvaluationFC".
4. Save the block and download it into the CPU.
SI
Fault 1
"P_fault1" %Q0.2
AR
Fault 2
%Q0.3
"P_fault2"
Fault1 Fault 3
%I 0.4 "S_fault1" "P_fault3" %Q0.4
Fault2
%I 0.5 "S_fault2"
Fault3
%I 0.6
"S_fault3"
Acknowledge fault
%I 0.7
″S_acknowledge″
Task
N
The evaluation of the old Fault 2 (programmed up until now through the call of the function
"FaultEvaluationFC") and the evaluation of the new Fault 3 is to be implemented with the newly
AI
created function block "FaultEvaluationFB".
For this, the parameter-assignable function block "FaultEvaluationFB" must be called twice in the
function "Fault", each time with its own instance data block.
What to Do
1. In the function "Fault", replace the second call of the function "FaultEvaluationFC" with the
TR
You can generate a new instance via the context menu of the call (right-click on the
Block call > "Create instance") or you create a new instance DB (data block of the type
"FaultEvaluationFB") and insert it using drag & drop.
3. Program the second call of "FaultEvaluationFB" - as shown in the picture - in a new network
and let the Editor generate the instance "InstFaultEvaluationFB_3".
SI
A R
Automatic update of the
call during compiling
Problem
I N
If you have to adjust or supplement the interfaces or the code of individual blocks during or after
A
program creation, it can lead to time stamp conflicts. Time stamp conflicts can, in turn, lead to
block inconsistencies between calling and called blocks or reference blocks and thus to a high
degree of correction effort.
If block parameters are added later on to a block already called in the program, you also have to
update the calls of the block in other blocks.
R
• Automatic Update
Time stamp conflicts are also detected when the entire user program is compiled and in case
of added parameters, affected block calls are automatically updated.
T
For functions, the added formal parameter must still be assigned before downloading into the
CPU, since this is a "Must Assign".
I
For function blocks, the default value from the associated instance DB is used when the
formal parameter is not assigned ("Can Assign").
• Manual Update
S
See 9.10.2 Manually Updating a Block Call
AR
(Options > Settings > PLC programming >
General > Compilation)
Function "Delete actual parameters on interface
update" is activated?
Yes
No
N
If block parameters are deleted (removed) later on from a block already called in the program,
you also have to update the calls of the block in the calling blocks.
•
AI
Automatic Update
Attention/Caution: If the deleted formal parameters have already been assigned with actual
parameters, then this automatic update only occurs if the function "Delete actual parameters
on interface update" is activated under Options > Settings > PLC-programming > General >
Compilation.
TR
• Manual Update
See 9.10.2 Manually Updating a Block Call
SI
1xR
A R
Manual Update
I N
In the open, calling block, the inconsistent calls of a block are highlighted in red. By right-
A
clicking the inconsistent call, the function "Update block call" can be selected in the context
menu. A window then appears in which the old (faulty) and the new block call (in the picture
without the parameter "LEDTest") are displayed. For function blocks, the associated instance
DB is subsequently regenerated.
TR
S I
TIA-PRO1 - Functions and Function Blocks
9-26 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1
AR
N
AI
TR
SI
OB1
FB1
FB2
FB2
AR
FB2 call in FB1
updated
OB1
FB1
FB2
Only FB2 compiled:
FB2
FB2 call in FB1
not yet updated In : Bool1
data block. As a result, a re-initialization of the data block becomes necessary during the next
download, which was prevented by the use of the memory reserve.
Compilation Results
The status of the compilation is hierarchically displayed in the Inspector window "Info -> Compile".
If errors occurred during compilation, you can jump directly to the error location by double-clicking
on the error entry.
• Valid throughout the entire CPU, • Are only valid in the block in which
i.e. all blocks have access they have been declared (defined)
Validity range
• The name of the tag must be • The name of the tag must be unique
unique within the entire CPU within the block
AR
• Inputs • Temporary variables
• Outputs (in all code (logic) blocks)
Operands • Memory bits
• Tags in data blocks • Static variables
• SIMATIC Timers / Counters (only in function blocks)