Anda di halaman 1dari 13

Data Storage in Data Blocks

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.1

Contents Page

Storage Areas for Data …………...................................................................................................... 2


Data Blocks (DBs) ......................................................................................................................... 3
Overview of Data Types in STEP 7 ................................................................................................. 4
Elementary Data Types in STEP 7 .................................................................................................... 5
Creating a New Data Block ............................................................................................................ 6
Entering, Saving, Downloading and Monitoring a Data Block .......................................................... 7
Addressing Data Elements …......................................................................................................... 8
Accessing Data Elements ............................................................................................................. 9
Exercise: Counting the Transported Parts (FC 18) ........................................................................... 10
Complex Data Types ………............................................................................................................ 11
Example of an Array ........................................................................................................................... 12
Example of a Structure ..................................................................................................................... 13

SITRAIN Training for ST-7PRO1


Automation and Drives Page 1 Data Storage in Data Blocks
Storage Areas for Data

Bit memories
DBz
PIQ
DBy . .
PII .
DBx
I/O area

L stack Data blocks

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.2

Overview Apart from program blocks, a user program also consists of data containing
information about process states, signals, etc., which are then processed
according to 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 variables of a logic block, including 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.

SITRAIN Training for ST-7PRO1


Automation and Drives Page 2 Data Storage in Data Blocks
Data Blocks (DBs)

Accessible to all blocks

Function
OB1
FC10
Global data

DB20
Function
FC20

Instance DB for FB1

Function Instance data


block
FB1 DB5

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.3

Overview Data blocks are used for storing user data. Like the logic blocks, data blocks
take up space in the user memory. The data blocks contain variable data (e.g.
numeric 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.
• 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.

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.

SITRAIN Training for ST-7PRO1


Automation and Drives Page 3 Data Storage in Data Blocks
Overview of Data Types in STEP 7

Elementary • Bit data types (BOOL, BYTE, WORD, DWORD, CHAR)


data types
• Mathematical data types (INT, DINT, REAL)
(up to 32 bits)
• Time types (S5TIME, TIME, DATE, TIME_OF_DAY)

• Time (DATE_AND_TIME)
Complex
data types • Array (ARRAY)
(longer than 32 bits)
• Structure (STRUCT)
• Character chain (STRING)

User-defined data types


(longer than 32 bits) Data type UDT (User Defined Type)

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.4

Overview Data types determine the properties of data, i.e. the way the contents of one or
more associated addresses are to be represented and the permissible range of
values.
The data type also determines which operations can be used.

Elementary Elementary data types are predefined in accordance with IEC 1131-3. The data
Data Types 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 Complex data types can only be used in conjunction with variables declared in
Data Types 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 A user-defined data type can be used for data blocks or as a data type in a
Data Types variable declaration table.
You create UDTs with the Data Block Editor.
The structure of a UDT can contain groups of elementary and/or complex data
types.

SITRAIN Training for ST-7PRO1


Automation and Drives Page 4 Data Storage in Data Blocks
Elementary Data Types in STEP 7

Keyword Length (in bits) Example of a constant of this type

BOOL 1 1 or 0
BYTE 8 B#16#A9
WORD 16 W#16#12AF
DWORD 32 DW#16#ADAC1EF5
CHAR 8 'w'

S5TIME 16 S5T#5s_200ms

INT 16 123
DINT 32 65539
REAL 32 1.2 or 34.5E-12

TIME 32 T#2D_1H_3M_45S_12MS
DATE 16 D#1993-01-20
TIME_OF_DAY 32 TOD#12:23:45.12

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.5

BOOL, BYTE, WORD Variables of the data type BOOL consist of one bit, variables of data types
DWORD, CHAR BYTE, WORD, DWORD are sequences of 8, 16 and 32 bits respectively. 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. 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 TIME_OF_DAY takes up a doubleword. It contains the


number of milliseconds since the beginning of the day (0:00 o‘clock) in the form
of an unsigned integer. (e.g.: TOD#23:59:59.999 = DW#16#05265B77).

SITRAIN Training for ST-7PRO1


Automation and Drives Page 5 Data Storage in Data Blocks
Creating a New Data Block

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.6

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

"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 that can be processed 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" In this dialog box, you select the type of data block to be created:
Dialog Box • Data Block (global data block)
• 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".

SITRAIN Training for ST-7PRO1


Automation and Drives Page 6 Data Storage in Data Blocks
Entering, Saving, Downloading and Monitoring a Data Block

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.7

Entering Data You enter the individual data elements in the table. To do so, you select the first
empty line in the "Name" column and enter the element‘s description. You can
jump to the other columns - Type, Initial Value and Comment - by using the Tab
key.

Columns The meanings of the columns are as follows:

• Address - is entered by the Program Editor when you save.


It is the first byte address occupied by the variable in the
data block.
• Name - symbolic name of the element.
• Type - data type (you select this with the right mouse button).
• Initial Value - 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.
• Comment - for documenting the data element (optional).

Save You save the data block on the hard disk of the programming device using the
"Diskette" 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).

SITRAIN Training for ST-7PRO1


Automation and Drives Page 7 Data Storage in Data Blocks
Addressing Data Elements

8 Bits
7 0
Data Byte 0 DBB 0
Data Byte 1 DBW 0
Data Byte 2
DBD 0
Data Byte 3
DBX 4.1

DBD 8188

DBW 8190
Data Byte 8191 DBB 8191

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.8

General You address the data elements of a data block byte-by-byte, just as you do bit
memories.
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.

SITRAIN Training for ST-7PRO1


Automation and Drives Page 8 Data Storage in Data Blocks
Accessing Data Elements
DB 19 "Values" Traditional Fully-qualified Access
Access
Add. Name Type absolute symbolic
0.0 Status BOOL OPN "Values"
or A DB99.DBX0.0 or A "Values".Status
A DBX 0.0
1.0 States BYTE
2.0 Number INT
OPN DB 99
or L DB99.DBB1 or L "Values".States
L DBB 1
4.0 Weight[1] REAL
OPN "Values"
or L DB99.DBW2 or L "Values".Number
T DBW 2

OPN DB 99
8.0 Weight[2] REAL or L DB99.DBD8 or L "Values".Weight[1]
L DBD 8

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.9

Traditional Access In the traditional (typical in the S5 world) access of data blocks, these have to be
explicitly opened before the actual access. This can take place absolutely or
symbolically with the instruction OPN DB 99 or OPN "Values" (see
example). Thus, the data block that was open first is automatically closed. Then
the individual data elements can be accessed bit-by-bit (DBX...), byte-by-byte
(DBB...), word-by-word (DBW...) or doubleword-by-doubleword (DBD...) without
a data block having to be specified.
Disadvantages:
• When accessing data elements you have to make sure that the correct data
block is opened.
• Access can only be made absolute, thus it is up to the programmer to make
sure that he (reaches) the correct value in the data block. If the DBW3 in the
example were loaded, then neither the value of the variables Number nor
Weight[1] would be loaded, but an invalid value.
• Absolute accesses hamper correction possibilities and make the program
difficult to read.

Fully-qualified The opening of a data block and thus the closing of the one that was opened (!)
Access... first is integrated in the fully-qualified access. A fully-qualified access can be
made absolute and symbolic:

...absolute Opening of the data block and access of the data element is made absolute and
in combination with an instruction. Similar disadvantages to those of the
traditional access already described result.

...symbolic A symbolc access of a variable in a data block is only possible as fully-qualified


and completely symbolic. The Editor does allow the "mixing" of absolute and
symbolic addresses during editing, however, it switches over to completely
symbolic after the entry has been confirmed.

SITRAIN Training for ST-7PRO1


Automation and Drives Page 9 Data Storage in Data Blocks
Exercise: Counting the Transported Parts (FC 18)

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.10

Function Up Till Now: • The parts transported in AUTO mode are counted (by addition in MW 20,
in FC 18 auxiliary memory marker for RLO edge detection M 18.0), as soon as they
reach location Final Control or have passed the light barrier.
• The ACTUAL number of parts of the transported parts is displayed on the
BCD digital display.
• You can set the SETPOINT number of parts of how many parts are to be
transported using the BCD pushwheel button. When the given SETPOINT
number of parts is reached, it is displayed on the red LED (Q 20.4 / Q 8.4)
at location Final Control.
• As long as the message SETPOINT number of parts reached (red LED)
exists, no other transport function can be started (lock-out in FC 16).
• The ACTUAL number of parts (MW 20) is set to 0 through the momentary
contact switch at location Final Control when the system is switched off or
when the message Setpoint number of parts reached is acknowledged.

Task: • The functionality programmed in FC 18 is to remain unchanged. However,


do not continue to use MW 20 for the counting per addition, rather use the
variable ACT_Number_of_Parts (INT) to be declared in DB 18.
For the RLO edge detection needed for counting use the variable Edge_Aux
(BOOL), instead of the auxiliary memory marker M 18.0. This variable must
also be declared in DB 18.

What To Do: • Modify the data block DB 18 (see slide) with the variables ACT-number_of_
parts (INT) and Edge_Aux (BOOL) and download it to the CPU.
• Give DB 18 the symbolic name "DB_Parts" in the global symbol table
• Make the changes to FC 18 as described in the task. For this, work with
fully-qualified, symbolic accesses!

SITRAIN Training for ST-7PRO1


Automation and Drives Page 10 Data Storage in Data Blocks
Complex Data Types
Keyword Length (in bits) Example
DATE_AND_TIME 64 DT#97-09-24-12:14:55.0

STRING 8 * (number of ´This is a string´


(character string with characters +2) ´SIEMENS´
max. 254 characters)

ARRAY user-defined Measured values: ARRAY[1..20]


(Group of elements INT
of the same data type)

STRUCT user-defined Motor: STRUCT


(Group of elements Speed : INT
of different data types) Current: REAL
END_STRUCT

UDT UDT as block UDT as array element


(User Defined Data Type = user-defined
“Template" consisting of STRUCT Drive: ARRAY[1..4]
elementary or complex Speed : INT UDT1
data types Current: REAL
END_STRUCT

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.11

Complex Complex data types (arrays and structures) consist of groups of elementary or
Data Types complex data types.
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.
Variables of the complex data types can only be declared in global data blocks
and as parameters or local variables of logic blocks.

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


Data Type UDT blocks (UDT1 ... UDT65535) and can be used as a "template" in another
variable‘s 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. 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".

SITRAIN Training for ST-7PRO1


Automation and Drives Page 11 Data Storage in Data Blocks
Example of an Array
Measuring_point
1. Measuring_point, data type Real
2. Measuring_point, data type Real
Array with the name "Measuring_point"
3. Measuring_point, data type Real (several elements
• of the same data type)


10. Measuring_point, data type Real

Display in the Program Editor (Data block DB 2):

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.12

Array An array consists of several elements of the same data type. In the slide above,
you can see the array "Measuring_point" with 10 elements of the data type
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.

Note To create an empty data block, you define an array with the data type you want.

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".

SITRAIN Training for ST-7PRO1


Automation and Drives Page 12 Data Storage in Data Blocks
Example of a Structure
Motor_data
Operating Speed, data type Integer
Rated Current, data type Real Structure with the name "Motor_data"
(several elements
Startup Current, data type Real with different data types)
Turning Direction, data type Bool

Display in the Program Editor (Data block DB 1):

SIMATIC S7 Date: 22.08.2007


SITRAIN Training for
Automation and Drives
Siemens AG 2000. All rights reserved. File: PRO1_09E.13

Structure The slide shows an example of a structure with the name "Motor_data". The
structure consists of several elements of different data types. The individual
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 "Drive_1".Motor_data.operating_speed
"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 The keyword for a structure is "STRUCT". The end of a structure is indicated
in DB by "END_STRUCT". A name is defined for the structure (in the example:
"Motor_data").

SITRAIN Training for ST-7PRO1


Automation and Drives Page 13 Data Storage in Data Blocks

Anda mungkin juga menyukai