Sunteți pe pagina 1din 27

Contents

Page

Introduction .................................................................................................................................... 2
Temporary Variables
........................................................................................................................
Local Data Stack Size
...............................................................................................................
Byte Requirement of a Block in the Local Data Stack
................................................................. 5
Total Occupation in the Local Data Stack ...................................................................................... 6
Exercise: Use of Temporary Variables
...........................................................................
Example of a Message Display Indicating a Problem in the Process
............................................
Parameter-assignable Blocks
.........................................................................................................
Declaration of the Formal Parameters
...........................................................................................
Editing a Parameter-assignable Block
.......................................................................................
Calling a Parameter-assignable Block
.........................................................................................
Using the EN/ENO Parameters with Block Calls
..............................................................
Exercise: Creating a Parameter-assignable FC Block ................................................................... 14
Exercise: Calling a Parameter-assignable FC Block
......................................................................
Function Blocks (FBs)
.................................................................................................................
Function Blocks for Message Display
..................................................................................
Generating Instance Data Blocks
............................................................................................
The Multiple Instance Model
..........................................................................................................
Inserting/Deleting Block Parameters Later On
............................................................
Corrections when Calling Modified Blocks
.........................................................................
Exercise: Editing a Function Block
................................................................................................
Calling a Function Block and Testing It
..........................................................................................
C
Converting
ti an FC tto an FB using
i aS
Source P
Program (1) .............................................................. 24
Converting an FC to an FB using a Source Program (2)
..............................................................
Exercise: Recognizing Types of Variables
................................................................................... 26
Summary: Block Calls
.......................................................................................

Training Center
for Automation and Drives

Page 1

3
4

7
8
9
10
11
12
13
15
16
17
18
19
20
21
22
23
25
27

ST-7PRO1
Functions and Function Blocks

General

Up until now, the inputs and outputs in the bottling plant were addressed with their
actual
t l parameters.
t
You
Y could
ld nott assign
i parameters
t
to
t the
th blocks.
bl k
You would choose this procedure, for example, for the creation of a program that is
only used once with a special machine.
For frequently recurring functions in larger systems, you create universally useable,
parameter-assignable blocks (FC, FB). These have formal input and output
parameters, that are assigned actual parameters when the block is called.
The adjustment of block functionality to the hardware takes place with the
parameter assignment when the block is called; the inner life" of the block does not
change.
change

Local Variables

Up until now you have used global variables (bit memories and data blocks) to store
production data, for example. In this chapter you will find out more about data
storage in local variables.

Temporary Variables Temporary variables are variables that are only stored while the block is being
executed. They can be used in all blocks (OB, FC, FB).
St ti Variables
Static
V i bl

Training Center
for Automation and Drives

If the
th data
d t are to
t be
b retained
t i d even after
ft the
th block
bl k is
i executed,
t d they
th mustt be
b stored
t d
in static variables.
Static variables can only be used in function blocks.

Page 2

ST-7PRO1
Functions and Function Blocks

General

Temporary variables can be used in all blocks (OB, FC, FB). They are used to
t
temporarily
il store
t
information
i f
ti while
hil th
the bl
block
k iis b
being
i executed.
t d Th
The d
data
t are llostt
when the block is exited.
The data are stored in the L stack (local data stack). It is a separate memory area in
the CPU.

Declaration

You define the variables in the declaration table of the block. In the line "temp" you
enter a variable name and the associated data type.
You cannot predefine a start value here.
Aft you have
After
h
saved
d the
th block,
bl k the
th memory location
l
ti in
i the
th L stack
t k is
i displayed
di l
d iin th
the
"Address" column.

Access

In Network 1, you see an example of the symbolic access to a temporary variable.


The result of subtracting is stored in the temporary variable result".
You can also make an absolute access (T LW0). You should, however, try to avoid
this since the program is difficult to read.

Note
#

Variable names that begin


g with the special
p
character # are local variables which
are only valid within the block in which they are declared in the declaration table.
The Program Editor automatically enters the special character.

Training Center
for Automation and Drives

Page 3

ST-7PRO1
Functions and Function Blocks

Local Data Stack

The local data stack (L stack) is a memory area that contains the temporary
variables (replacement for scratchpad memories in SIMATIC S5) of the blocks
blocks.

Local Data Stack


Size

When the operating system calls an OB, an L stack area of 256 bytes is opened
up while the OB and the blocks called in it are executed.
Every priority class is assigned 256 bytes.
The L stack of the 313..316 CPUs has a total of 1536 bytes (1.5kByte).

Priority Classes

There are a total of eight priority classes with the S7-300. However, no more than
6p
priority
y classes can be active at the same time. If, for example,
p OB 100 is active
(with priority class 27), then OB1 (priority class 1) can never be active.
Furthermore, the error OBs 80 to 87 for asynchronous errors can only then have
priority class 28, if the fault occurs in the startup program. In other words, when
they interrupt OB100. More information can be found in the chapter "Organization
Blocks".

S7-400

With the S7-400 CPUs, you can decide what the size of the local data stack is for
the individual priority classes (Tool: HW Config.).
You can deselect the priority classes which you do not need
need. That way
way, you can
make more local data available to the other priority classes.

Training Center
for Automation and Drives

Page 4

ST-7PRO1
Functions and Function Blocks

Displaying the
B t R
Byte
Requirement
i
t

You can see the exact number of bytes a block requires in the local data stack
b going
by
i iinto
t th
the bl
block
k properties.
ti

Activate

1. In the SIMATIC Manager, select the block with the right mouse button and
then -> Object Properties. or
2. In the SIMATIC Manager, select the block with the left mouse button and then
the menu options Edit -> Object Properties.

Notes

The sum of local data for an execution level (OB) is a maximum of 256 bytes
with the S7-300. Every OB itself always takes up 20 or 22 bytes.
This means that a maximum of 234 bytes can be used in an FC or FB
FB.
If more than 256 bytes of local data are defined in a block, the block cannot be
downloaded into the CPU. The transmission is interrupted with an error message
The block could not be copied". Within this error message is a "Details" button. If
you click on it, a message box appears with an explanation Incorrect local data
length".

Training Center
for Automation and Drives

Page 5

ST-7PRO1
Functions and Function Blocks

Total Occupation in
Local Data Stack

You can display the number of bytes an entire program requires in the local
data stack with the "Reference
Reference Data
Data" tool.
tool You will become familiar with this tool
in the chapter Troubleshooting".
The total occupation of the local data stack and the number of bytes required per
call path is displayed on the screen.

Activate
Reference Data

In the SIMATIC Manager you select the block folder and then the menu options
Options -> Reference Data -> Display.

Note

If the maximum number of local data is exceeded during program execution in the
CPU, the CPU goes into the Stop mode. STOP caused by error when allocating
local data" is entered as the cause of error in the diagnostics buffer.

Training Center
for Automation and Drives

Page 6

ST-7PRO1
Functions and Function Blocks

Goal

You are not to use any bit memories as temporary storage in the FC 19 block of the
S7 program "Fill".
"Fill" These
Th
are tto b
be replaced
l
db
by a ttemporary variable
i bl P
Packages".
k
"
If you wrote a program for the block in LAD or FBD, the bit memories were
necessary to connect the output of the divider with the input of the code converter.
Even if you wrote a program for the block in STL (where no bit memories are
necessary) insert the temporary variable Packages" for storing.

What to Do

Result

Training Center
for Automation and Drives

Open the FC 19 block in the S7 program "FILL" .


Define a temporary variable with the name Packages" and the data type
"Integer
g in the declaration table.
Store the number of packages in the temporary variable.
Download the changed program and test it.

You now know the use of temporary variables.

Page 7

ST-7PRO1
Functions and Function Blocks

Description

Problems (disturbances) that occur are to be displayed by an LED on the operator


console.
l Wh
When th
the problem
bl
(I1
(I1.3)
3) occurs, th
the LED (Q8
(Q8.3
3 or Q4
Q4.3)
3) iis tto flflash
h with
ith
2Hz. The problem is detected at the acknowledge input I 1.2. If the problem is
corrected, the LED stops flashing. If the problem continues, the LED switches to a
steady light until the problem is corrected.

Program

So that even problems that only exist for a short time are not lost, a dominant set flip
flop (M40.0) is used.
An RLO edge detection of the message signal is also carried out, since the memory
is otherwise immediately reset when an existing problem is acknowledged.
If the report memory is set (message has not yet been acknowledged), the upper
AND logic operation causes the LED to flash. With this, the bit memory M10.3, that
was defined as a clock memory when parameter assignment was made in the CPU,
is gated.
The lower AND logic operation is used to cause a steady light for a problem that is
acknowledged but still exists.

Training Center
for Automation and Drives

Page 8

ST-7PRO1
Functions and Function Blocks

Introduction

Example

You can use parameter-assignable blocks for frequently recurring program sections.
Thi h
This
has th
the ffollowing
ll i advantages:
d
t
the program only has to be created once
the block is only stored in the user memory once and you can call it as often
as you like
the block can be programmed with formal parameters (input, output or in/out
parameters) and only when the block is called is it given the real addresses
(actual parameters).
When the block is executed, the statement A Disturbance input" is checked to see
which actual p
parameters are assigned
g
to the formal p
parameter Disturbance input".
p
If, when the block is called, I 1.4 is lined up as the actual parameter, then the
statement A I 1.4" is carried out.

FC / FB

Parameter-assignable blocks can either be FCs or FBs.

Parameterassignable FC20

In the example, the message display is required ten times in the system.
It is created as a parameter-assignable FC 20 block and is then called ten times
with different actual parameters.

Training Center
for Automation and Drives

Page 9

ST-7PRO1
Functions and Function Blocks

Formal Addresses

Before you can create the program for the parameter-assignable block, you have to
d fi th
define
the fformall parameters
t
in
i th
the d
declaration
l ti ttable.
bl

Type of Parameter

In the table in the slide, you can see three possible types of parameters and their
uses. Please make sure that when you have a reading and writing access to a
formal address that you use an in/out parameter.

Example of the FC20 In the lower section of the slide, you can see the declaration table for the message
display (see previous page). Since the report memory is to be accessed reading
((set/reset)) as well as writing
g (q
(query),
y) it has to be defined as an in/out p
parameter.
Notes

There is only one row for every type of parameter in the declaration table. Should
you need several input parameters, you have to use the "Return" key when you end
your inputs in the first row. An additional row for this type of parameter is then
opened up. After you have selected a declaration row, you can also use the menu
options Insert -> Declaration Row -> Before Selection / After Selection to insert an
additional row.

Attention!

If you want to insert or delete declaration rows later - after the block has already
been called - , you have to update the block calls !

Training Center
for Automation and Drives

Page 10

ST-7PRO1
Functions and Function Blocks

Notes

It doesnt matter whether the names of the formal parameters are written with
capital
it l or smallll lletters.
tt
The
Th "#" character
h
t iin ffrontt off th
the name iis automatically
t
ti ll
inserted by the PG. This is to indicate to you that it is a local variable that was
defined in the variable declaration table of this block.
It is possible, that when you write the program in LAD / FBD, that the name is not
completely displayed in one row. This depends on how you customized the settings
in the Program Editor (Options -> Customize -> "LAD/FBD" tab -> Width of address
field).

S b l
Symbols

Training Center
for Automation and Drives

1 If you use a symbolic


1.
b li name when
h you edit
dit a bl
block,
k th
the Edit
Editor searches
h th
the
variable declaration table.
If it is there, the symbol with the # in front of it is accepted in the program as
a local variable.
2. If it cannot be found as a local variable, the Editor searches the symbol table
for global symbols.
If it is found there, the symbol is placed in quotation marks and is accepted in
the program.
3. If yyou specified
p
the same symbolic
y
name as g
global ((in the symbol
y
table)) as
well as local (in the variable declaration table), the Editor will always insert
the local variable.
If, however, you want to work with the global symbol, you must place the
symbol name in quotation marks when you make the entry.

Page 11

ST-7PRO1
Functions and Function Blocks

Call

In LAD/FBD, you can select the call from the "Program Elements" browser.
Q
Question
ti marks
k (??.?)
(?? ?) are di
displayed
l
d att th
the iinput,
t output
t t and
d iin/out
/ t parameters
t
off
the block. Here you insert the actual parameters you want.

Note

When you call a parameter-assignable FC block, you must assign all block
parameters (with the exception of EN and ENO).

Training Center
for Automation and Drives

Page 12

ST-7PRO1
Functions and Function Blocks

Standard FCs

The following rules exist for the execution of standard FCs:


If EN=0, the block is not executed and ENO is also =0.
If EN=1, the block is exeucted and if it is executed without errors ENO is also
=1.
If an error occurs while the block is being executed, ENO becomes =0.

User FCs

It doesnt matter whether a user block was written in LAD, FBD or STL, when it is
called in LAD/FBD, the p
parameters EN and ENO are added as well. Thus it is
possible to pass on the RLO.
EN/ENO do not exist in STL. You can, however, emulate them.
You must program -irregardless of the programming language- an error evaluation.

Interconnection

In LAD/FBD, several boxes can be grouped together one after the other and linked
logically with EN / ENO.

Training Center
for Automation and Drives

Page 13

ST-7PRO1
Functions and Function Blocks

Task

Write the program for a message display as a parameter-assignable block. In the


slide
lid you can see th
the d
declaration
l ti ttable
bl with
ith th
the iinputt and
d output
t t parameters
t
and
d th
the
beginning of the program.

What to Do

Training Center
for Automation and Drives

Insert an FC 20 block in the S7 program "My Program" .


Write the program in FC 20.
Save FC 20.

Page 14

ST-7PRO1
Functions and Function Blocks

Task

Call FC 20 in OB 1 twice (with different absolute addresses). Test the functionality


off the
th program.

What to Do

Insert two networks into the OB 1 from the S7 program "My Program".
Create the two calls to FC 20, as given in the slide,
Download the FC20 and OB 1 blocks,
Test the function.
Note: During HW Config, you used MB10 for parameter-assignment of the clock
memory byte. If you have performed a memory reset in the meantime, you must
download the HW configuration
g
once again
g
to cause the M10.3 to flash.

Training Center
for Automation and Drives

Page 15

ST-7PRO1
Functions and Function Blocks

Special Features
off FBs
FB

Unlike functions (FCs), function blocks (FBs) have a (recall) memory. That
means that
th t a local
l
ld
data
t bl
block
k iis assigned
i
d tto th
the ffunction
ti bl
block,
k th
the so-called
ll d
instance data block. When you call an FB, you also have to specify the number of
the instance DB, which is automatically opened.
An instance DB is used to save static variables. These local variables can only be
used in the FB, in whose declaration table they are declared. When the block is
exited, they are retained.

Parameters

When the function block is called, the values of the actual parameter are stored in
the instance data block.
If no actual parameters are assigned to a formal parameter in a block call, then the
last value stored in the instance DB for this parameter is used in the program
execution.
You can specify different actual parameters with every FB call.
When the function block is exited, the data in the data block is retained.

FB Advantages

Training Center
for Automation and Drives

When you write a program for an FC, you must search for empty bit memory
address areas or data areas and you must maintain them yourself. The static
variables
i bl off an FB
FB, on th
the other
th hand,
h d are maintained
i t i db
by th
the STEP 7
software.
When you use static variables you avoid the risk of assigning bit memory
address areas or data areas twice.
Instead of the formal parameters Report memory" and Edge memory
marker" of the FC20, you use the static variables Report memory" and
Edge memory marker" in the FB. This makes the block call simpler since
the two formal parameters are dropped.

Page 16

ST-7PRO1
Functions and Function Blocks

Message Display

In an earlier exercise you created a parameter-assignable FC 20 block for


di l i a message (i
displaying
(indicating
di ti a problem).
bl )
Instead of bit memories, that were used in the FC20 to save the message signal
and its RLO edge detection, you can use so-called static variables in an FB. They
are stored in the instance DB referencing the FB.

Instance DB
Structure

When a DB is generated and references an FB, STEP7 creates the data


structure of the data block using the structure specified in the local declaration table
for the function block. After you save the DB, the data block is created and can then
be used as an instance DB.

Training Center
for Automation and Drives

Page 17

ST-7PRO1
Functions and Function Blocks

Generating an
I t
Instance
DB

There are two ways of generating a new instance DB:


When
Wh you callll an FB
FB, you specify
if with
ith which
hi h instance
i t
DB th
the FB is
i to
t work.
k
The following message then pops up:
"Instance data block DB x does not exist. Do you want to generate it?".
When you create a new DB, you select the option "Data block referencing a
function block".

Notes

One instance DB can only reference one FB. However, one FB can be referenced
by a different instance DB every time it is called.
If you modify the FB (by adding parameters or static variables),
variables) you must then also
generate the instance DB again.

Training Center
for Automation and Drives

Page 18

ST-7PRO1
Functions and Function Blocks

Multiple Instance
M d l
Model

Up until now, you had to use a different instance data block for every call of a
f
function
ti block.
bl k Th
The number
b off d
data
t bl
blocks
k iis lilimited
it d h
however, and
d ffor th
thatt reason
there is a method that allows you to use a common instance DB for several FB
calls.
The multiple instance model now enables you to use a single DB for several calls.
To do this, you need an additional FB to manage these instances.
For every FB call (FB 20), you define a static variable in the higher-level FB (FB
100). With the block call Call Dist_1, you do not then have to specify an instance
DB.
The higher-level FB (FB 100) is called, for example, in OB1, the common instance
DB (DB 100) is only generated once.

Note

Training Center
for Automation and Drives

Multiple instances are discussed in an advanced programming course.

Page 19

ST-7PRO1
Functions and Function Blocks

The Problem

When you add additional block parameters later on to a block already called in the
program, you mustt also
l update
d t th
the bl
block
k call.
ll
Otherwise, the CPU would either go into Stop or the block function could not be
guaranteed since the additional parameters still have to be supplied with actual
parameters in the call.
In the example, an additional input parameter Check_lights" was inserted.

Updating the Call

When you save the block in which the declaration table was modified, a message
pops up warning you of possible problems.

Training Center
for Automation and Drives

Page 20

ST-7PRO1
Functions and Function Blocks

Updating the Call

Training Center
for Automation and Drives

When the calling block is opened, the following message pops up


Ti
Time
stamp
t
conflict
fli t with
ith att least
l
t one block
bl k call"
ll" and
d the
th callll is
i displayed
di l
d iin red.
d
With the right mouse button, click on the block call and select the menu option
Update Call". The block call is then redisplayed and contains, in our example, the
additional input parameter Check_light. This parameter can then be assigned. In
the case of function blocks, the instance DB is then regenerated.

Page 21

ST-7PRO1
Functions and Function Blocks

Task

The program for displaying a message (indicating a problem) is now to be


i l
implemented
t d iin a ffunction
ti bl
block.
k For
F saving
i the
th edge
d memory bit and
d th
the reportt
memory you are to use static variables that are stored in the instance DB of the FB.
That way, you do not require any bit memories for saving.
In the slide you can see the declaration table with the input and output parameters
and the beginning of the program.

What to Do

Training Center
for Automation and Drives

Insert an FB 20 block into the S7 program "My Program".


Write the program in the FB 20.
Save the FB 20.

Page 22

ST-7PRO1
Functions and Function Blocks

What to Do

In the S7 program "My Program":

delete both networks by calling the FC20.

write a program for the two calls to the FB 20, as given in the slide.

download the FB20, DB20, DB21 and OB 1 blocks.

test the functionality of the program.

FC -> FB

In our example, FB20 was rewritten, even though an FC20 with the same contents
already existed. It was not hard to do.
If an FC with an extensive program is to be converted into an FB
FB, you go about it
differently:
1st. possible solution:
insert a new FB
copy the declaration table of the FC block into the FB and adjust it
copy the networks from the FC into the FB
save the FB.
2nd. p
possible solution:
generate a source file from the FC block
make the adjustments in the source file
generate a new FB from the source file (see next page).

Training Center
for Automation and Drives

Page 23

ST-7PRO1
Functions and Function Blocks

Introduction

Just as with higher-level programming languages, you can also create blocks using
a source program (text
(t t fil
file).
) When
Wh you compile
il thi
this source program, executable
t bl
blocks are generated. By the same token, a source file can be generated from
existing blocks.
You are to make use of this to create, with the least amount of work (typing), an FB
20 from an FC 20 block.

What to Do

1. Open a block.
2. In the LAD/STL/FBD Editor, choose the menu options Options ->
Customize then the "Editor
Customize,
Editor tab.
tab In the View"
View box select the option
"Symbolic representation".
3. In the LAD/STL/FBD Editor select the menu options File -> Generate Source
File...
4. In the "New" window, you can enter a name of your choice for the source file
to be generated in the "Object name" box.
5. In the Generate Source File" window that then appears, select the program
blocks that are to be compiled one after the other in the Unselected Blocks
window.
6. Click "OK" to start the compilation of the blocks into source code.
The compiled blocks are then found in the source file which you named
previously, in the Source Files" folder of the S7 program.

Training Center
for Automation and Drives

Page 24

ST-7PRO1
Functions and Function Blocks

Sources, What For?

for automatic rewiring using symbols

for assigning block attributes, for example, block protection

as data protection of the entire program

more freedom in editing and processing blocks:


- modifying block type
- insert / delete network separations
- merging program elements and comments into new blocks
- creating program sources in other text editors (WORD,
(WORD WordPad)
WordPad),
without having to install STEP7 in the PC
- complete symbolic programming without syntax check, etc.

Source Program

In the left-hand section of the slide you can see the source program for the FC 20
block. On the right-hand side, the source file is displayed for the program as FB 20.
Here, the associated keywords for a function block were entered. In addition to that,
static variables for the report memory and the edge memory marker are defined.
After a compilation run, an executable FB 20 exists once more.

Note

If you do not know the key words, you can insert a block template using the menu
options Insert -> Block Template -> FB.
The subject of Source Files" is discussed in greater detail in an advanced
programming course.

Training Center
for Automation and Drives

Page 25

ST-7PRO1
Functions and Function Blocks

Goal

You are to recognize the differences between the various types of variables.

What to Do

In the table, mark the associated data type with an X.


Answer the following question:
What is not correct in the statement T#Number_2 ?
.................................................................................

Result

Training Center
for Automation and Drives

g
and use the types
yp of variables.
You can recognize

Page 26

ST-7PRO1
Functions and Function Blocks

CALL

The "CALL" instruction is used for calling program blocks (FC, FB, SFC, SFB),
regardless
dl
off the
th RLO or any other
th conditions.
diti
If you call an FB or SFB with "CALL, you must also specify the relevant instance
DB.
You can use either the absolute or the symbolic name for the program block. For
example: "CALL FB2, DB2" or "CALL valve, level".
The "CALL" operation saves the return address, deactivates dependence on the
MCR and creates the local data area for the block to be called.

UC

The "UC" iinstruction


Th
t ti iis an unconditional
diti
l callll off a bl
block
k off th
the ttype FC or FB without
ith t
parameter assignment.
"UC" is otherwise identical to "CALL".

CC

The "CC" instruction calls a block of the type FC or FB without parameters if


RLO=1.
"CC" is otherwise identical to "CALL".

Parameters

Parameters declared in the declaration table are known as formal p


parameters". The
addresses or values specified in the call are referred to as actual parameters".
Static and temporary variables are not specified in the call.

Training Center
for Automation and Drives

Page 27

ST-7PRO1
Functions and Function Blocks

S-ar putea să vă placă și