Vous êtes sur la page 1sur 18

Contents

Page
g

Storage Areas for Data


......................................................................................................
Data Blocks (DBs)
..........................................................................................................................
Overview of Data Types in STEP 7
.................................................................................................
Elementary Data Types in STEP 7
....................................................................................................
Complex Data Types
...........................................................................................................
Example of a Structure ...................................................................................................................... 7
E
Example
l off an A
Array
..........................................................................................................................
Creating a New Data Block
............................................................................................................
Entering, Saving, Downloading and Monitoring a Data Block ............................................................ 10
Addressing Data Elements
.........................................................................................................
Accessing Data Elements
.............................................................................................................
Validity of an Open DB
.............................................................................................................
User-Defined Data Type (UDT)
.....................................................................................................
E t i a UDT Bl
Entering
Block
k
.....................................................................................................................
Creating a Data Block Referencing a Data Type
............................................................................ 16
Example: Array from UDTs .............................................................................................................. 17
Exercise: Program for a Bottling Plant - Data Storage
............................................................

Training Center
for Automation and Drives

Page 1

2
3
4
5
6
8
9
11
12
13
14
15

18

ST-7PRO1
Data Storage in Data Blocks

Overview

Apart from program blocks, a user program also consists of data containing
i f
information
ti about
b t process states,
t t
signals,
i
l etc.,
t which
hi h are th
then processed
d according
di tto
the instructions in the user program.
Data is stored in variables of the user program, which are uniquely identified by:
Storage location (address: e.g. P, PII, PIQ, bit memory, L stack, DB)
Data type (elementary or complex data type, parameter type)
Depending on the accessibility, a distinction is also made between:
Global variables, which are declared in the global symbol table or in global
data blocks
Local variables, which are declared in the declaration part of OBs, FBs and
FCs.
Variables can have a permanent storage location in the process image, bit memory
area or in a data block or they can be created dynamically in the L stack when a block
is being executed.

Local Data Stack

The local data stack (L stack) is an area for storing:


temporary
p
y variables of a logic
g block, including
g OB start information
actual parameters to be passed when calling functions
intermediate logic results in LAD programs
This topic is dealt with in the chapter "Functions and Function Blocks".

Data Blocks

Data blocks are blocks used by the logic blocks of the user program for storing
values. Unlike the temporary data, the data in data blocks is not overwritten when
execution of the logic block is completed or when the DB is closed.

Training Center
for Automation and Drives

Page 2

ST-7PRO1
Data Storage in Data Blocks

Overview

Data blocks are used for storing user data. Like the logic blocks, data blocks take up
space in
i th
the user memory. Th
The d
data
t blocks
bl k contain
t i variable
i bl d
data
t ((e.g. numeric
i
values) that is used in the user program.
The user program can access the data in a data block with bit, byte, word or
doubleword operations. Symbolic or absolute addresses can be used.

Uses

You can use data blocks in different ways, depending on their contents. You
differentiate between:
Global data blocks: These contain information that can be accessed by all
the logic blocks in the user program.
program
Instance data blocks: These are always assigned to a particular FB. The
data in each DB should only be used by the assigned FB.
Instance data blocks are dealt with in more detail in the chapter "Functions
and Function Blocks.

Creating DBs

You can create global DBs either using the Program Editor or with a user-defined
data type" that you have already created.
Instance data blocks are created when an FB block is called
called.

Registers

The CPU has two data block registers, the DB and DI registers. Thus, you can have
two data blocks open at the same time.
You will find further information in an advanced programming course.

Training Center
for Automation and Drives

Page 3

ST-7PRO1
Data Storage in Data Blocks

Overview

Data types determine the properties of data, i.e. the way the contents of one or
more associated
i t d addresses
dd
are tto b
be represented
t d and
d th
the permissible
i ibl range off
values.
The data type also determines which operations can be used.

Elementary
Data Types

Elementary data types are predefined in accordance with IEC 1131-3. The data
type determines the amount of memory space required. For example, the Word
data type takes up 16 bits in the user memory.
Elementary data types are never more than 32 bits long and can be loaded into the
accumulators of the S7 processor in full and processed with elementary STEP 7
instructions.

Complex
Data Types

Complex data types can only be used in conjunction with variables declared in
global data blocks. Complex data types cannot be completely loaded into the
accumulators with load instructions. You use standard blocks from the library ("IEC"
S7 Program) to process complex data types.

User-Defined
D t T
Data
Types

A user-defined data type can be used for data blocks or as a data type in a
variable
i bl d
declaration
l ti ttable.
bl
You create UDTs with the Data Block Editor.
The structure of a UDT can contain groups of elementary and/or complex data
types.

Training Center
for Automation and Drives

Page 4

ST-7PRO1
Data Storage in Data Blocks

BOOL, BYTE, WORD Variables of the data type BOOL consist of one bit, variables of data types
DWORD CHAR
DWORD,
BYTE WORD,
BYTE,
WORD DWORD are sequences off 8
8, 16 and
d 32 bit
bits respectively.
ti l Th
The
individual bits are not evaluted in these data types.
Special forms of these data types are the BCD numbers and the count value used in
conjunction with the count function, as well as the data type CHAR, which
represents a character in ASCII code.
S5TIME

Variables of the data type S5TIME are required for specifying timer values in timer
functions (S5 Timer functions). You specify the time in hours, minutes, seconds or
milliseconds. You can enter the timer values with an underline ((1h_4m)
_ ) or without
an underline (1h4m).
Functions FC 33 and FC40 from the library convert S5TIME to TIME format and
TIME to S5TIME format.

INT, DINT, REAL

Variables of these data types represent numbers which can be used in


mathematical operations.

TIME

A variable of data type TIME takes up a doubleword. This variable is used, for
example for specifying timer values in IEC timer functions.
example,
functions The contents of the
variable are interpreted as a DINT number in milliseconds and can be either positive
or negative (e.g.: T#1s=L#1 000, T#24d20h31m23s647msw = L#214748647).

DATE

A variable of data type DATE is stored in a word in the form of an unsigned integer.
The contents of the variable represent the number of days since 01.01.1990 (e.g.:
D#2168-12-31 = W#16#FF62).

TIME_OF_DAY
_ _

A variable of data type


yp TIME_OF_DAY
_ _
takes up
p a doubleword. It contains the
number of milliseconds since the beginning of the day (0:00 oclock) in the form of
an unsigned integer. (e.g.: TOD#23:59:59.999 = DW#16#05265B77).

Training Center
for Automation and Drives

Page 5

ST-7PRO1
Data Storage in Data Blocks

Complex
D t T
Data
Types

Complex data types (arrays and structures) consist of groups of elementary or


complex
l d
data
t ttypes.
They enable you to create data types to suit your problem, with which you can
structure large quantities of data and process it symbolically.
Complex data types cannot be processed with STEP 7 instructions all at once
(longer than 32 bits), but only one element at a time.
Complex data types are predefined. The data type DATE_AND_TIME has a length
of 64 bits. The lengths of the data types ARRAY, STRUCT and STRING are
defined by the user.
V i bl off th
Variables
the complex
l d
data
t ttypes can only
l b
be d
declared
l d iin global
l b ld
data
t bl
blocks
k and
d
as parameters or local variables of logic blocks.

User-Defined
Data Type

User-defined data types represent a self-defined structure. This is stored in


UDT blocks (UDT1 ... UDT65535) and can be used as a template" in another
variables data type.
You can save typing time when inputting a data block if you require the same
structure several times.
Example: You require the same structure 10 times in a data block.
block First,
First you define
the structure and save it as UDT 1, for example.
In the DB, you define a variable "Addresses" as an array with 10 elements of the
type UDT1:
Addresses array[1..10]
UDT 1
Thus, you have created 10 data ranges with the structure defined in UDT 1 without
additional typing".

Training Center
for Automation and Drives

Page 6

ST-7PRO1
Data Storage in Data Blocks

Structure

The slide shows an example of a structure with the name "Motor_data". The
structure
t t
consists
i t off severall elements
l
t off different
diff
t data
d t types.
t
The
Th individual
i di id l
elements of a structure can be elementary or complex data types.
The access to the individual elements of a structure contains the structure name.
The program is thus easier to read.
In order to be able to access the elements symbolically, the data block must be
given a symbol name, for example, Drive_1".
Examples of accessing individual elements of a structure:
L Drive_1".Motor_data.rated_current or
L D
Drive_1".Motor_data.operating_speed
i
1" M t d t
ti
d
Drive_1" is the symbol name of the data block, that contains the structure. The
structure name is given (separated by a dot) after the symbol name. An element
name of the structure follows (separated by a dot) after the structure name.

Define Structure
in DB

The keyword for a structure is "STRUCT". The end of a structure is indicated


by "END_STRUCT". A name is defined for the structure (in the example:
"Motor_data").

Training Center
for Automation and Drives

Page 7

ST-7PRO1
Data Storage in Data Blocks

Array

An array consists of several elements of the same data type. In the slide above, you
can see the
th array "Measuring_point"
"M
i
i t" with
ith 10 elements
l
t off th
the d
data
t ttype REAL
REAL.
Later, various measured values are to be stored in this array.

Define Array in DB

The keyword for an array is "ARRAY[n..m]". The first (n) and the last element (m) is
specified in the square brackets. In the example, [1..10] means 10 elements,
whereby the first element is addressed with the index [1] and the last with the index
[10]. Instead of [1..10] you could, for example, define [0..9]. This only affects access
to the elements.

N t
Note

T create
To
t an empty
t data
d t block,
bl k you define
d fi an array with
ith th
the d
data
t ttype you want.
t

Data View

To see which values are stored in the individual elements, you select the menu
options View -> Data View to switch to another display. In "Data View, you will find
the values stored presently in the column "Actual Value".

Training Center
for Automation and Drives

Page 8

ST-7PRO1
Data Storage in Data Blocks

Program Editor

You can open an existing data block or create a new one with the LAD/STL/FBD
Editor.
Edit

"New" Dialog Box

When you click the icon for "New, the "New" dialog box is displayed. You select the
project and the user program and then enter, for example, DB4 for "Object name"
(Data Block or All Editable should be selected as the Object type). When you
confirm your entries by clicking the "OK" button, the "New Data Block" dialog box
appears.

"New Data Block"


Dialog
Di
l Box
B

In this dialog box, you select the type of data block to be created:
Data
D t Bl
Block
k ((global
l b ld
data
t bl
block)
k)
Data Block Referencing a User-Defined Data Type (creates a data block
using the same structure as a UDT block)
Data Block Referencing a Function Block (creates an instance DB for an
FB). This item is explained in more detail in the chapter "Functions and
Function Blocks".

Training Center
for Automation and Drives

Page 9

ST-7PRO1
Data Storage in Data Blocks

Entering Data

You enter the individual data elements in the table. To do so, you select the first
empty
t line
li iin th
the "N
"Name"" column
l
and
d enter
t th
the elements
l
t d
description.
i ti
Y
You can jjump
to the other columns - Type, Initial Value and Comment - by using the Tab key.

Columns

The meanings of the columns are as follows:

Address

Name
Type
Initial Value

Comment

is entered by the Program Editor when you save.


It is the first byte address occupied by the variable in the
data block.
symbolic
y
name of the element.
data type (you select this with the right mouse button).
is used for setting a default value for an element. If you do
not make an entry here, zero will be inserted as the initial
value.
for documenting the data element (optional).

Save

You save the data block on the hard disk of the programming device using the
"Diskette"
Diskette icon.
icon

Download

Just as the logic blocks, you also have to download the data blocks to the CPU.

Monitor

To monitor the current values in the data block, you first of all have to switch to the
"Data View" view. You can monitor the data block by using the "spectacles" icon in
the toobar (permanent display of the actual values of the DB in the CPU).

Training Center
for Automation and Drives

Page 10

ST-7PRO1
Data Storage in Data Blocks

General

You address the data elements of a data block byte-by-byte, just as you do bit
memories.
i
You can load and transfer data bytes, data words or data doublewords. When using
data words, you specify the first byte address (e.g. L DBW 2) with the operation.
Two bytes are loaded as of this address. With doublewords, 4 bytes are loaded as
of the byte address that you enter.

Number, Length

The number of data blocks depends on the CPU you use.


The maximum block length is 8KByte for the S7-300 and 64KByte for the
S7-400.

Note

If you access non-existent data elements or data blocks, the CPU goes into the
Stop mode, if you did not program an error OB.

Training Center
for Automation and Drives

Page 11

ST-7PRO1
Data Storage in Data Blocks

Opening a DB

The instruction OPN DB..." opens a global data block. If a global DB was already
open, it iis automatically
t
ti ll closed.
l
d
If a symbol name (e.g: Values") was defined for the DB, the data block can also be
opened with the instruction OPN Values" .

Accessing a DB

The instructions for read (Load) or write (Transfer) access to a DB are shown in the
slide.
If the DB was already opened, the simple Load or Transfer instruction is enough.
The combined instruction, for example, L DB19.DBW2 includes the DB you want.
The instruction contains the opening of the data block.

Symbolic Access

A symbolic access is only possible if the following requirements are fulfilled:


1. The DB was given a symbolic name in the symbol table.
2. The individual data elements in the data block were given symbolic names
with the LAD/STL/FBD Editor.
Example: The instruction
L Values".Number opens the DB with the name
Values' and loads the data element with the name Number'.

Note

As a rule,
rule you should use the symbolic access of DBs
DBs. This has the following
advantages:
the program is easier to read,
it guarantees that the correct DB is accessed,
it is easier to make corrections to the data structure in the DB later on.
With absolute accesses to the DB, you must manually correct all program
locations with DB accesses. With symbolic accesses, it is easy to make
adjustments using a source program.
Working with Source Programs is dealt with in an advanced programming
course
course.

Training Center
for Automation and Drives

Page 12

ST-7PRO1
Data Storage in Data Blocks

Introduction

An open data block remains open until a new DB is opened or until a combined
i t ti ((e.g. L DB4
instruction
DB4.DBW6)
DBW6) accesses another
th DB.
DB

FC Block Calls

If an OB block or an FC block is exited because another function is called, the


current data block remains valid. When you return to the calling FC, the data block
that was valid when you exited is reopened.

FB Block Calls

A function block call is different. An instance DB is always assigned to an FB (more


information in the chapter "Functions and Function Blocks"). With the call of a
function block, the asssigned
g
instance DB is automatically
y opened.
p
When you return to the calling block, the previously opened global DB is no longer
valid.
This means that after FB calls, you must reopen the required global DB.

Note

You can open a DB by using the relevant instruction e.g. OPN DB. You can also
open the DB by passing parameters to parameter-assignable blocks. Thus, you can
use "DB 4.DBW6", for example, as the actual parameter. In this case, DB 4 is
opened.
Recommendation: If possible, use the fully-qualified access to data blocks.

Training Center
for Automation and Drives

Page 13

ST-7PRO1
Data Storage in Data Blocks

Use

User-defined
User
defined data types are used for:
creating structured data blocks,
creating arrays, that contain the same structure several times,
creating local variables in FC, FB with a given structure (see the chapter
"Functions and Function Blocks").
The user-defined data types are stored as UDT blocks on the hard disk. They are
used as templates", to save you typing when you create a data block.

User Defined
User-Defined
Data Type (UDT)

Training Center
for Automation and Drives

User-defined
User
defined data types are made up of elementary data types or other user
userdefined data types.
These data types cannot be stored in the PLC.
Example: Storing recipe data (see next pages).

Page 14

ST-7PRO1
Data Storage in Data Blocks

Entering a Data Type You create a UDT in the LAD/STL/FBD Editor by selecting the menu options File - >
N
New
and
d th
the Obj
Objectt name UDT..
UDT .
You then enter the data structure you want.
You must fill in the "Name" and "Type" columns, but you can leave the Initial
Value" and Comment" columns blank.
Saving a Data Type

Training Center
for Automation and Drives

Finally, you save the data structure (mouse click on the "Save icon).

Page 15

ST-7PRO1
Data Storage in Data Blocks

Creating a DB

When you have defined a data type and saved it as a UDT block, you can create
severall d
data
t bl
blocks
k with
ith th
the same d
data
t structure.
t t

What to DO

1.
2.
3.
4.
5.

Training Center
for Automation and Drives

Select the menu options File - > New in the Program Editor.
Select the project and user program and a DB.
Activate the option "Data block referencing a user-defined data type".
Select the UDT block you want in the Reference" box.
Save the data block.

Page 16

ST-7PRO1
Data Storage in Data Blocks

Example

If you need the same structure several times in one data block, you can use a UDT
as a data type
type, in an array,
array for example
example.
If the data block DB11 was assigned the symbol name Cake" in the symbol table, a
symbolic access from the user program can appear as follows:
L "Cake".Recipe[2].Eggs
Load the Number of Eggs" from the second recipe from the data block Cake.

Notes

You have to switch to "Data View" to be able to change


g the amount of the
ingredients. You can then overwrite the initial values in the "Actual Value column
with the amounts you want.
To change the structure of a UDT block later on, you have to re-create the data
blocks that contain UDTs. As well, all accesses to this data block must be updated.
The easiest way to do this is by using a source program.
You can also give UDT blocks symbol names.

Training Center
for Automation and Drives

Page 17

ST-7PRO1
Data Storage in Data Blocks

Goal

In an earlier exercise, you stored the production data (full, empty and broken
bottles)
b
ttl ) iin memory words.
d
These memory words are not retentive. The production data is lost if a complete
restart is carried out. To prevent this from happening, you should store the
production data in a data block.

What to Do

1. Create a data block DB 5 in the S7 program "FILL" containing the specified


variables and the data type INT.
2. Change the FC 18 block in such a way, that the production data (full,
empty
p y and broken bottles)) is stored in memory
y words in DB 5 instead.
3. Download the blocks and test your solution on the training unit.

Result

It should work.

Training Center
for Automation and Drives

Page 18

ST-7PRO1
Data Storage in Data Blocks