Professional Documents
Culture Documents
Page
g
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.
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.
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
_ _
Training Center
for Automation and Drives
Page 5
ST-7PRO1
Data Storage in Data Blocks
Complex
D t T
Data
Types
User-Defined
Data Type
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
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
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.
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
Address
Name
Type
Initial Value
Comment
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
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
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
FB Block Calls
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
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
Result
It should work.
Training Center
for Automation and Drives
Page 18
ST-7PRO1
Data Storage in Data Blocks