0% found this document useful (0 votes)
170 views29 pages

TIA Portal: Functions & Blocks Guide

This document section discusses structured programming and parameter-assignable blocks in SIMATIC TIA Portal. It introduces the concept of structured programming using modular and reusable blocks. It also presents a task that involves evaluating three faults using parameter-assignable function blocks or functions, where the blocks or functions would be called from networks and passed fault parameters. The participant will learn about declaring block parameters, editing parameter-assignable blocks, using local and temporary variables, calling blocks, and instantiating and using function blocks versus functions for this fault evaluation task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views29 pages

TIA Portal: Functions & Blocks Guide

This document section discusses structured programming and parameter-assignable blocks in SIMATIC TIA Portal. It introduces the concept of structured programming using modular and reusable blocks. It also presents a task that involves evaluating three faults using parameter-assignable function blocks or functions, where the blocks or functions would be called from networks and passed fault parameters. The participant will learn about declaring block parameters, editing parameter-assignable blocks, using local and temporary variables, calling blocks, and instantiating and using function blocks versus functions for this fault evaluation task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

SIMATIC TIA Portal Programming 1

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

9.8. Changing the Block Call ...................................................................................................... 9-21


9.9. Exercise 3: Creating the Function Block "FaultEvaluationFB" ............................................ 9-22
9.9.1. Exercise 4: Calling and Parameterizing "FaultEvaluationFB" ............................................. 9-23
9.10. Adding Block Parameters Later On .................................................................................... 9-24
9.10.1. Removing Block Parameters Later On ............................................................................... 9-25
9.10.2. Manually Updating a Block Call .......................................................................................... 9-26
9.11. Additional Information ......................................................................................................... 9-27
9.11.1. Compiling Individual / All Changed Blocks ......................................................................... 9-28
SI

9.11.2. Global and Local Tags ........................................................................................................ 9-29

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-1
SIMATIC TIA Portal Programming 1

9. Functions and Function Blocks

At the end of the chapter the participant will ...

... be familiar with the purpose of parameter-assignable blocks

… be familiar with the declaration section of a block

AR
… be familiar with the purpose of temporary variables

... be familiar with the purpose of static variables

… know what a structured programming is

... be able to program parameter-assignable functions and function


blocks and their calls

N
AI
TR
SI

TIA-PRO1 - Functions and Function Blocks


9-2 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.1. Task Description: Fault Evaluation


with Parameter-assignable Blocks

"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

fault no longer exists, the LED goes dark.


SI

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-3
SIMATIC TIA Portal Programming 1

9.2. Structured Programming

Block-structured
Linear Program Structured Program
Program

Recipe A
Pump

AR
Recipe B
Cycle Cycle Cycle
OB OB OB
Mixer Outlet

Outlet

The instructions for the Re-usable functions are programmed in


All instructions are found individual functions are found in individual blocks.
in one block (in a individual blocks. Cycle OB Cycle OB or other blocks call these
"Program cycle OB") calls these blocks one after the blocks and pass on the relevant data.
other.

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

− A "pump block" contains instructions for the control of a pump.


− The program blocks, which are responsible for the control of special pumps, call the "pump
block" and provide it with information about which pump is to be controlled with which
parameters.

TIA-PRO1 - Functions and Function Blocks


9-4 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.2.1. Modular and Re-usable Blocks

"DB_Motor" "DB_Controller"
Modularization of the entire task:
• Partial tasks are solved in separate "Motor" "Modulator" "TON"
programs or blocks "Cycle_A"

• Parameter assignment enables flexible


"DB_Valves"
usage
"Valve" "Limit" "BLKMOV"
• Example: Drilling cycle with changeable
depth

AR
Re-usability of blocks: "Cycle_B"
"InstMotor"

 Blocks can be called as often as is "Motor"


required "InstMotor" Decl. Name Type
in Start BOOL
 Restrictions: "Motor"
in Stop BOOL
out Motor_ON BOOL
Start Motor_ON
• No access to global operands out Speed INT
Stop Speed stat Speed_old INT
• Communication only via the parameter temp Calc_1 INT
:
list :
A #Start

Modularization of the Entire Task


N
Abstraction is the basis for solving complex problems, in which we concentrate on the
fundamental aspects of a program in every abstraction level and ignore all the details that are not
AI
essential. Abstraction helps us to divide complex tasks into partial tasks which can then be solved
on their own.

Parameter-assignable (Re-usable) Blocks


STEP7 supports this concept of modularization with its block model. The partial tasks that result
from the division of the entire task are assigned to blocks in which the necessary algorithms and
TR

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

be made from within the statement section of blocks.

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.

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-5
SIMATIC TIA Portal Programming 1

9.2.2. Local and Global Operands

Global Operands Local Operands


(valid in the entire program) (only valid in one block)
Formal Parameters (Input, Output, InOut)
• PII / PIQ • interface for data exchange between calling and called block
• temporary storage in the L-stack for FCs or storage in the IDB for FBs
• can be used in FCs / FBs
• I / O peripherals

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.

TIA-PRO1 - Functions and Function Blocks


9-6 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.3. Solution with Parameter-assignable Block

Solution with non- Solution with parameter-assignable block


parameter-assignable block
Program in the Function Call of Function (e. g. OB_Cycle)
FaultEvaluation
%I0.4 fault

%I0.7 acknowledge %Q0.2

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.

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-7
SIMATIC TIA Portal Programming 1

9.3.1. Declaration of Formal Parameters

Type of Parameter Declaration Use Graphic Display

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.

TIA-PRO1 - Functions and Function Blocks


9-8 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.3.2. Editing a Parameter-assignable Block

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:
OptionsSettingsPLC programmingLAD/FBDOperand fieldMaximum 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.

Drag & Drop


Just as with global variables (for example, from the PLC tags) local variables can be dragged into
the program part of the Editor as operands from the block interface using drag & drop and placed
in the desired position there.

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-9
SIMATIC TIA Portal Programming 1

9.4. Local, Temporary Variables

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

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 #tempResult
before it is then scanned during the Multiplication. (The arithmetic operations are dealt with in the
chapter “Digital Operations”.)

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

The Program Editor automatically inserts the # character.

TIA-PRO1 - Functions and Function Blocks


9-10 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.4.1. Local Data Stack

Operating system

OB 1 2 FC 17 3 FC 20 Event 1 2 3 4 5 6 7

with temp. 4 with temp.


variables variables
Usage of

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

Total Usage of the Local Data Stack (L-Stack)


N
For every program execution level or priority class (such as, OB 1 with all blocks that are called in
it), a separate local data stack is reserved. That is, a segment of defined size is reserved on the L
AI
stack of the CPU (allocation or reservation of memory space).
The local variables/operands of OB 1 as well as the local, temporary variables of the blocks (FCs
and FBs) called in or by OB 1 are stored in this local data stack.
The "Call structure" display indicates to what extent an S7 program puts a burden on the local
data stack. (The Call structure itself is dealt with in the chapter "Troubleshooting".)
TR
SI

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-11
SIMATIC TIA Portal Programming 1

9.5. Calling a Parameter-assignable Block

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.

TIA-PRO1 - Functions and Function Blocks


9-12 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.6. Task Description:


Fault Evaluation with a Function (FC)

"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

parameter-assignable function "FaultEvaluationFC" which is then to be called twice in the function


"Fault" for the evaluation of the two faults.
SI

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-13
SIMATIC TIA Portal Programming 1

9.6.1. Fault Evaluation

Task

faultInput

acknowledge

storedFault

AR
display

Possible storedFault
faultInput
Solution SR
P S

oldfault acknowledge R Q & >=1


flashFreq

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

TIA-PRO1 - Functions and Function Blocks


9-14 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.6.2. Exercise 1: Creating the Function "FaultEvaluationFC"

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

4. Save the block.


T
SI

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-15
SIMATIC TIA Portal Programming 1

9.6.3. Exercise 2: Calling and Parameterizing "FaultEvaluationFC"

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.

TIA-PRO1 - Functions and Function Blocks


9-16 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.7. Task Description: Fault Evaluation with a Function Block (FB)

"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

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-17
SIMATIC TIA Portal Programming 1

9.7.1. Instantiating Function Blocks

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

do not have to be supplied with actual parameters.

TIA-PRO1 - Functions and Function Blocks


9-18 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.7.2. FB - Declaration Section

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

Static Variables (Static)


Unlike functions, function blocks additionally have "static variables" (Static). These variables form
the memory of the FB. They are not stored in the L-Stack but also in their own instance data
block.
SI

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-19
SIMATIC TIA Portal Programming 1

9.7.3. Generating Instance Data Blocks

Automatically generating an instance DB


during the FB call Manually creating an instance DB

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

9.8. Changing the Block Call

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

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-21
SIMATIC TIA Portal Programming 1

9.9. Exercise 3: Creating the Function Block "FaultEvaluationFB"

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

TIA-PRO1 - Functions and Function Blocks


9-22 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.9.1. Exercise 4: Calling and Parameterizing "FaultEvaluationFB"

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

call of the function block "FaultEvaluationFB"


2. Generate the instance data block "InstFaultEvaluationFB_2" and specify it as "Single
Instance" for the programmed call of the function block "FaultEvaluationFB".

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

4. Save the modified function "Fault".


5. Download the entire program into the CPU and check the program function.

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-23
SIMATIC TIA Portal Programming 1

9.10. Adding Block Parameters Later On

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

TIA-PRO1 - Functions and Function Blocks


9-24 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.10.1. Removing Block Parameters Later On

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

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-25
SIMATIC TIA Portal Programming 1

9.10.2. Manually Updating a Block Call

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

9.11. Additional Information

AR
N
AI
TR
SI

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-27
SIMATIC TIA Portal Programming 1

9.11.1. Compiling Individual / All Changed Blocks

OB1

FB1

FB2
FB2

All changed blocks In : Bool1


compiled:

AR
FB2 call in FB1
updated

OB1

FB1

FB2
Only FB2 compiled:
FB2
FB2 call in FB1
not yet updated In : Bool1

Compile  Software (Only Changes):


N
If one individual block is selected in the Project tree or if a compilation is triggered through the
button shown to the left when a block is open, only this single block is compiled (if it was
AI
changed). The disadvantage of compiling one individual block is that interface conflicts in the
calling blocks caused by interface changes are not corrected.
If several blocks are selected or a block group, only those blocks modified since the last
compilation are compiled (delta compilation).
If the "Program blocks" folder is selected, the delta compilation for the entire program is carried
out.
TR

Compile  Software (Rebuild All Blocks)/ or Rebuild All):


All, even those blocks not modified since the last compilation, are compiled.

Compile  Software (Reset Memory Reserve):


Note: What a memory reserve is, is dealt with in Programming 2.
With this compilation process, the memory reserve of data blocks is also reset, that is, that
variables that were created later on in the course of data block expansions and were stored in the
memory reserve are removed from the memory reserve and integrated in the regular part of the
SI

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.

TIA-PRO1 - Functions and Function Blocks


9-28 Training Document, V16.00.00
SIMATIC TIA Portal Programming 1

9.11.2. Global and Local Tags

Global Tags Local Tags

• 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)

Location of • PLC tag table


• Declaration part of the block
declaration • Global data blocks (Chap. 11)

• Presented in quotation marks • Presented preceded by #


Presentation Example: "Max" Example: #Max

Validity Range of Tags


N
Tags that are declared in the PLC tag table or in a global data block can be addressed by all CPU
program blocks. For that reason, these tags are called global tags.
AI
Tags and parameters that are declared in the declaration part of a code (logic) block are local
operands; they can only be used in the statement part of the same block.
TR
SI

TIA-PRO1 - Functions and Function Blocks


Training Document, V16.00.00 9-29

You might also like