You are on page 1of 27

Functions and Function Blocks

SIMATIC S7 Date: 04.03.2019


File: 09_.1
Introduction

Global Variables / Data Local Variables / Data


(valid in the entire program) (only valid in one block)

Temporary Variables
• PII / PIQ Static Variables
• are overwritten with undefined
• I/ O • are retained even after
values after the associated
the block is executed
•M/T/C block is executed
• permanent storage in DBs
• DB areas • temporary storage in L stack
• can only be used in FBs
• useable in OBs / FCs / FBs

absolute symbolic

Access

SIMATIC S7 Date: 04.03.2019


File: 09_.2
Temporary Variables

Declaration

SIMATIC S7 Date: 04.03.2019


File: 09_.3
Local Data Stack Size
Entire size:
1.5 Kbyte
(CPU 313..316)

For S7-300:
Execution
Priority class L stack size

Startup (one-time execution) 27


256 bytes
Cyclic execution 1

Time-of-Day Interrupt 2 256 bytes


Time-controlled
execution Time-Delay Interrupt 3 256 bytes

Cyclic Interrupt 12 256 bytes

Hardware Interrupt 16 256 bytes


Event-driven
execution Error handling in startup 28
256 bytes
Error handling in scan cycle 26

SIMATIC S7 Date: 04.03.2019


File: 09_.4
Byte Requirement of a Block in the Local Data Stack

rechts

SIMATIC S7 Date: 04.03.2019


File: 09_.5
Total Usage of the Local Data Stack
Operating
system
1

OB 1 FC 17 FC 20
3
2
with with temp.
temp. 4 variables
variables

5 FC 30

7 with temp.
6 variables

Result 1 2 3 4 5 6 7

Usage of
the L stack FC30
FC20

Bytes
256
FC17 FC17 FC17 FC17 FC17
OB1 OB1 OB1 OB1 OB1 OB1 OB1

SIMATIC S7 Date: 04.03.2019


File: 09_.6
Exercise: Use of Temporary Variables

Replace by the
temporary variable
"Packages"

SIMATIC S7 Date: 04.03.2019


File: 09_.7
Example for a Fault Display

Task

Fault_Signal

Acknowledge

Stored_Fault

Display

SIMATIC S7 Date: 04.03.2019


File: 09_.8
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 Acknowledge Display Q 5.1
A I 1.0 A #Acknowledge M 10.3 Flash_Freq.
R M 17.1 R #Stored_Fault M 17.1 Stored_
Fault
A M 17.1 A #Stored_Fault
M 17.2 Edge_
A M 10.3 A #Flash_Freq. Memory
O O
AN M 17.1 AN #Stored_Fault
A I 1.1 A #Fault_Signal
Formal parameters
= Q 5.1 = #Display
Actual parameters

SIMATIC S7 Date: 04.03.2019


File: 09_.9
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_out Read / Write To the left of the block box

SIMATIC S7 Date: 04.03.2019


File: 09_.10
Editing a Parameter-assignable Block

SIMATIC S7 Date: 04.03.2019


File: 09_.11
Calling a Parameter-assignable Block

SIMATIC S7 Date: 04.03.2019


File: 09_.12
Using the EN/ENO Parameters with Block Calls
LAD/FBD STL

FC 1
Unconditional call ?? . ? EN ENO CALL FC 1
NOP 0

FC 1 Q 9.0
A I 0.1
Conditional call I 0.1 EN ENO = JNB _001
CALL FC 1
_001: A BR
= Q 9.0

Example
FC 1 FC 2 FC 3

?? . ? EN ENO EN ENO EN ENO =

SIMATIC S7 Date: 04.03.2019


File: 09_.13
Exercise: Editing a Parameter-assignable FC Block

SIMATIC S7 Date: 04.03.2019


File: 09_.14
Exercise: Calling a Parameter-assignable FC Block

1st. call of FC 20 for


display of Disturb. 1

2nd. call of FC 20 for


display of Disturb. 2

SIMATIC S7 Date: 04.03.2019


File: 09_.15
Function Blocks (FBs)
OB 1

DB 2

FB 2
EN
Disturb. input
Acknowledge Display
Flash freq. ENO

Declaration table of the function block

SIMATIC S7 Date: 04.03.2019


File: 09_.16
Function Block for Message Display

Declaration table
of the function
block

Instance
data block

SIMATIC S7 Date: 04.03.2019


File: 09_.17
Generating Instance Data Blocks

1. Generate instance DB with FB call 2. Create new instance DB

SIMATIC S7 Date: 04.03.2019


File: 09_.18
The Multiple Instance Model

The Instance Model The Multiple Instance Model

OB 1 DB10 OB 1

Call FB20, DB10 Call FB100, DB100


FB20
Disturb._Input:=
Acknowledge:= FB 100
Flash_Freq:=
Display:= stat Dist_1 FB20

DB11 stat Dist_2 FB20


Call FB20, DB11 DB100
Disturb._Input:=
Acknowledge:= Call Dist_1 Parameters and
FB20 Disturb._Input:=
Flash_Freq:= static variables
Display:= Acknowledge:= of the 1st. call
Flash_Freq:= of FB20
Display:=

Call FB20, DB12 DB12 Call Dist_2


Disturb._Input:= Disturb._Input:= Parameters and
Acknowledge:= FB20 Acknowledge:= static variables
Flash_Freq:= Flash_Freq:= of the 2nd. call
Display:= Display:= of FB20

SIMATIC S7 Date: 04.03.2019


File: 09_.19
Inserting/Deleting Block Parameters Later On

SIMATIC S7 Date: 04.03.2019


File: 09_.20
Checking the Block Consistency

Block inconsistency

SIMATIC S7 Date: 04.03.2019


File: 09_.21
Corrections when Calling Modified Blocks

Once with the right

SIMATIC S7 Date: 04.03.2019


File: 09_.22
Exercise: Editing a Function Block

SIMATIC S7 Date: 04.03.2019


File: 09_.23
Exercise: Calling a Function Block and Testing It

Add
Network 2
&3

Delete
Network 2

SIMATIC S7 Date: 04.03.2019


File: 09_.24
Converting an FC to an FB using a Source Program (1)

SIMATIC S7 Date: 04.03.2019


File: 09_.25
Converting an FC Block to an FB using a Source Program (2)

: :
: :

SIMATIC S7 Date: 04.03.2019


File: 09_.26
Summary: Block Calls
FC FB
Lan-
guage Without parameters With parameters W/o param., w/o inst. DB With param., with inst.DB

• CALL FC1 • CALL FC2 • CALL FB2, DB3


STL • UC FC1 Par1: ... • UC FB1 Par1: ...
Par2: ... Par2: ...
• CC FC1 Par3: ... • CC FB1 Par3: ...

FC1
( CALL ) DB3
FC2 FB1 FB2
FC1
LAD EN ENO EN ENO EN ENO
EN ENO
Par1 Par1
Par2 Par3 Par2 Par3

FC1
CALL DB3
FC2 FB1 FB2
FC1 EN Par3 EN EN Par3
FBD EN Par1 Par1
ENO
Par2 ENO Par2
ENO
ENO

SIMATIC S7 Date: 04.03.2019


File: 09_.27

You might also like