You are on page 1of 20

System 800xA Training

Chapter 11 Structured Text

TABLE OF CONTENTS

Chapter 11 Structured Text..................................................................................................................................................... 1


11.1 General Information ................................................................................................................................................... 2
11.1.1 Objectives............................................................................................................................................................ 2
11.1.2 Legend................................................................................................................................................................. 2
11.1.3 Reference Documentation .................................................................................................................................... 2
11.2 Structured Text........................................................................................................................................................... 3
11.2.1 Advantages of Using Structured Text ................................................................................................................... 3
11.2.2 Structured Text Editor.......................................................................................................................................... 4
11.3 Basic Language Elements ........................................................................................................................................... 5
11.3.1 Assignment Statements ........................................................................................................................................ 5
11.3.2 Comment Statements ........................................................................................................................................... 5
11.3.3 Operators............................................................................................................................................................. 6
11.3.4 Precedence........................................................................................................................................................... 7
11.3.5 Conditional Structures.......................................................................................................................................... 8
11.3.6 Iteration Structures............................................................................................................................................... 9
11.3.7 RETURN Statement........................................................................................................................................... 10
11.4 Functions and Function Blocks ................................................................................................................................. 10
11.4.1 Functions........................................................................................................................................................... 10
11.4.2 Function Blocks ................................................................................................................................................. 10
11.4.3 How to Declare Function Block Instances........................................................................................................... 11
11.4.4 How to Call a Function Block Instance............................................................................................................... 11
11.5 Common Mistakes and their Error Messages............................................................................................................. 14
11.6 How to Declare Variables from Error Messages ........................................................................................................ 17
11.7 How to Change from ST to Function Block in Online mode ...................................................................................... 18

Chapter 11 - 1
T314-11 Structured Text - RevC

11.1 General Information

11.1.1 Objectives
On completion of this chapter you will be able to:
• Describe the Structured Text Language rules
• Write simple application code in ST
• Use Functions and Function Blocks in ST

11.1.2 Legend
> Indicates when you go from one menu to a sub-menu
Italic Indicates object and file names
“ “ Indicates dialog box buttons, tabs, menus etc.
Bold Indicates important topics
Indicates start/explanation of student activity

11.1.3 Reference Documentation


3BSE043732 Industrial IT 800xA – Control and I/O
Application Programming – Introduction and Design
3BSE035980 Industrial IT 800xA – Control and I/O
Basic Control Software – Introduction and Configuration
3BSE035981 Industrial IT 800xA – Control and I/O
Extended Control Software – Binary and Analog Handling

Chapter 11 - 2
System 800xA Training

11.2 Structured Text


Structured Text (ST) is a high-level programming language. It is compact, highly
structured and contains a comprehensive range of constructs for assignments,
function/function block calls, expressions, conditional statements, iterations and more.
The code is simple to write and easy to read, because of its logical and structured
layout. The compactness of the language gives an excellent overview of the code and
less scrolling in the editor.

11.2.1 Advantages of Using Structured Text


1. Free layout of code and comments
2. When in online mode the layout can be changed to Function Block or Ladder for
viewing by different personnel.
3. Text may be generated in any external text editor and pasted into the ST editor.
For example you might use macros in MS Word to generate code.
4. Structured text reads like English and is the most efficient way of writing code.
5. You will have to learn Structured Text in any case because it is the only choice in
the SFC editor!

A small example of code written in Structured Text is shown below:

Chapter 11 - 3
T314-11 Structured Text - RevC

11.2.2 Structured Text Editor


The appearance of the structured text editor for a program is shown below:

Declarations pane

Code pane

Messages pane

The code pane is used for writing code in Structured Text. It is a simple text editor.
Tabs and spaces are used to structure the code for easy reading.
The code pane may be divided into several ‘tabs’. Each tab is also referred to as Code
Blocks. When the code is compiled the execution order of the code is firstly in tab
order (left to right) and then from top to bottom inside the tabs.
Instances of variables and function blocks are declared in the declaration pane.

Chapter 11 - 4
System 800xA Training

11.3 Basic Language Elements


This section gives a very brief review of the structured text language.

11.3.1 Assignment Statements


An assignment statement assigns a value to a variable.
The statement has two parts:
Result := In1 AND In2 OR In3;
The left hand side, before the assignment operator (:=) is called the assigned variable.
The right hand side is an expression which is evaluated to give a value to the assigned
variable.
In the above example the expression results in a Boolean – true/false value. (It is a
Boolean expression). The AND and the OR are referred to as operators (In this case
Boolean operators).
NOTE! The := symbol is used for the assignment of operators
and a statement must be ended with a semi-colon.

Another example of an assignment statement:


AverageFlow := (Flow1 + Flow2)/2;
The variable AverageFlow is assigned the value given by the result of the calculation
on the right. Flow1 is firstly added to Flow2 and then the total is divided by 2.
The expression on the right is a Real expression because it results in a Real value
(floating point value). The symbols + and / are called arithmetic operators because
they perform arithmetic operations on the variable values which follow them.

11.3.2 Comment Statements


Operator Description
(*…*) Comment according to IEC 1131-3.
(#…#) Comment that can be nested (ABB extension).

It is normally not possible to have comments within comments (nested comments):


(* This is not (* Inner Comment *) allowed *)
However there is an alternative comment symbol that allows this:
(# This is (* Inner Comment *) allowed #)
This is useful for commenting out large blocks of code containing comments.

Chapter 11 - 5
T314-11 Structured Text - RevC

11.3.3 Operators
Below is a list of the most commonly used operators:
Boolean Operators
Operator Description
NOT Negates the Boolean value (1/0, on/off or True/False).
AND Boolean AND.
& Boolean AND. See AND.
XOR Boolean XOR.
OR Boolean OR.

Arithmetic Operators
Operator Description
** Exponential, i.e. raising to the power.
* Multiplication.
/ Division.
+ Addition.
- Subtraction.
MOD Modulus.

Relational Operators
Operator Description
< Less than.
> Greater than.
<= Less than or equal to.
>= Greater than or equal to.
= Equal to.
<> Not equal to.

Chapter 11 - 6
System 800xA Training

11.3.4 Precedence
The priority of operators decides the order of evaluation of an expression. Below is a
summary of available operators, in descending priority:

Operator Description Priority


(…) Parenthesized expression. Highest
Function (…) Parameter list of a function, function evaluation.
Not, - Negation, Boolean complement, i.e. value with "opposite"
value (0 becomes 1, 1 becomes 0) and arithmetical
negation (-).
** Exponentiation, i.e. raising to a power.
*, / ,mod Multiplication, division and modulus.
+, - Addition and subtraction.
<, >, <=, >= Comparison operators
=, <> Equality and inequality.
and, & Boolean AND.
xor Boolean exclusive OR
or Boolean OR Lowest

If you are unsure of the evaluation order then use parenthesis to force evaluation of
sub-expressions.

Chapter 11 - 7
T314-11 Structured Text - RevC

11.3.5 Conditional Structures


There are tow main groups of conditional structures:
IF…Then
Operator Description
IF Boolean Expression THEN If (and only If) the Boolean Expression
Statement(s); evaluates to True, then the Statement(s)
between the IF and END_IF is/are executed.
END_IF;
IF Boolean Expression THEN If the Boolean Expression evaluates to True,
Statement(s); then the Statement(s) before the ELSE is/are
executed. Else the statements after the ELSE
ELSE and before the END_IF are executed.
Statement(s);
END_IF;
IF Boolean Expression 1 THEN If the Boolean Expression 1 evaluates to True,
Statement(s); then the Statement(s) before the first ELSIF
is/are executed. If this condition is false then
ELSIF Boolean Expression 2 THEN the subsequent Boolean Condition is tested
Statement(s); and so on for each ELSIF. If no expression is
true then the ELSE statement(s) execute.
ELSIF Boolean Expression n THEN
The ELSE clause is optional. You may have
Statement(s);
as many ELSIF clauses as you wish.
ELSE
Statement(s);
END_IF;

NOTE! A conditional statement is always concluded with


END_IF;

Chapter 11 - 8
System 800xA Training

IFs may be nested to many levels. A better structure to be used instead of nesting is
the CASE structure. This is very useful when many conditions need evaluation; there
are several forms:
CASE…OF
Operator Description
CASE Integer Expression OF A statement is executed depending on the
Integer Literal1 : Statement(s); value of an integer variable or an integer
expression.
END_CASE;
The <integer literal> is one or several integer
values or one or several ranges of values.
CASE Integer Expression OF In this example, three values are tested and
Integer Literal1 : Statement(s); the appropriate statements) executed. One
variation is to permit a range of values to be
Integer Literal2 : Statement(s); tested rather than an exact single value.
Integer Literal3 : Statement(s); If none of the test literals match the result of
ELSE the expression then the statement(s) in the
ELSE clause will be executed. If no ELSE
Statement(s);
exists, none of the statements will be
END_CASE; executed.

11.3.6 Iteration Structures


Several iteration constructs exist: They should be used with extreme caution. During
the iteration of the loop, the CPU remains in the loop. If a loop has a large number of
iterations then the normal scan time may be easily exceeded.
NOTE! Only use these constructs when you know exactly how
many iterations are going to be done.

Operator Description
FOR i := 0 to 15 DO The FOR statement is used to allow a statement (or
Statement(s); statements) to be executed repeatedly for a given
number of times. The counter used in the repetition
END_FOR; process can be used in the statements.
In the example, the statements between the FOR
and END_FOR will be executed 16 times.
WHILE Level > 80.0 DO The WHILE statement is used in order to allow a
Statement(s); statement (or statements) to be executed repeatedly
while a certain condition is True.
END_WHILE;
This separates it from the FOR statement. It has
some similarities with the REPEAT statement.
REPEAT The REPEAT statement is used in order to allow a
Statement(s); statement (or statements) to be executed repeatedly
until a certain condition is True.
UNTIL Boolean Expression
Note that the test to exit the loop is placed at the end,
END_REPEAT; so a minimum of one execution of the statement(s)
will occur even if the expression is true at the time
the loop is entered.
EXIT Use the EXIT statement whenever you want to
terminate a loop immediately and continue execution
from the first line after the iteration statement.

Chapter 11 - 9
T314-11 Structured Text - RevC

11.3.7 RETURN Statement


The RETURN statement causes immediate exit from the current code block (A tab in
a program or control module or function block). No further code in that block is
executed.

11.4 Functions and Function Blocks

11.4.1 Functions
Functions are called inside expressions with the following syntax:
Var := FunctionName(Parameter(s))
In the example below the square root of a flow signal is calculated by using the Sqrt()
function.

11.4.2 Function Blocks


Function block calls do not appear in assignment statements. The call is itself a valid
statement. Note that in ST, function block input parameters are listed with the :=
symbol and output parameters are listed with the => symbol.
The example below shows the code for calling a delay off timer:

The above timer function block is the same as the following in FBD form:

Function blocks are declared in a similar way to variables, by giving them a name (an
instance name). This name is then used to call them in the code. In the ST editor you
must declare function blocks explicitly in the “Function Block” declaration tab in the
editor by giving a name and a type:

Chapter 11 - 10
System 800xA Training

11.4.3 How to Declare Function Block Instances


As an absolute minimum a function block instance must be given a name and a
function block type:

1. Mark the “Function Blocks” tab in the declarations pane of the POU.
2. Type in an instance name for the function block in the “Name” column.
3. Type in the required function block type in the “Function Block Type” column.
Place the cursor in the field and press ‘Ctrl + J’ to see a list of all available
function block types.

NOTE! Function Block Types are defined in the libraries.


If the library that contains the function block type that you want has not been
connected to the application then it will not appear in the list.

11.4.4 How to Call a Function Block Instance


In the example below a delay off timer has been declared in the “Function Block”
declaration tab, called MixTimer of type TOf:

Chapter 11 - 11
T314-11 Structured Text - RevC

Four variables which will be used to connect to the function block have also been
declared in the “Variables” declaration tab:

1. Call the function block instance by typing its instance name in the code pane
followed by an opening parenthesis ‘(‘

The system will then offer a dialogue for you to make connections.

2. Fill in the parameters that are to be connected to the function block instance in the
parameter column by any of the following methods:

Type the name of the variable to be connected directly into the “Parameter” field.
(After a few characters the system will try to help you finish by supplying first
matching variable name).

Chapter 11 - 12
System 800xA Training

3. Use the “Insert from List” method by clicking on the Insert Variable … icon in the
menu bar or use the “Insert Path from Tree” Icon to browse the application for the
variable:

4. Click on the “Exit and Close” icon in the dialogue.

5. The result (for this example) looks like:

Chapter 11 - 13
T314-11 Structured Text - RevC

11.5 Common Mistakes and their Error Messages


Identifier, constant or opening parenthesis expected

Double clicking on the error message line places the cursor at the end of the statement
with the problem: Usually a missing semi-colon (;)at the end of a statement.

Variable name is not unique


Two variables have been declared with the same name. (Note that this results in two
error one for each non-unique name).

Note also that some other items are classed as ‘variables’ by the system:
- Instance names for function blocks
- Instance names for control modules
- Sequence Step names
- Code Block names
- Sequence Transition names
- Parameters (in function block types and control module types)
This means that within any POU all of the above must have unique names.

Chapter 11 - 14
System 800xA Training

Identifier too long or invalid


This error message is given under two circumstances:
1. Variable naming rules have been disobeyed - the name begins with a number.

2. A key word has been used - both words “On” and “Off” are reserved.

Undefined function block


Usually caused by a typing error in the name of the function block instance:

The programmer has declared the function block with the name MixTimer but has
referenced MuxTimer in the code.

Chapter 11 - 15
T314-11 Structured Text - RevC

Type mismatch in assignment


This occurs when the programmer has mixed data types in an assignment statement

In this case the programmer has attempted to add a “real” and a “dint” to get a “dint”
result. (You can’t multiply apples with pears!)

Incompatible types in expression


This occurs when an operation has been attempted on different types that are not
compatible for that operation:

In the above a real has been added to a bool.

Chapter 11 - 16
System 800xA Training

11.6 How to Declare Variables from Error Messages


There is a quick shortcut that can save a lot of time when programming. That is to let
the error checker do the work of declaring variables. This method is not for the purist!
1. Program as normal but without declaring the variables and then do a check on the
code. You will get “Variable not declared” errors:

2. Now do a check and you will get the following errors:

3. To get automatic declaration, mark the first error, click right and select “Declare
Variable”:

The system will copy the name into the declaration pane and also as an added
bonus will fill in the type if it can deduce it from the sense of the statement.

Chapter 11 - 17
T314-11 Structured Text - RevC

11.7 How to Change from ST to Function Block in Online mode


When in online mode the layout can be changed to Function Block or Ladder for
viewing by different personnel.
1. Start by online mode

2. Select “Tools > Setup”

3. Select “Structured Text” tab

Chapter 11 - 18
System 800xA Training

4. Select the “Function Diagram” in “Structured Text” tab and press then “OK”.

5. To get the Structured Text code as Function Block Diagram

Chapter 11 - 19
T314-11 Structured Text - RevC

Chapter 11 - 20

You might also like