You are on page 1of 16

Course T300, Engineering an 800xA System

Chapter 6 Variables and Data Types

TABLE OF CONTENTS
Chapter 6 Variables and Data Types ............................................................................................................................................. 6-1
6.1 General Information ............................................................................................................................................................. 6-3
6.1.1 Description .................................................................................................................................................................... 6-3
6.1.2 Objectives...................................................................................................................................................................... 6-3
6.1.3 Reference Documentation............................................................................................................................................. 6-3
6.2 Kind of Variables ................................................................................................................................................................. 6-4
6.2.1 Application Variables ................................................................................................................................................... 6-4
6.2.2 Program Variables......................................................................................................................................................... 6-7
6.2.3 Control Module Variables ............................................................................................................................................ 6-8
6.2.4 External Variables......................................................................................................................................................... 6-9
6.2.5 Access Variables ......................................................................................................................................................... 6-10
6.3 Declaration of Variables .................................................................................................................................................... 6-10
6.3.1 General ........................................................................................................................................................................ 6-10
6.3.2 Declaring Variables in POU ....................................................................................................................................... 6-11
6.4 Structured Data Types........................................................................................................................................................ 6-13
6.4.1 Components................................................................................................................................................................. 6-14
6.4.2 Dot Notation................................................................................................................................................................ 6-15
6.5 Project Constants................................................................................................................................................................ 6-16

6-1

Course T300, Engineering an 800xA System

6-2

Engineering an 800xA System

6.1 General Information


6.1.1 Description
This chapter contains facts about variables and data types.

6.1.2 Objectives
On completion of this chapter you will be able to:

Describe the scope of local variables, global variables and access variables.

Describe the influence of attributes on variables.

Declare variables with different attributes and of different data types.

Access a component in a variable of a certain structured data type.

Declare project constants.

Declare user-defined structured data types.

6.1.3 Reference Documentation


In the Online help of the Control Builder: Index tab, keyword data type,
In the Online help of the Control Builder: Index tab, keyword variable

6-3

Engineering an 800xA System


Chapter 6 Variables and Data Types

6.2 Kind of Variables


6.2.1 Application Variables
6.2.1.1 Global Variables
Global variables are declared in the Global Variables tab of the Application editor, and
can be accessed directly without any declaration in the program editor. A global
variable can be used in any program, function block or control module within the
application where it is declared. It is possible to assign a global variable to a board
channel.

6.2.1.1.1 Scope

6-4

Engineering an 800xA System

6.2.1.1.2 Where to create?


Open the Context Menu of an Application object (In the PPA or in the Control
Builder) and select the entry Application Editor (For CBM: Editor). Select the tab card
Global Variables and create a variable as displayed in the picture below.

6.2.1.2 Application Variables


Application variables are declared in the Variables tab of the Application editor, and
can not be accessed by programs. An application variable can only be connected to
control modules residing directly under the root module within the application where
it is declared. It is possible to assign a application variable to a board channel.

6-5

Engineering an 800xA System


Chapter 6 Variables and Data Types

6.2.1.2.1 Scope

6.2.1.2.2 Where to create?


Open the Context Menu of an Application object (In the PPA or in the Control
Builder) and select the entry Application Editor (For CBM: Editor). Select the tab card
Variables and create a variable as displayed in the picture below.

6-6

Engineering an 800xA System

6.2.2 Program Variables


Program variables are declared in the Variables tab of the POU editor. They can only
be used in the program where they are declared. It is possible to assign a program
variable to a board channel.

6.2.2.1 Scope

6.2.2.2 Where to create?


Open the Context Menu of a Program object (In the PPA or in the Control Builder)
and select the entry Program Editor (For CBM: Editor). Select the tab card Variables
and create a variable as displayed in the picture below.

6-7

Engineering an 800xA System


Chapter 6 Variables and Data Types

6.2.3 Control Module Variables


Control module variables are declared in the Variables tab of the POU editor. They
can only be used in the control module where they are declared. It is possible to assign
a single control module variable to a board channel as well.

6.2.3.1 Scope

6-8

Engineering an 800xA System

6.2.3.2 Where to create?


Open the Context Menu of a Control Module Type object (In the PPA or in the
Control Builder) and select the entry Control Module Type Editor (For CBM: Editor).
Select the tab card Variables.

6.2.4 External Variables


Described in IEC 61131-3.
If global variables are to be used inside function block types or control modules, they
must be declared as external variables in the function block type editor or control
module editor.
External variables do not have to be declared in programs. Instead a global variable
declared in the Application can be directly accessed from the program.

6-9

Engineering an 800xA System


Chapter 6 Variables and Data Types

6.2.5 Access Variables


If Controllers should be able to read or write a variable in another Application, the
Controller that owns the variable has to share it on the network. Declaring an access
path to the variable in the Controller that owns the variable does this sharing.

6.3 Declaration of Variables


6.3.1 General
A data type is a description of the structure of a variable or a parameter. The system
supports simple data types such as Boolean (bool), integer (int), and real. You can
combine these simple data types into new data types called structured data types. A
structured data type can contain other structured data types. The library SystemLib
contains all of the systems predefined data types (for example, BoolIO, date_and_time
etc). In most libraries, additional data types are declared.

6-10

Engineering an 800xA System

6.3.1.1 Why Variables and Data Types?


The variables in the program are the holders of values. They are declared by giving
each a name (also called identifier) and by assigning a data type. A single program
may contain many variables and they must be declared when they are used. Below is
an example of variable declarations.

6.3.1.2 Simple Data Types


Simple data types are primarily grouped into five categories: numerical, date and time,
string, bit string (like bool) and structured data types. Each category can then be
subdivided into sub ordinary categories such as numerals (int and real). These can then
be subdivided into subordinated items like dint, uint etc. In the table below you will
find a table containing some of the predefined data types.
Data type

Description

Bits

Initial value

bool

Boolean

False, 0

Dint

double integer

32

Int

Integer

16

Uint

Unsigned integer

16

String

Character string *

Word

Bit string

16

DWord

Bit string

32

Time

Duration

Date_And_Time

Date and time of day

Real

Real number

T#0s
1979-12-31-00:00:00
32

0.0

*) String length is 40 characters by default, but this can be changed by entering


string[xx] as the data type, where xx is the string length. Maximum value is 140.

6.3.1.3 Key Words


Key words are words that are reserved by the system. For example true is used to
indicate a boolean status or if an event is true. Key words can not be used for anything
else than the purpose for which they are reserved. If you use a key word incorrectly an
error message will be given. You may not use key words as names of variables.

6.3.2 Declaring Variables in POU


The variables used in the POU editor should be declared in the Variables tab of the
declaration pane.

6-11

Engineering an 800xA System


Chapter 6 Variables and Data Types

6.3.2.1 Name
It is best to give the variables simple and explanatory names. It is advisable to begin a
variable name with a capital letter, and names consisting of more than one word
should have capital letters at the beginning of each new word. Examples of
recommended variable names are DoorsOpen or PhotoCell. Certain identifiers
(names) are reserved by the system and cannot be used for other purposes. If a
reserved word is used an error message is given in the message pane: Invalid
variable identifier, Reserved word or Name xxx not valid.

6.3.2.2 Data Type


Press < Ctrl J > in the Data Type column, to find a suitable Data Type depending on
the operation that should be done

6.3.2.3 Attributes
The following attribute can be given to a variable:
Name

Description

No attribute

The value of the variable is not maintained after a restart. Instead it is set
to the initial value of the variable. If the variable has no initial value
assigned to it, it will get the default value of the data type (see table in
page 6.3)

retain

The value of the variable is maintained after a warm restart. The system
sets retain on all variables by default. To override this the attribute must be
left empty.

coldretain

The value of the variable is maintained after a warm or cold restart.


This attribute overrides the retain attributes in a structured data type.
You cannot change the value of the variable once given.

constant

hidden

This attribute overrides the coldretain and retain attributes in a structured


data type.
The variable will not be visible to the OPC server, and therefore not
available in the HMI system.

Note! Variables declared in a Contol Module can also have the attributes State or
Nosort. These are described in detail in chapter 10.

6.3.2.4 Initial Value


At the declaration of a basic Data Type variable, you can give the variable an initial
value that is assigned to the variable when the application is executed. This setting
overrides the default value that would have been used if no initial value were
specified. In the table under the header Simple Data Types are some default initial
values to the most common data types presented.

6.3.2.4.1 Initial Values at Reboot


If the variable does not have the attribute retain, its value is reset to its initial value at
warm boot. At cold boot the value of the variable is set to its initial value, if such a

6-12

Engineering an 800xA System

value is given. Otherwise the value of the variable is set to the default value of the data
type.

6.3.2.5 I/O-address
It is suitable to make the I/O connection from the I/O module editor. This is described
in chapter 15 Basic Hardware Configuration.

6.3.2.6 Description
Description describes and/or provides information about the variable. A short
descriptive text, for example, would include an explanation of the cause of a condition
or a simple event; 'Pump 1 is running'.

6.4 Structured Data Types


Structured data types are data types with components of simple or structured data
types. This concept is best explained through an example: Given a motor who has the
following characteristics:

Start -

The motor starts

Stop-

The motor stops

Right spin -

The motor spins right.

Left spin -

The motor spins left.

Speed -

The motor speed.

Feedback -

Yes, the motor spins

You can collect all these characteristics into one signal using a structured data type.
Given the specifications above you create a structured data type called Motor_Type,
with subordinate identifiers Start (Boolean), Stop (Boolean), RightSpin (Boolean),
LeftSpin (Boolean), Speed (real) and Feedback (Boolean).

6-13

Engineering an 800xA System


Chapter 6 Variables and Data Types

It is possible to create a Data Type in an Application or in a Library. The Data Type


can be of a structured type, making nested structured data types possible. The picture
below shows the data type editor for the motor type.

6.4.1 Components
The variables in the structured Data Type are called Components.

6.4.1.1 Name
It is best to give the components simple and explanatory names.

6.4.1.2 Data Type


Press < Ctrl+J > in the Data Type column, to find a suitable Data Type depending on
the operation that should be done.

6.4.1.3 Attributes
Press < Ctrl+J > in the Attribute column, to find a suitable attribute to be assigned.

6.4.1.4 Initial Value


You can specify initial values for simple data types only, not for the whole structured
data type. To assign initial values to a structured data type, specify initial values for
the separate components. If the components are also structured, then you must go on
to a deeper level until you reach the level where the simple data type is defined. In that
respect the initial values are bound to the data type definition and cannot be reset at
the declaration of a structured type variable.

6-14

Engineering an 800xA System

6.4.1.5 Description
In the description fields a short description can be written at declaration of the Data
Type component.

6.4.2 Dot Notation


To get access to the values, you cannot use the tank variable as is, because it is a name
for the entire (main) structure. To gain access to the components, use the syntax
<main_identifier>.<component>

If you want to set the Full component of the Tank variable to 1 (i.e. "True"), write:
tank.full := true ;

The rest of the subordinate variables are accessed in a similar way. A structured data
type is a data type that contains several components and can thus have several values,
one for each component.

6-15

Engineering an 800xA System


Chapter 6 Variables and Data Types

6.5 Project Constants


Use project constants for useful software settings you want to make globally available
to libraries and applications. With project constants, you can adapt such settings to an
individual project without having to modify any source code or having to introduce
parameters passing values to all concerned types.
Project constants are declared at the top level of libraries and projects. They are
globally visible, and can be used wherever a constant value is permitted, e.g., in
program code and for variable initialization.
Typically, project constants are declared in a library and given default values. They
are then used, for example, in code inside function block types. In a project that
includes the library, the project constant settings can be overridden by project specific
settings, which are kept and saved with the project.
You can insert, edit and group project constants in the Project Constant editor. It is
possible to define your own Project Constants in a library or project. Select Tools>
Project Constants from the menu in the project explorer or right click on the Library
(must be a user defined library) and select Project constants. Any project constant
declared in a library will be a part of that library. They will however also be
incorporated in the main list of project conctants in the Tools>Project Constants
menu.

6-16