Professional Documents
Culture Documents
TABLE OF CONTENTS
Chapter 11 - 1
T314-11 Structured Text - RevC
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
Chapter 11 - 2
System 800xA Training
Chapter 11 - 3
T314-11 Structured Text - RevC
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
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:
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
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.
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.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.
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
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.
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:
Chapter 11 - 13
T314-11 Structured Text - RevC
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.
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
2. A key word has been used - both words “On” and “Off” are reserved.
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
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!)
Chapter 11 - 16
System 800xA Training
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
Chapter 11 - 18
System 800xA Training
4. Select the “Function Diagram” in “Structured Text” tab and press then “OK”.
Chapter 11 - 19
T314-11 Structured Text - RevC
Chapter 11 - 20