Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Storage in Data Blocks

Similar presentations

Presentation on theme: "Data Storage in Data Blocks"— Presentation transcript:

1 Data Storage in Data Blocks
Contents Page Storage Areas for Data ………… Data Blocks (DBs) … Overview of Data Types in STEP Elementary Data Types in STEP Complex Data Types ……… Example of a Structure Example of an Array Creating a New Data Block Entering, Saving, Downloading and Monitoring a Data Block Addressing Data Elements … Accessing Data Elements Validity of an Open DB …… User-Defined Data Type (UDT) … Entering a UDT Block Creating a Data Block Referencing a Data Type Example: Array from UDTs Exercise: Program for a Bottling Plant - Data Storage ……

2 . Storage Areas for Data Bit memories DBz PIQ DBy PII DBx I/O area
Data blocks DBx DBy DBz . Bit memories PIQ L stack PII I/O area 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.

3 Data Blocks (DBs) Accessible to all blocks Function FC10 OB1
Global data DB20 Accessible to all blocks Function FC10 OB1 Function FC20 Instance data DB5 Instance DB for FB1 Function block FB1 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.

4 Overview of Data Types in STEP 7
• Bit data types (BOOL, BYTE, WORD, DWORD, CHAR) • Mathematical data types (INT, DINT, REAL) • Time types (S5TIME, TIME, DATE, TIME_OF_DAY) Elementary data types (up to 32 bits) • Time (DATE_AND_TIME) • Array (ARRAY) • Structure (STRUCT) • Character chain (STRING) Complex data types (longer than 32 bits) 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 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. Data type UDT (User Defined Type) User-defined data types (longer than 32 bits)

5 Elementary Data Types in STEP 7
Keyword Length (in bits) Example of a constant of this type BOOL 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 DINT REAL or 34.5E-12 TIME 32 T#2D_1H_3M_45S_12MS DATE 16 D# TIME_OF_DAY 32 TOD#12:23:45.12 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# ). 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 (e.g.: D# = 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: = DW#16#05265B77).

6 Complex Data Types Keyword Length (in bits) Example
DATE_AND_TIME 64 DT# :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 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".

7 Example of a Structure Motor_data Operating Speed, data type Integer
Structure with the name "Motor_data" (several elements with different data types) Rated Current, data type Real Startup Current, data type Real Turning Direction, data type Bool Display in the Program Editor (Data block DB 1): 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").

8 Example of an Array Measuring_point 1. Measuring_point, data type Real
Array with the name "Measuring_point" (several elements of the same data type) Display in the Program Editor (Data block DB 2): 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".

9 Creating a New Data Block
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 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" 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".

10 Entering, Saving, Downloading and Monitoring a Data Block
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).

11 Addressing Data Elements
8 Bits 7 Data Byte 0 DBB 0 Data Byte 1 DBW 0 Data Byte 2 DBD 0 Data Byte 3 DBX 4.1 Data Byte 8191 DBD 8188 DBW 8190 DBB 8191 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.

12 Accessing Data Elements
Fully-qualified Access Traditional Access DB 19 (Symbol name: Values) absolute symbolic OPN DB19 A DBX 0.0 1) or A DB19.DBX0.0 or A “Values".Start 1 2 3 4 5 6 7 8 9 OPN DB19 L DBW2 Number or L DB19.DBW2 or L “Values".Number OPN DB19 L DBB5 Loop or L DB19.DBB5 or L “Values".Loop Opening a DB The instruction “OPN DB..." opens a global data block. If a global DB was already open, it is automatically closed. 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, you should use the symbolic access of 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. Data bit 0.0 with the element name "Start" 1)

13 ! Validity of an Open DB OB 1 FC 1 DB 4 CALL FC 1 T DBW 4
OPN DB 4 L DBW2 DB 4 T DBW 8 CALL FC 2 FC 2 DB 5 DB 5 OPN DB 5 L DBB6 DB 6 OPN DB 6 L DBB6 T DBW 2 DB 4 DB 5 L DBW 0 DB 2 T DB2.DBB 0 DB 2 L DBW 4 FB 1 CALL FB1, DB1 ??? ! Introduction An open data block remains open until a new DB is opened or until a combined instruction (e.g. L DB4.DBW6) accesses another 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 instance DB is automatically opened. 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. L DBW 10 DB ??

14 User-Defined Data Type (UDT)
UDT block as Template Global DB (Example) Flour Milk Eggs Yeast Sugar Recipe 1 Flour Milk Eggs Yeast Sugar Array with 3 elements of type UDT Recipe 2 Flour Milk Eggs Yeast Sugar Flour Milk Eggs Yeast Sugar DB created according to UDT Recipe 3 Flour Milk Eggs Yeast Sugar Use 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 types are made up of elementary data types or other user- Data Type (UDT) defined data types. These data types cannot be stored in the PLC. Example: Storing recipe data (see next pages).

15 Entering a UDT Block Entering a Data Type You create a UDT in the LAD/STL/FBD Editor by selecting the menu options File - > New and the Object name 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 Finally, you save the data structure (mouse click on the "Save“ icon).

16 Creating a Data Block Referencing a Data Type
Creating a DB When you have defined a data type and saved it as a UDT block, you can create several data blocks with the same data structure. What to DO 1. Select the menu options File - > New in the Program Editor. 2. Select the project and user program and a DB. 3. Activate the option "Data block referencing a user-defined data type". 4. Select the UDT block you want in the “Reference" box. 5. Save the data block.

17 Example: Array of UDTs Declaration View Data View
Example If you need the same structure several times in one data block, you can use a UDT as a data type, in an array, for 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 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.

18 Exercise: Program for a Bottling Plant - Data Storage
Data block DB5 Empty bottles (MW 100) Variable: empty Full bottles (MW102) Variable: full "Broken" bottles (MW 104) Variable: broken Goal In an earlier exercise, you stored the production data (full, empty and broken bottles) in memory words. 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 and broken bottles) is stored in memory words in DB 5 instead. 3. Download the blocks and test your solution on the training unit. Result It should work.

Download ppt "Data Storage in Data Blocks"

Similar presentations

Ads by Google