You are on page 1of 25

Functions and Function Blocks

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.1

SITRAIN Training for


Automation and Industrial Solutions

Objectives
Upon completion of this chapter the participant will ...

SIMATIC S7
Siemens AG 2006. All rights reserved.

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

Date:
File:

2/8/2012
PRO1_11E.2

SITRAIN Training for


Automation and Industrial Solutions

Variables Overview

Global Variables / Data


(valid in the entire program)

Local Variables / Data


(only valid in one block)
Temporary Variables

Static Variables
are retained even after the
block is executed

M/T/C

are overwritten with undefined


values after the associated
block is executed

DB areas

temporary storage in L stack

PII / PIQ
I / O Peripherals

permanent storage in DBs


can be used in FBs only

usable in OBs / FCs / FBs

absolute

symbolic
Access

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.3

SITRAIN Training for


Automation and Industrial Solutions

Temporary Variables

Declaration

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.4

SITRAIN Training for


Automation and Industrial Solutions

Total Usage of the Local Data Stack


Operating
system
1

OB 1

FC 17
2

with temp.
variables

Result

FC 20

with temp.
variables

FC 30

with temp.
variables

FC30

FC20

OB1

SIMATIC S7
Siemens AG 2006. All rights reserved.

256 Bytes

Usage of
the L stack
FC17

FC17

FC17

FC17

FC17

OB1

OB1

OB1

OB1

OB1

Date:
File:

2/8/2012
PRO1_11E.5

OB1

SITRAIN Training for


Automation and Industrial Solutions

Example for a Fault Display


Task

Fault_Signal
Acknowledge
Stored_Fault

Display
Solution Suggestion

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.6

SITRAIN Training for


Automation and Industrial Solutions

Parameter-assignable Blocks
Solution with
non-parameterassignable block

Solution with parameter-assignable block


Program in FC 20

Call of FC 20 (e.g. in OB 1)
FC 20

1.1

#Fault_Signal

FP

M 17.2

FP #Edge_Memory

M 17.1

I 1.1

Fault_
Signal

#Stored_Fault

I 1.0

Acknowledge Display

#Acknowledge

M 10.3

Flash_Freq.

M 17.1

#Stored_Fault

M 17.1

M 17.1

#Stored_Fault

Stored_
Fault

M 10.3

#Flash_Freq.

M 17.2

Edge_
Memory

1.0

Q 5.1

AN

M 17.1

AN #Stored_Fault

1.1

#Fault_Signal

Q 5.1

#Display

SIMATIC S7
Siemens AG 2006. All rights reserved.

Formal parameters
Actual parameters

Date:
File:

2/8/2012
PRO1_11E.7

SITRAIN Training for


Automation and Industrial Solutions

Declaring Formal Parameters

Type of Parameter

Declaration

Use

Graphic Display

Input parameter
Output parameter

in
out

Read only
Write only

To the left of the block box


To the right of the block box

In/Out parameter

In_out

Read / Write

To the left of the block box

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.8

SITRAIN Training for


Automation and Industrial Solutions

Editing a Parameter-assignable Block

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.9

SITRAIN Training for


Automation and Industrial Solutions

Calling a Parameter-assignable Block

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.10

SITRAIN Training for


Automation and Industrial Solutions

Exercise 1: Editing the Parameter-assignable Function FC 20

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.11

SITRAIN Training for


Automation and Industrial Solutions

Exercise 2: Calling the Parameter-assignable Function FC 20

DI

DO

L_SYSTEM

I 1.0

T_Ackn_Fault

I 1.1
I 1.2

S_Fault1
S_Fault2

SIMATIC S7
Siemens AG 2006. All rights reserved.

Q 4.1

L_MAN

Q 4.2

L_AUTO

Q 4.3

L_Conv_
Fault

L_Fault1

Q 5.1

L_Fault2

Q 5.2

Date:
File:

2/8/2012
PRO1_11E.12

SITRAIN Training for


Automation and Industrial Solutions

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

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.13

ENO

SITRAIN Training for


Automation and Industrial Solutions

Function Block for Fault Display

Declaration Table
of the
Function Block

Instance
Data Block

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.14

SITRAIN Training for


Automation and Industrial Solutions

Generating Instance Data Blocks


1. Generate instance DB with FB call

2. Create new instance DB

In the LAD / FBD / STL Editor

SIMATIC S7
Siemens AG 2006. All rights reserved.

In the SIMATIC Manager

Date:
File:

2/8/2012
PRO1_11E.15

SITRAIN Training for


Automation and Industrial Solutions

Exercise 3: Editing the Parameter-assignable Function Block FB 20

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.16

SITRAIN Training for


Automation and Industrial Solutions

Exercise 4: Calling the Parameter-assignable Function Block FB 20

DI

DO

I 1.0

T_Ackn_Fault

I 1.1
I 1.2
I 1.3
I 1.4

S_Fault1
S_Fault2
S_Fault3
S_Fault4

SIMATIC S7
Siemens AG 2006. All rights reserved.

L_SYSTEM

Q 4.1

L_MAN

Q 4.2

L_AUTO

Q 4.3

L_Conv_
Fault

L_Fault1

Q 5.1

L_Fault2
L_Fault3
L_Fault4

Q 5.2
Q 5.3
Q 5.4

Date:
File:

2/8/2012
PRO1_11E.17

SITRAIN Training for


Automation and Industrial Solutions

Inserting / Deleting Block Parameters Later On

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.18

SITRAIN Training for


Automation and Industrial Solutions

Checking the Block Consistency

Block inconsistency

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.19

SITRAIN Training for


Automation and Industrial Solutions

Corrections when Calling Modified Blocks

Once with the right

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.20

SITRAIN Training for


Automation and Industrial Solutions

Using the EN and ENO Output


LAD/FBD

STL

FC 1
Unconditional call

Conditional call

...

EN

ENO

Q5.0

FC 1
EN
ENO

I 0.1

=
_001:

Example

ADD_I
...

SIMATIC S7
Siemens AG 2006. All rights reserved.

EN

MUL_I

ENO

EN

Date:
File:

ENO

2/8/2012
PRO1_11E.21

CALL
NOP 0

FC

A
JNB
CALL
A
=

I
_001
FC
BR
Q

0.1

DIV_I
EN

ENO

1
5.0

Q 5.1
=

SITRAIN Training for


Automation and Industrial Solutions

Summary: Block Calls


FC
Language

STL

FB

without parameters

with parameters

CALL FC1
UC FC1
CC FC1

CALL FC2
Par1: ...
Par2: ...
Par3: ...

w/o parm., w/o stat var.

UC FB1
CC FB1

with parm., and/or stat var

CALL FB2, DB3


Par1: ...
Par2: ...
Par3: ...

FC1
( CALL )

LAD

FC2

FC1
EN

DB3
FB2

ENO

EN

not available

ENO

Par1
Par2

EN
Par1

Par3

Par2

FC1
CALL

FBD

EN

EN

Par3

DB3
FC2

FC1

ENO

FB2
Par3

not available

Par1
Par2

EN
Par1

Par3
ENO

Par2

ENO

ENO

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.22

SITRAIN Training for


Automation and Industrial Solutions

Exercise 5: Recognizing Operand Types

Statement

L
L
T
L
T
T

Global

Local

Absolute

Symbolic

Static

Parameter

#Number_1
#Number_2
#Maximum_value
Number_1"
MW 40
MW 40
#Number_2

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.23

SITRAIN Training for


Automation and Industrial Solutions

If You Want to Know More

SIMATIC S7
Siemens AG 2006. All rights reserved.

Date:
File:

2/8/2012
PRO1_11E.24

SITRAIN Training for


Automation and Industrial Solutions

Comparison of Functions and Function Blocks


Program execution

CALL FC 10
On_1 :=
On_2 :=
Off
:=

I 0.1
I 0.2
Q 8.0

CALL FB5, DB5


Start
:=I 0.0
Stop
:=I 0.1
Motor_on
:=Q8.0
Speed
:=QW12

in
in
out
...

...
A
A
=
...

0.0
0.1
2.0
4.0

FB 5

FC10
On_1
On_2
Off

BOOL
BOOL
BOOL

in
Start
in
Stop
out Motor_on
out Speed
stat ...
Temp...

#On_1
#On_2
#Off

SIMATIC S7
Siemens AG 2006. All rights reserved.

...
A
AN
=
...

Date:
File:

2/8/2012
PRO1_11E.25

DB 5
Start
Stop
Motor_on
Speed

BOOL
BOOL
BOOL
INT

Drive

BOOL
BOOL
BOOL
INT

#Start
#Stop
#Motor_on

SITRAIN Training for


Automation and Industrial Solutions