You are on page 1of 33

What is IEC 61131?

Chapter contents : IEC 61131 Basics


What is IEC 61131?...........................................................................................................................1

Configuration elements in IEC 61131−3.........................................................................................2


Configurations in IEC 61131−3............................................................................................2
Resources in IEC 61131−3..................................................................................................2
Tasks in IEC 61131−3..........................................................................................................2

POUs in IEC 61131−3........................................................................................................................3


Overview..............................................................................................................................3
Function POUs.....................................................................................................................3
Function block POUs............................................................................................................3
Program POUs.....................................................................................................................4

Instantiation......................................................................................................................................5

Programming languages in IEC 61131−3.......................................................................................6


Instruction List (IL)................................................................................................................6
Structured Text (ST).............................................................................................................6
Function Block Diagram (FBD).............................................................................................6
Ladder Diagram (LD)............................................................................................................6
Sequential Function Chart (SFC).........................................................................................7

Data types in IEC 61131−3...............................................................................................................8


Elementary data types..........................................................................................................8
Generic data types...............................................................................................................9
User defined data types.....................................................................................................10
Array data type............................................................................................................10
Programming example for an array.............................................................................12
Structured data type....................................................................................................13
String data type...........................................................................................................14

Variables in IEC 61131−3...............................................................................................................16


Scope of variables..............................................................................................................16
Symbolic variables.............................................................................................................16
Directly represented and located variables........................................................................17
Retentive variables.............................................................................................................18
Initializing variables............................................................................................................19
Variable declaration keywords...........................................................................................19

Literals in IEC 61131−3...................................................................................................................21


Numeric literals...................................................................................................................21
Character string literals......................................................................................................22
Duration literals..................................................................................................................22
Date and time of day literals...............................................................................................22

IEC 61131−3 and your programming system...............................................................................23


Projects in your programming system................................................................................23
Libraries in your programming system...............................................................................23
POUs in your programming system...................................................................................24
Data types in your programming system............................................................................25
Variables in your programming system..............................................................................26
Configuration elements in your programming system........................................................27
Identifier..............................................................................................................................28

i
Chapter contents : IEC 61131 Basics
IEC 61131−3 and your programming system
Program and function block instances in the instance tree................................................29
Variable declaration keywords for program and FB instances...........................................30

ii
What is IEC 61131?
The standard IEC 61131 has been established to standardize the multiple languages, sets of
instructions and different concepts existing in the field of automation systems. The great variety of
PLC concepts has led to an incompatibility between the different PLC platforms and manufacturers.
The result was a great effort to be made for training, hard− and software investments.

IEC 61131 standardizes the programming languages, the interfaces between PLC and
programming system, the sets of instructions and the handling and structuring of projects. The
advantage of using IEC 61131 conform PLCs and programming systems is a portability of all
platforms and the use of same concepts reducing costs for automation systems.

The main changes that have come with IEC 61131−3 are:

• Declaration of variables is similar to the variable declaration in higher programming


languages.

• Declaration of data types is possible.

• Global and local data can be differentiated.

• Programming means symbolic programming.

1
Configuration elements in IEC 61131−3
An IEC 61131−3 conform PLC programming system reflects the hardware structure by
configuration elements. These configuration elements are basically configurations, resources and
tasks.

Configurations in IEC 61131−3


A configuration can be compared to a programmable controller system, e.g. a rack. In a
configuration one or several resources can be defined.

Resources in IEC 61131−3


A resource can be compared for example to a CPU which can be inserted in the above mentioned
rack or a remote CE device connected via TCP/IP or a bus interface board. In a resource global
variables can be declared, which are only valid within this resource. In a resource one or several
tasks can be executed.

Tasks in IEC 61131−3


Tasks determine the time scheduling of the programs associated with them. This means that
programs have to be associated to tasks. The settings of the task determine the time scheduling.

IEC 61131−3 describes different time scheduling models which results in different task types:

• Default task. Each resource can contain one default task. The default task is the task with
the lowest priority (lower than cyclic tasks) and is not time scheduled. After executing it
completely the system waits for a defined idle time. If then no cyclic task or any other task
with a higher priority has to be processed, the default task is executed again
automatically. This means that the default task is always executed (apart from the
necessary pre−defined idle period between two invocations), as long as no task with a
higher priority runs.

• Cyclic tasks are activated in a certain time interval and the program is executed
periodically.

• System tasks are called automatically by the PLC operating system if an error or a
change of the operational state of the PLC occurs. They are also known as system
programs or SPGs.

• Event or interrupt tasks are activated if a certain event has happened.

Each task has a certain priority. In so called preemptive scheduling systems, an active task with
low priority is interrupted immediately, when a task with higher priority becomes active due to a
certain event. In systems with non−preemptive scheduling, task interruptions by tasks with higher
priority are not possible.

The supported task types depend on the used PLC.

2
POUs in IEC 61131−3
Overview
According to IEC 61131−3 program organization units or POUs are the language elements of a
PLC program. They are small, independent software units containing the program code. The name
of a POU must be unique within the project.

Three different POU types are available:

• programs
• function blocks (FBs)
• functions

Each POU consists of two different parts: The variables declaration part and the code body part.
Both are designated as 'Worksheets'. In the declaration part all local variables are declared. The
instruction or code body part of a POU contains the instructions, programmed in the desired
programming language.

Function POUs
Functions are POUs with multiple input parameters and exactly one output parameter. Calling a
function with the same values returns always the same result. Return values can be single data
types. Within a function it is possible to call another function but not a function block or a program.
Recursive calls are not allowed. The abbreviation for functions is FU.

IEC 61131−3 describes standard functions. Depending on the used hardware and PLC type
possibly not all standard functions are available or firmware functions are provided.

The following is a list of standard IEC 61131−3 functions:

• Type conversion functions, such as INT_TO_REAL


• Numerical functions, such as ABS and LOG
• Standard arithmetic functions, such as ADD and MUL
• Bit−string functions, such as AND and SHL
• Selection and comparison functions, such as SEL and GE
• Character string functions, such as RIGHT and INSERT
• Functions of time data types, such as SUB with the data type TIME

For detailed information about IEC 61131−3 standard functions please refer to the
Function/Function block Online Help. For that purpose you can either select the menu item '? >
Help on FB/FU' or call the context sensitive help topic out of the Edit Wizard.

Function block POUs


Function blocks are POUs with multiple input/output parameters and internal memory. The value a
function block returns, depends on the value of its internal memory. Within a function block it is
possible to call another function block or functions but not a program. Recursive calls are not

3
Program POUs

allowed. The abbreviation for function blocks is FB.

IEC 61131−3 describes standard function blocks. Depending on the used hardware and PLC type
possibly not all standard function blocks are available or firmware function blocks are provided. All
FBs (IEC−defined, firmware FBs and user defined FB POUs) can easily be inserted into the code
body of a calling POU using the Edit Wizard. Each called FB has to be declared in the local
variables worksheet of the calling POU. Calling a FB in another POU is designated as instantiation.

The following is a list of standard IEC 61131−3 function blocks:

• Bistable elements, such as SR and RS


• Edge detection function blocks, such as R_TRIG and F_TRIG
• Counters, such as CTU and CTD
• Timer function blocks, such as TON and TOF

For detailed information about IEC 61131−3 standard function blocks please refer to the
Function/Function block Online Help. For that purpose you can either select the menu item '? >
Help on FB/FU' or call the context sensitive help topic out of the Edit Wizard.

Program POUs
Programs POUs usually contain a logical combination of function/function block calls. The behavior
and use of programs are similar to function blocks. Programs have input and output parameters
and they can have an internal memory. Recursive calls are not allowed.

Programs must be associated to tasks. The following figure shows a default task with two
programs:

4
Instantiation
IEC 61131−3 provides the possibility of instantiation. Instantiation means that a function block is
defined once and can be used several times.

This applies to all FBs: User defined FBs (developed in a FB POU), IEC defined FBs and firmware
FBs.

As function blocks always have an internal memory it is necessary to store their values for each
time the function block is used to a different memory area. This is done using instance names. The
instance name is declared in the variable declaration of the POU where the function block is going
to be used. Each instance has an associated identifier and contains the input and output
parameters.

The following example shows the FB instance declaration for the function block 'FB_exam'. This
declaration must be contained in the local variables worksheet of the calling POU.

VAR
drive1 : FB_exam;
drive2 : FB_exam;
END_VAR

The function block 'FB_exam', of which the code body has either been defined somewhere in the
project or is contained in a library, has got two instances. The instance name of the first instance is
'drive1', of the second 'drive2'. In the corresponding code body worksheet you can use the function
block 'FB_exam' twice, entering in both cases the correct instance name.

A function block can be instantiated in other function blocks or in program POUs. Instance names
are created automatically whenever a function block is inserted using the Edit Wizard.

Programs can only be instantiated within resources. Program instances are created by associating
a program to a task and entering an instance name in the corresponding dialog. See topic Instance
Tree.

Functions are called without instantiation because they do not have an internal memory.

5
Programming languages in IEC 61131−3
IEC 61131−3 defines the syntax of 5 programming languages and describes their representation
and language elements. We distinguish two textual and three graphical languages.

Instruction List (IL)


A code body programmed in the textual language IL consists of a sequence of instructions. Each
instruction starts at a new line. A line number is displayed in front of each line. Each line begins
with an operator followed by one operand. Additionally modifiers can be used. Operands in IL can
be variables, literals or instance names of function blocks. Operands are used together with
operators or functions in instructions. Due to the syntax highlighting the various elements are
displayed in different colors: operators and their modifiers are shown blue variables and operands
are black, comments are green.

In IL code bodies jumps and labels can be used. When processing the code, the program jumps
from the line containing the jump to the line with the label. Comments can be inserted to improve
the comprehensibility of the code.

Structured Text (ST)


A code body programmed in the textual language ST consists of statements and expressions. Each
code line must be terminated with a semicolon.

An expression is a construct which returns one value for the execution of statements. Expressions
consist of operators and operands. The operators have to be applied to the operands in the way
that the operator with the highest precedence is followed by the operators with the next lower
precedence. Operands in ST can be literals, variables or names of functions. Operands are used
together with operators in expressions.

Comments can be inserted to improve the comprehensibility of the code.

Function Block Diagram (FBD)


A code body programmed in the graphical language FBD is composed of functions and function
blocks which are connected with each other or with variables using lines. These lines can also be
connected with each other. In FBD networks it is not possible to connect outputs with outputs. The
set of connected objects is called FBD network.

When developing large networks, connectors can be used to improve the layout of the element's
structure in the worksheet. Connectors simply replace connection lines. Additionally jumps can be
used: For realizing jumps two objects have to be inserted in the same worksheet: a jump and label
(which is the target for the jump). Returns can be inserted to go back to a calling POU. Comments
can be inserted to improve the comprehensibility of the code.

Ladder Diagram (LD)


A code body programmed in the LD is composed of contacts and coils. According to IEC 61131−3
different types of contacts and coils can be used. Contacts lead (according to their type) the power

6
Sequential Function Chart (SFC)

from the left to the right. Coils store the incoming value. Both, contacts and coils, are assigned to
Boolean variables.

Contacts and coils are connected via lines and they are bounded on the left and on the right with
power rails. The state of the left power rail is considered ON all the time. The right rail is optional. In
addition to serial connections, parallel branches can be created. Parallel branches are also called
wired−Ors.

The set of connected objects is called LD network. Comments can be inserted to improve the
comprehensibility of the code.

Sequential Function Chart (SFC)


A code body programmed in the graphic language SFC is composed of steps and transitions which
are connected with directed links. One or several action blocks can be associated to a step.

An action block consists of an action and the corresponding action qualifier. Action qualifiers
specify how an action is affected by the step it is associated to. While a step is active, the
associated action is executed according to its action qualifier. The action can either be a Boolean
variable or an IL/ST/LD/FBD code body (called 'detail').

A transition represents the condition on that the process continues with the next step. If a transition
becomes TRUE the preceding step is executed once again and the succeeding step becomes
active. A transition can either be a Boolean variable or a directly connected Boolean expression in
FBD or LD. It is also possible to edit the code to be executed in an additional code body named
detail.

The set of connected objects is called SFC network. A SFC network must have one initial step
which is the first step to be executed when the SFC POU is called. Simultaneous branches
(executed simultaneously) or alternative branches (executed alternatively) can be inserted in the
SFC network.

7
Data types in IEC 61131−3
Data types determine the format, bitsize, possible value range and initial value of variables.

IEC 61131−3 distinguishes the following data types:

• Elementary data types


• Generic data types
• User defined data types

Elementary data types

Data type Description Size Range Default initial


value

BOOL Boolean 1 0...1 0

SINT Short integer 8 −128...127 0

INT Integer 16 −32768...32767 0

DINT Double integer 32 −2.147.483.648 up to 0


2.147.483.647

USINT Unsigned short 8 0 up to 255 0


integer

UINT Unsigned integer 16 0 up to 65535 0

UDINT Unsigned double 32 0 up to 4.294.967.295 0


integer

REAL Real numbers 32 −3.402823466 E+38 0.0


up to
−1.175494351 E−38
and
+1.175494351 E−38
up to
+3.402823466 E+38

LREAL Long real numbers 64 −1.7976931348623158 0.0


E+308
up to
−2.2250738585072014
E−308
and
+2.2250738585072014
E−308

8
Generic data types

up to
+1.7976931348623158
E+308

TIME Duration 32 0... 4.294.967.295 ms t#0s

BYTE Bit string of length 8 8 0...255 0


(16#00...16#FF)

WORD Bit string of length 16 0...65.535 0


16 (16#00...16#FFFF)

DWORD Bit string of length 32 0...4.294.967.295 0


32 (16#00....16#FFFFFFFF)

The use of elementary data types depends on your hardware. Please refer to your hardware
documentation for the supported data types.

The data type STRING is also an elementary data type but it does not belong to the above
mentioned group. Its format in the memory depends on your PLC type.
With your runtime system the data type STRING has the following structure:
Byte 0−1 offset to maximum length (0 corresponds to 80)
Byte 2−3 current length
Byte 4−83 characters
Byte 84 zero terminator

Generic data types


Generic data types are data types which include hierarchical groups of elementary data types.
ANY_INT includes the elementary data types DINT, INT, SINT, UDINT, UINT and USINT. If a
function can be connected with ANY_INT it means that variables of the data types DINT, INT,
SINT, UDINT, UINT and USINT can be handled.

Generic data types are organized as follows:

ANY
ANY_NUM
ANY_REAL
REAL, LREAL
ANY_INT
DINT, INT, SINT
UDINT, UINT, USINT
ANY_BIT
DWORD, WORD, BYTE, BOOL
STRING
TIME

9
User defined data types

The use of data types depends on your hardware. Please refer to your hardware documentation for
restrictions concerning generic data types.

User defined data types


User defined data types have to be declared in a data type worksheet by the application
programmer using a TYPE ... END_TYPE declaration block. Derived data types can be structures
or arrays.

Array data types include several elements of one data type. An array can be used to declare
several elements of the same type with only one line in the type declaration.

Structured data types include several elements of different data types.

User defined strings are STRING data types with a variable number of characters. When declaring
a user defined string, the length is set in brackets behind the data type as it is shown in the
example.

The use and (memory) format of user defined data types depend on your PLC type. Please refer to
your hardware documentation concerning the use of these data types.

Array data type

Declaring arrays

Array data types include several elements of the same data type. An array can be used to declare
several elements of the same type with only one line in the type declaration. Example for the
declaration of an array data type:

TYPE

graph : ARRAY[1..23] OF INT;

END_TYPE

In the example the data type 'graph' contains 23 elements of the data type 'INT'. All elements of an
array are stored continuously one after another in the PLC memory. Each particular element within
the array can be accessed via its array index.

In order to use an array you first have to declare the array in the type declaration of the data type
worksheet as shown above. Subsequently you can declare a variable of this data type in the
variable worksheet of the POU where the the array is going to be used (please refer to the 'Variable
declaration' example below).

Multi−dimensional arrays

10
User defined data types

If multi−dimensional arrays are needed, arrays of arrays can be used.

Data type declaration:

TYPE
graph : ARRAY [1..10] OF INT;
my array : ARRAY [1..3] OF graph;
END_TYPE

Variable declaration:

VAR
var1 : my_array;
var2 : INT;
END_VAR

In the code body a single element in an array of an array is accessed using two indexes:

Code body in ST:

var2 := var1[1][3];

Initializing arrays

Arrays can be initialized by assigning a value to each array element. As already mentioned above,
each single element is accessed using its index. The initialization of an array can be done while
editing the code body.

Variable declaration:

VAR
graph : ARRAY [1..10] OF INT;
END_VAR

Code body in ST:

graph[1]:=7;
graph[2]:=1092;
.
.
.
graph[10]:=13;

It is not necessary to initialize all elements of an array. If no initial value is used, the array element
is initialized with the default initial value of the element's data type when starting the program
execution.

11
Programming example for an array

Programming example for an array

Arrays can be used for describing related data belonging to the same automation object. Let us
imagine that a process changes an input value every 3 seconds and that it would be necessary to
store each of these input values to compare them with set points. This means, all input values are
of the same data type. In this case it is useful to declare an array because its values can be easily
compared using an iteration statement (e.g. a FOR loop). The single components of the array can
be accessed using the array index.

Type declaration

TYPE
graph : ARRAY[1..23] OF INT;
set_point : ARRAY[1..23] OF INT;
END_TYPE

Variable declaration

VAR
input : graph; (* incoming values of the machine *)
values : set_point; (* values to compare with *)
i : INT :=1; (* variable for array index *)
run : BOOL :=TRUE;
ERROR : BOOL;
timer : FB_TIMER; (* declare FB instance *)
END_VAR

Code body in ST

timer (pt:=t#3s;in:=run);
IF timer.Q THEN (* provide input values to array 'graph' *)
input[i] := %IW0; (* assign input value to array *)
run := 0; (* edge detection to start timer again *)
i := i+1; (* higher array index *)
ELSE
run :=1; (* count up *)
END_IF;
IF i = 23
FOR i:=1 TO 23 BY 1 DO
IF input[i] <> values[i] THEN (* comparing array 'graph' to 'set point' *)
ERROR := TRUE;
END_IF;
END_FOR;
i := 1;
END_IF;

12
Structured data type

Structured data type

Declaring structures

Structured data types include several elements of the same or of different data types.

TYPE
machine :
STRUCT
x_pos : INT;
y_pos : INT;
depth : INT;
rpm : UINT;
END_STRUCT;
END_TYPE

In the example the structured data type 'machine' consists of the components x_pos, y_pos, depth
and rpm. All components describe the characteristics of the machine.

Programming example

Structures should be used if data describing the same object have to be declared. For example a
drilling machine drills several holes in one workpiece. For each hole to be drilled the following
information are required: Position (x/y coordinates), depth and drill revolutions per minutes. The
actual values vary for each drill−hole but the used variables are always the same. So we would
declare a structure consisting of four components for the position (x and y), depth and revolutions
per minute. For each hole different values can be assigned to the components. The function block
for the drilling process is just working on the same variable being a structure.

Arrays of structures

It is possible to use a structure within arrays as it is shown in the following example:

TYPE
machine:
STRUCT
x_pos : REAL;
y_pos : REAL;
depth : INT;
rpm : INT;
END_STRUCT;
my array : ARRAY[1..10] OF machine;
END_TYPE

13
String data type

An application example for arrays of structures could be a transfer line with several drilling
machines. Via the array index the concrete drilling machine can be accessed and via the structure
components the different values for drilling can be assigned.

Structures with arrays

It is possible to use arrays in structures as it is shown in the following example:

TYPE
graph : ARRAY[1..10] of INT;
drive :
STRUCT
rpm : INT;
inputs : IN_BOOL;
performance : graph;
END_STRUCT;
END_TYPE

Initializing structures

Structures can be initialized by assigning the values to the components while editing the code body
declaration. An example is shown in the following figure:

Variables declaration:

VAR
var1 : machine;
first : BOOL :=TRUE;
END_VAR

Code body in ST:

IF first THEN
var1.x_pos := REAL#1.3E+2
var1.rpm := 3000;
...
first := FALSE;
END_IF
...

String data type

User defined string data types are strings with a variable number of characters. Declaring a user
defined string the length is set in parentheses behind the data type.

Example for the declaration of an user defined string data type.

TYPE
STRING10 : STRING(10)

14
String data type

END_TYPE

In this example the string length is 10 characters.


Limits: The shortest possible string has the length 1, the longest possible string has the length
32766.

15
Variables in IEC 61131−3
This topic contains general information about variables as it is described in the IEC standard.

According to IEC 61131−3 variables are used for programming instead of direct addressing inputs,
outputs or flags. In IEC 61131−3 different types of variables are described: Symbolic variables,
directly represented and located variables.

Scope of variables
The scope of a variable determines in which POUs it can be used. Possible scopes are local and
global. The scope of each variable is defined by the location where it is declared (local or global
variables worksheet) and the variable keyword used for the declaration.

Local variables: If a variable can be only used within one POU it is called a local variable. In those
cases it is declared in the variables worksheet of the corresponding POU using one of the variable
declaration keywords VAR, VAR_INPUT or VAR_OUTPUT. Function block instances are treated
similar to local variables, i.e. instance names have to declared using in the local variables
worksheet using the keyword VAR.

Global variables: If a variable can be used within the whole project it is called a global variable. It
has to be declared using the keyword VAR_GLOBAL in the global declaration and as
VAR_EXTERNAL in each POU where it is used.

Symbolic variables
Symbolic variables are declared with a symbolic name and a data type. An initial value is optional.
The programming system stores a symbolic variable to a free memory area in the PLC memory
which is not known to the user. Symbolic variables can be declared as retentive variables using the
keyword 'RETAIN'.

The following example shows a variable declaration of two local symbolic variables:

VAR

var1 : BOOL;

var2 : INT;

END_VAR

The second example shows the declaration of a global variable in the global variables worksheet:

VAR_GLOBAL

SampleVar : BOOL;

END_VAR

In order to use this variable in a POU, it must additionally be declared in the local variables

16
Directly represented and located variables

worksheet in the POU where it is used:

VAR_EXTERNAL

SampleVar : BOOL;

END_VAR

When declaring input and output parameters for a function block POU, the variable declaration
keywords VAR_INPUT and VAR_OUTPUT must be used.

Directly represented and located variables


Located variables are declared using a symbolic name and a logical address. Directly represented
variables are declared without symbolic name but also with a logical address. Both, directly
represented and located variables are stored at the declared logical address and it is up to the
application programmer to check that no memory address is used twice.

A location declaration consists of the keyword AT, the percent sign "%", a location prefix, a size
prefix and the name of the logical address.

Directly represented and located variables can be declared in the global variable worksheet using
VAR_GLOBAL or in programs.

The following tables shows the location and size prefix for directly represented and located
variables:

Location Description
prefix

I Physical input

Q Physical output

M Physical address in the PLC memory

Size Description
prefix

X Single bit size (only with data type


BOOL)

None Single bit size

B Byte size (8 bits)

W Word size (16 bits)

D Double word size (32 bits)

L Long word size (64 bits)

17
Retentive variables

Variables of the data type LREAL can currently not be declared as direct represented variables.

Like symbolic variables, located variables can also be declared as retentive variables and they can
be initialized.

It is recommended to declare I/Os as global variables. In the global variable declaration they should
be declared as located variables and in the VAR_EXTERNAL declaration of the POU they should
be declared as symbolic variables. The typing effort in case of address changes is reduced in this
way.

In the following example such a declaration of located global variables in the global variables
declaration is shown:

VAR_GLOBAL

var1 AT %QX2.4 : BOOL;

var2 AT %IB7 : BYTE;

var3 AT %DD2.4 : DWORD;

END_VAR

In order to use these global located variables in a POU, they must additionally be declared in the
local variables worksheet of this POU as follows:

VAR_EXTERNAL

var1 : BOOL;

var2 : BYTE;

var3 : DWORD;

END_VAR

In the next example a declaration of local directly represented variable is shown:

VAR

AT %QB 7 : BYTE;

END_VAR

Retentive variables
The value of a so−called retentive variables remains stored, even if power is switched off. After a
warm start the last value of the variable will be used.

Retentive variables are declared using the additional keyword RETAIN as it is shown in the
following example:

18
Initializing variables

VAR RETAIN

var1 : BOOL := TRUE;

END_VAR

In this example the variable has the initial value 'TRUE' which is used when the PLC performs a
cold start. In case of a warm start the current value of the variable is used.

The keyword RETAIN can be used in combination with the keywords VAR, VAR_OUTPUT and
VAR_GLOBAL. It is not possible to declare retentive variables with the keywords VAR_INPUT and
VAR_EXTERNAL.

Please observe the information concerning the way, the programming system handles changed or
new inserted retain variables when downloading or patching projects and performing a PLC warm
start.

Initializing variables
According to IEC 61131−3 an initial value can be assigned to a variable. This means that a variable
which is going to be used for the first time in the PLC program is used with this initial value. The
initial value is optional. If no initial value is used, the variable is initialized with the default initial
value of the data type or with the retained value in case of retentive variables. Initial values can be
defined in conjunction with all declaration keywords except VAR_EXTERNAL.

Initial values have to be inserted at the end of the declaration line of the variable using ':=' as it is
shown in the following example:

VAR

var1: INT := 28;

var2: TIME := T#1s;

var3 AT %QX0.0 : BOOL := TRUE;

END_VAR

It is not possible to initialize variables which are located at physical inputs.

The initial value has to fit to the data type. It is not possible to use e.g. the data type BOOL and an
initial value '5'. In this case the system displays an error message.

Variable declaration keywords


In variable declarations variable declaration keywords have to be used.

19
Initializing variables

Keyword is used ...

VAR for declaring local variables (symbolic, directly represented and located)
and for declaring function block instances. The keyword can be used
with the keyword 'RETAIN' for declaring retentive variables.

VAR_INPUT for declaring symbolic variables which are inputs of functions, function
blocks and programs. Using the a VAR_INPUT variable a value can be
handed over to the POU which is read−only when the POU is executed.

VAR_OUTPUT for variables which are outputs to function blocks and programs. Using
the a VAR_OUTPUT variable an output value can be handed over e.g.
to other POUs. The variable can be read and written when the POU is
executed. The keyword can be used with the keyword 'RETAIN' for
declaring retentive variables.

VAR_IN_OUT typically for complex data types such as strings, arrays and structures.
The address of an input/output variable is passed by reference. The
variable can be read or written.

VAR_EXTERNAL for the local declaration of a global variable (only symbolic variables) in
the POU where it is used (see description of keyword VAR_GLOBAL).
The value of an VAR_EXTERNAL variable is supplied by the related
VAR_GLOBAL declaration. Its value can be modified by the POU.

VAR_GLOBAL for declaring global variables (directly represented, located and


symbolic) which can be used in all POUs of the project. A global
variable can be declared as retentive variable (using the keyword
'RETAIN').

Note: Global variables have to be declared as VAR_GLOBAL in the


global variable declaration of the resource and as VAR_EXTERNAL in
the variable declaration of the POU where they are used.

END_VAR to close a variable declaration block.

For declaring program instances or function block instances, you have to use additional variable
declaration keywords:
VAR_GLOBAL_PG, VAR_GLOBAL_FB, VAR_EXTERNAL_PG and VAR_EXTERNAL_FB.
These keywords are IEC extensions.

20
Literals in IEC 61131−3
Literals can be used to enter external representation of data. Literals are necessary for the
representation of numeric, character strings and time data. Every time you have to enter values,
you have to use literals.

Numeric literals
The numeric literals which can be used are shown in the following table:

Type Examples

Integer literals −12 0 123_456 +986

Real literals −12.0 0.0 0.4560


3.14159_26

Real literals with −1.34E−12 −1.34e−12


exponent 1.0E+6

Long Real literals LREAL#−12.0

Long Real literals with LREAL#2.3E−89


exponent

Base 2 literals INT#2#1111_1111

Base 8 literals INT#8#377

Base 16 literals INT#16#FF SINT#16#ff

Boolean FALSE and FALSE TRUE


TRUE

Boolean 0 and 1 0,1

Literals used in variable worksheets as well as INT and BOOL literals can be used without
keyword. Examples:
For INT#16#ff you can use 16#ff.
For BOOL#FALSE you can use FALSE.

In variable declarations you can use "var1 : DINT :=10", but in the code body you have to use "LD
DINT#10". The same applies to LREAL: When entering an LREAL literal using the variable dialog,
for example "0.0" is sufficient. However, in the code body an LREAL constant has to be entered as
"LREAL#0.0".

21
Character string literals

Character string literals


A character string literal is a sequence of zero or more characters included in two single quote
characters.

Type Examples

Empty string ''

String with a blank ''

Not empty string 'this is a text'

Duration literals
Duration data can be represented in hours, minutes, seconds, milliseconds and in combination of
these formats.

Type Examples

Short prefix T#14ms t#14ms t#12m18s3.5ms


T#25h_15m t#25h_15m

Long prefix TIME#14 ms time#14ms TIME#25h_15m


time#25h_15m

Date and time of day literals

Type Examples

Date DATE#1996−01−24
date#1996−01−24
D#1996−01−24
d#1996−01−24

Time of TIME_OF_DAY#15:36:55.36
day time_of_day#15:36:55.36
TOD#15:36:55.36
tod#15:36:55.36

Date and DATE_AND_TIME#1996−01−24−15:36:55.36


time date_and_time#1996−01−24−15:36:55.36
DT#1996−01−24−15:36:55.36
dt#1996−01−24−15:36:55.36

22
IEC 61131−3 and your programming system
Your programming system is a standard programming system for IEC designed PLCs and
traditional PLCs. It is based on the standard IEC 61131−3 and includes the full range of IEC
features.

Projects in your programming system


An IEC 61131−3 project contains all necessary elements of an automation system. It is represented
in the project tree.

Each project consists of libraries, data types, POUs and the set of configuration elements
(configurations, resources, tasks and programs) which are contained in the subtree 'Physical
Hardware'.

Libraries in your programming system


Libraries provide function blocks, functions, programs and data types. After including a library you
can use the contained elements in the project you are currently editing.

Firmware libraries are libraries containing POUs prepared by the PLC manufacturer. The file
extension for firmware libraries is *.fwl.

User libraries are projects which were created before and from which you want to reuse POUs or
data types. The file extension for user libraries is *.mwt. If a project is created with an earlier
system version younger than 1.3.6.14, the project file name has the extension *.pwt. Projects which
are created with a version before 1.3.6.14 cannot be announced as libraries.

Libraries have an own subtree in the project tree. You can either display the complete project tree
or only the subtree 'Libraries' by clicking on the tab 'Libraries' at the bottom of the project tree.

23
POUs in your programming system

POUs in your programming system


Programs, function blocks and functions can be edited in the project tree.

The following figure shows an example:

A POU consists of several worksheets:

• A description worksheet which contains text entered by the PLC application programmer
serving for documentation purposes. These worksheets are optional.
Note: It is possible to use the description worksheet as context sensitive help if you are
calling help on a user defined function or FB (e.g. when working with the Edit Wizard). For
detailed information please refer to the topic Calling help on FU/FB in the topic list below.

• One variables grid worksheet containing the declaration of the local variables and function
block instances.

• One or several code body worksheet(s) containing the actual code, edited one of the
available programming languages.

These worksheets are represented graphically by icons:

24
Data types in your programming system

In case of a POU in the programming language SFC some more worksheets can be edited: action
and transition worksheets.

If a code worksheet is opened, you can easily open the related variables grid worksheet by either
selecting the menu item 'Open Variables Worksheet' in the submenu 'View' or by clicking on the
icon 'Variables Worksheet' in the toolbar:

Data types in your programming system


Data types in your programming system are basically the same data types as described in IEC
61131−3. User defined data types can be edited in data type worksheets which are contained in the
subtree 'Data Types' in the project tree.

The following figure shows an example:

25
Variables in your programming system

To open a data type worksheet with the text editor

• Double click on the icon 'Data type worksheet'.

or

• Press <â> or <á> to mark the icon 'Data type worksheet'.


Press <↵>.

The text editor with the data type worksheet appears.

Depending on the used hardware it is possible that manufacturer defined data types are provided.
Please refer to your hardware documentation.

Variables in your programming system


Variables have to be declared in local and/or global variables worksheets (depending on their
scope).

These worksheets are realized as variables grids. Declarations are not entered as plain text (as
described in the IEC) but in the form of a table which makes declarations considerably easier to
handle. Each table row contains one variable or instance declaration, each table column contains
one variable property (declaration item). This way the table reflects the declaration syntax
according to IEC 61131. The variable declaration keywords described by the standard are
contained in the column 'Usage', variables do not have to be combined within declaration blocks
which avoids syntax errors.

Comparison: Textual variable declaration and grid declaration

In a program POU the textual declaration of a located local variable

VAR RETAIN

26
Configuration elements in your programming system

var1 AT %QX1.0 : BOOL := TRUE;

END_VAR

is represented as follows in the variables grid worksheet:

Two possibilities to declare variables

You can

• either declare variables while editing code using a variables dialog. Using this method, the
new variable is inserted in the code body worksheet and the declaration is autoinserted
into the variables grid at the same time. Therefore it is not necessary to open the variables
grid worksheet.

• or declare variables directly in the variables grid worksheet (see figure above) by inserting
a new entry in the variables grid and specifying the variable properties. Having declared a
variable in the grid, it can be inserted in the code body worksheet using the variables
dialog.

Naming conventions for IEC variables

The programming system allows to use DIN qualifiers in IEC variable names. For that purpose, the
checkbox 'Use DIN identifier' must be checked in the 'Options' dialog, tab 'Build' (called via the
menu item 'Extras > Options'). However, some rules have to be observed when using DIN
identifiers. Please read the related topic.

Configuration elements in your programming system


Configuration elements are represented graphically in the subtree 'Physical Hardware' of the
project tree.

A definition of configuration elements according to the IEC can be found here.

The following figure shows an example:

27
Identifier

A project may contain one or several configurations. In each configuration one or several resources
can be declared. Within each resource several tasks with their associated programs can run.

For a description of specific configuration elements available for your PLC type please refer to the
topics in the PLC Help referenced below or to your hardware documentation.

Identifier
Identifiers can consist of letters, digits and underscores. The identifier has to begin with a letter or
an underscore. Upper and lower case can be mixed.

Multiple underscores or blanks are not allowed.

For identifiers different lengths are allowed, as shown in the following table:

Identifier for... Number of characters

POUs, description 24 characters


worksheets, projects,
configuration
elements, bitmaps,
pagelayouts, archive
files

Variable and code 24 characters


body worksheets See note below!

Steps, actions and 24 characters


transitions

Variables, instance 30 characters


names

DIN identifiers in IEC variable names

28
Program and function block instances in the instance tree

The programming system allows to use DIN qualifiers in IEC variable names. For that purpose, the
checkbox 'Use DIN identifier' must be checked in the 'Options' dialog, tab 'Build' (called via the
menu item 'Extras > Options'). However, some rules have to be observed when using DIN
identifiers. Please read the related topic.

Program and function block instances in the instance tree


The way of instantiating programs in the subtree 'Physical Hardware', i.e. associating a program to
a task, has been extended. It is also possible to instantiate programs and function blocks and to
edit a separate global variable worksheet for each instance. This way each program or function
block instance has its own data instance which can be accessed directly.

Program and function block instances are useful if you have e.g. two identical machines controlled
by the same PLC but connected to different I/Os. You can edit the PLC program in the subtree
'Logical POUs' as normal and instantiate twice the program and/or function blocks. Each instance
works with the data of its own global variable declaration.

The instance tree shows all instances used in your project as it is shown in the following example.
The instance tree is made visible by clicking with the left mouse button on the tab 'Instances' at the
bottom of the project tree.

In this example one program instance of the program 'FBD' is used in the task 'T_100ms'. The
function block 'TLC' has been instantiated in the program 'FBD'.

Please observe the following items when instantiating:

• Program instances are created by associating a program to a task and entering an


instance name in the corresponding dialog.

• It is not possible to instantiate a function block within a function block. Function blocks can
only be instantiated in programs.

29
Variable declaration keywords for program and FB instances

• The instance name which is used in the instance tree/subtree 'Physical Hardware' has to
correspond to the instance name used in the subtree 'Logical POUs'.

Variable declaration keywords for program and FB instances


For declaring program instances or function block instances, you have to use additional variable
declaration keywords. These additional keywords can be selected in the column 'Usage' of the
variables grid worksheet, when declaring an instance.

VAR_EXTERNAL_PG for the local declaration of a global variable (only symbolic variables) in
the program POU where it is used (see description of keyword
VAR_GLOBAL_PG). The value of an VAR_EXTERNAL_PG variable is
supplied by the related VAR_GLOBAL_PG declaration. A variable of
this type cannot be initialized. Its value can be modified when executing
the program POU.

VAR_EXTERNAL_FB for the local declaration of a global variable (only symbolic variables) in
the function block POU where it is used (see description of keyword
VAR_GLOBAL_FB). The value of an VAR_EXTERNAL_FB is supplied
by the related VAR_GLOBAL_FB declaration. A variable of this type
cannot be initialized. Its value can be modified when executing the
function block POU.

VAR_GLOBAL_PG for declaring global variables (directly represented, located and


symbolic) which can be used in all program POUs of the project. They
can be declared as retentive variable (using the keyword 'RETAIN').

Note: Global variables have to be declared as VAR_GLOBAL_PG in


the global variable declaration of the resource and as
VAR_EXTERNAL_PG in the variable declaration of the POU where
they are used.

VAR_GLOBAL_FB for declaring global variables (directly represented, located and


symbolic) which can be used in all function block POUs of the project.
They can be declared as retentive variable (using the keyword
'RETAIN').

Note: Global variables have to be declared as VAR_GLOBAL_FB in


the global variable declaration of the resource and as
VAR_EXTERNAL_FB in the variable declaration of the POU where
they are used.

Please refer also to the topic IEC variable declaration keywords.

30

You might also like