Functions and Function Blocks
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.1
Objectives
Upon completion of this chapter the participant will ...
… be familiar with the purpose of temporary variables
... be able to program parameter-assignable functions
and their calls
... know the difference between functions (FCs) and function blocks
(FBs)
... be familiar with the purpose of static variables
... be able to declare static variables and apply them in the program
... be able to program parameter-assignable function blocks and call
them
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.2
Variables Overview
Global Variables / Data Local Variables / Data
(valid in the entire program) (only valid in one block)
Temporary Variables Static Variables
• PII / PIQ
• are overwritten with undefined • are retained even after the
• I / O Peripherals values after the associated block is executed
block is executed
•M/T/C • permanent storage in DBs
• temporary storage in L stack
• DB areas • can be used in FBs only
• usable in OBs / FCs / FBs
absolute symbolic
Access
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.3
Temporary Variables
Declaration
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.4
Total Usage of the Local Data Stack
Operatin
g system
1
OB 1 FC 17 FC 20
3
2
with temp. with temp.
variables 4
variables
5 FC 30
7 with temp.
6
variables
Result 1 2 3 4 5 6 7
Usage of
the L
256 Bytes
stack FC30
FC20
FC17 FC17 FC17 FC17 FC17
OB1 OB1 OB1 OB1 OB1 OB1 OB1
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.5
Example for a Fault Display
Task
Fault_Signal
Acknowledge
Stored_Fault
Display
Solution Suggestion
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.6
Parameter-assignable Blocks
Solution with Solution with parameter-assignable block
non-parameter-
assignable block Program in FC 20 Call of FC 20 (e.g. in OB 1)
FC 20
A I 1.1 A #Fault_Signal I 1.1 Fault_
FP M 17.2 FP #Edge_Memory
Signal
S M 17.1 S #Stored_Fault I 1.0 Display Q 5.1
Ackno
A I 1.0 A M
wledg
10.3 e
R M 17.1 #Acknowledge R
M Flash
A M 17.1 #Stored_Fault
M17.1
17.2 _Freq
A M 10.3 A #Stored_Fault .
O A #Flash_Freq.
Stored_
AN M 17.1 O Fault
A I 1.1 AN #Stored_Fault Edge_
Formal parameters
Memor
= Q 5.1 A y
Actual parameters
#Fault_Signal
= #Display
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.7
Declaring Formal Parameters
Type of Parameter Declaration Use Graphic Display
Input parameter in Read only
To the left of the block box
Output parameter out Write only
To the right of the block box
In/Out parameter In_ Read / Write
out To the left of the block box
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.8
Editing a Parameter-assignable Block
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.9
Calling a Parameter-assignable Block
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.10
Exercise 1: Editing the Parameter-assignable Function FC 20
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.11
Exercise 2: Calling the Parameter-assignable Function FC 20
DI DO
Q 4.1
L_SYSTEM
L_MAN Q 4.2
L_AUTO Q 4.3
L_Conv_
I 1.0 T_Ackn_Fault
Fault
I 1.1 S_Fault1 Q 5.1
I 1.2 L_Fault1 Q 5.2
S_Fault2
L_Fault2
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.12
Function Blocks (FB)
Declaration Table
of the
Function Block
Call of
function block
such as in OB1 DB 2
FB 20
EN
Fault_Signal
Acknowledge
Display
Flash_frequency ENO
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.13
Function Block for Fault Display
Declaration Table
of the
Function Block
Instance
Data Block
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.14
Generating Instance Data Blocks
1. Generate instance DB with FB call 2. Create new instance DB
In the LAD / FBD / STL Editor In the SIMATIC Manager
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.15
Exercise 3: Editing the Parameter-assignable Function Block FB 20
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.16
Exercise 4: Calling the Parameter-assignable Function Block FB 20
DI DO
L_SYSTEM Q 4.1
L_MAN Q 4.2
L_AUTO Q 4.3
L_Conv_
I 1.0 T_Ackn_Fault Fault
I 1.1 S_Fault1 L_Fault1 Q 5.1
I 1.2 S_Fault2 L_Fault2 Q 5.2
I 1.3 S_Fault3 Q 5.3
I 1.4 S_Fault4 L_Fault3 Q 5.4
L_Fault4
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.17
Inserting / Deleting Block Parameters Later On
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.18
Checking the Block Consistency
Block inconsistency
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.19
Corrections when Calling Modified Blocks
Once with the right
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.20
Using the EN and ENO Output
LAD/FBD STL
FC 1
Unconditional call ... EN ENO CALL 1
FC NOP 0
FC 1 Q5.0
A I 0.1
Conditional call I 0.1 EN ENO = JNB _001
CALL FC 1
_001: A BR
= Q 5.0
Example
ADD_I MUL_I DIV_I
Q 5.1
... EN ENO EN ENO EN ENO =
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.21
Summary: Block Calls
FC FB
Language without parameters with parameters w/o parm., w/o stat var. with parm., and/or stat var
• CALL FC1 • CALL FC2 • CALL FB2, DB3
STL • UC FC1 Par1: ... • UC FB1 Par1: ...
Par2: ... Par2: ...
• CC FC1 • CC FB1
Par3: ... Par3: ...
FC1
( CALL ) DB3
FC2 FB2
FC1
LAD EN ENO not available EN ENO
EN ENO
Par1 Par1
Par3 Par3
Par2 Par2
FC1
CALL DB3
FC2 FB2
FC1 EN Par3 Par3
FBD not available EN
EN Par1
ENO
Par1
ENO
ENO Par2
Par2
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.22
Exercise 5: Recognizing Operand Types
Statement
Global Local Absolute Symbolic Static Parameter
L #Number_1
L #Number_2
T #Maximum_value
L „Number_1"
T MW 40
T #MNWum40ber_2
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.23
If You Want to Know More
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.24
Comparison of Functions and Function Blocks
Program execution CALL FB5, DB5
Start :=I
0.0
Motor_on
Stop :=I
CALL FC 10 0.1 :=Q8.0
On_1 := I Speed :=QW12 DB 5
0.1
On_2 := I 1. StartBOOL Drive
0.2 2. Stop BOOL
Off := Q 2.0 Motor_on
FC10
8.0
FB 5 BOOL
4.0 Speed INT
in On_1 BOOL
in On_2 BOOL in Start BOOL
in Stop
out Off BOOL
out BOOL
... n
Motor_o
BOOL
out Speed INT
... stat ...
A #On_1 Temp...
A #On_2
= #Off ...
A #Start
...
AN #Stop
= #Motor_on
...
SIMATIC S7 Date 2/8/2012 SITRAIN Training for
Automation and Industrial Solutions
Siemens AG 2006. All rights reserved. : PRO1_11E.25