Course T530
Chapter 9 Structured Text (ST) Language
9.1 General Information 9.1.1 Description
This chapter describes the use of the programming language ST.
9.1.2 Objectives
On completion of this chapter you will be able to: ? Use the ST Language.
9.1.3 Reference Documentation
In the Online help: Contents tab, Programming Languages > ST–Structured Text.
3BSE 022 850/A
9-1
Course T530 Chapter 9 Structured Text (ST) Language
9.2 Lesson 1 - ST Language 9.2.1 Description
This lesson describes the use of the ST Language.
9.2.2 Objectives
On completion of this lesson you will be able to: ? Use the ST Language.
9.2.3 General
Structured Text (ST) is a high-level programming language. It is highly structured and contains a comprehensive range of constructs for assignments, function/function block calls, expressions, conditional statements, iterations and more. One major advantage to other control programming languages is that the code is very simple to write and easy to read, because of its logical and structured layout. Tabs and spaces are used to structure the code for easy reading. Below is an example of code written in Structured Text to demonstrate the clearness and structure of this language.
9.2.3.1 Functions simplifying while editing
Press < Ctrl I > to insert Statement in the code pane or use the Press < Ctrl J > to insert declared variables in the code pane. button.
9-2
3BSE 022 850/A
Course T530
9.2.3.2 Using Microsoft Word
It is possible to write the code in Microsoft Word and then use the copy and paste function to move the code to the code pane if you are using the Structured Text editor.
9.2.4 Comments and the ABB extension
Comments can be inserted wherever it is acceptable to insert one or more spaces in the code. You can insert comments in the code by framing them with the characters (* and *) according to IEC 1131-3. You can not nest comments using this syntax. If you need to nest comments you can frame the comment with the characters (# and #). You can nest (*...*) comments inside (#...#) comments. This is useful for commenting out large blocks of code containing comments. Operator (*…*) (#…#) Description Comment according to IEC 1131-3. Comment that can be nested (ABB extension).
9.2.5 Instructions
The following basic instructions are available in the ST language.
9.2.5.1 Assign statements
Operator := () Description Assigns a value (number, logical or string) to a variable Parentheses. Commonly used to change the priority of an expression.
A variable is assigned a certain value with an assignment statement. If we wish the variable to take the value of Start we write: Run := Start; A statement is always concluded with a semicolon, ‘;’. As in mathematics, the parentheses are used to change the priority in which execution is performed. Parentheses have the highest order of precedence.
3BSE 022 850/A
9-3
Course T530 Chapter 9 Structured Text (ST) Language
9.2.5.2 Expressions
There are different kinds of expressions. They are Boolean Expressions (and, or, not, xor), Arithmetic Expressions (+, -, *, **, mod), Relational Expressions (=, >=, >, <=, <, <>). An expression produces a result. An expression contains operators, functions and operands. Operators may be +, -, /. Functions may be sin or cos. The operand can be a value, a variable, a function or another expression.
9.2.5.2.1 Boolean expressions
Operator NOT, AND & XOR OR Description Negates the Boolean value (1/0, on/off or True/False). Boolean AND. Boolean AND. See AND. Boolean XOR. Boolean OR.
Example of a Boolean expression: Activated := Start and not Stop;
9.2.5.2.2 Arithmetic expressions
Operator ** * / + MOD Description Exponential, i.e. raising to the power. Multiplication Division. Addition. Subtraction. Modulus.
Example of an Arithmetic expression: Value:= Value +1;
9-4
3BSE 022 850/A
Course T530
9.2.5.2.3 Relational expression
Operator < > <= >= = <> Description Less than. Greater than. Less than or equal to. Greater than or equal to. Equal to. Not equal to.
Under the headline Conditional Statements, is an example given with Relational Expressions.
9.2.5.3 Statements 9.2.5.3.1 Conditional statements
Operator if Description The if... then... elsif... else... statements conditionally execute a group of statements, depending on the value of a Boolean expression. An if statement is always concluded with end_if;
An example of a conditional statement:
9.2.5.3.2 Iteration statements
Operator For Description The for statement is used to allow a statement (or statements) to be executed repeatedly for a given number of times. The counter used in the repetition process can be used in the statements. The while statement is used in order to allow a statement (or statements) to be executed repeatedly while a certain condition is True. This separates it from the for statement. It has some similarities with the repeat statement.
9-5
While
3BSE 022 850/A
Course T530 Chapter 9 Structured Text (ST) Language
Repeat
The Repeat statement is used in order to allow a statement (or statements) to be executed repeatedly until a certain condition is True. This separates it from the for statement. It has some similarities with the while statement. Use the exit statement whenever you want to terminate a loop immediately and continue execution from the first line after the iteration statement.
Exit
9.2.5.3.3 Return statements
Operator Return Description Exit and terminate the execution of the current code. The instruction is usually found in function blocks and functions. It is not advisable to use the statement unless you are an advanced user as it can cause problems when executing together with other function blocks.
9.2.5.4 Execution order
The priority of operators decides the order of evaluation of an expression. Below is a summary of available operators, in descending priority: Operator (…) Function (…) Not, Description Parenthesized expression. Parameter list of a function, function evaluation. 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. Multiplication, division and modulus. Addition and subtraction. Comparison operators Equality and inequality. Boolean AND. Boolean exclusive OR Boolean OR Lowest Priority Highest
** *, / ,mod +, <, >, <=, >= =, <> and, & xor or
9-6
3BSE 022 850/A
Course T530
9.2.5.5 Simulate
When you simulate code written in the language Structured Text, is it possible to view the code in Ladder or Function Block Diagram (This works only for code written in Structured Text). Select (Tools> Setup) in the menu of the code block where the code is written (You must be in online mode when you do this).
9.3 Exercise 9.1 – Use the ST language
Please do exercise 9.1
3BSE 022 850/A
9-7
Course T530 Chapter 9 Structured Text (ST) Language
This page is intentionally left blank
9-8
3BSE 022 850/A
Course T530
TABLE OF CONTENTS Chapter 9 Structured Text (ST) Language ................................................................ 1 9.1 General Information ......................................................................................... 1 9.1.1 Description ......................................................................................... 1 9.1.2 Objectives .......................................................................................... 1 9.1.3 Reference Documentation.................................................................. 1 9.2 Lesson 1 - ST Language................................................................................... 2 9.2.1 Description ......................................................................................... 2 9.2.2 Objectives .......................................................................................... 2 9.2.3 General ............................................................................................... 2 9.2.3.1 Functions simplifying while editing ................................. 2 9.2.3.2 Using Microsoft Word...................................................... 3 9.2.4 Comments and the ABB extension .................................................... 3 9.2.5 Instructions ........................................................................................ 3 9.2.5.1 Assign statements ............................................................. 3 9.2.5.2 Expressions....................................................................... 4 9.2.5.2.1 Boolean expressions..................................... 4 9.2.5.2.2 Arithmetic expressions................................. 4 9.2.5.2.3 Relational expression ................................... 5 9.2.5.3 Statements ........................................................................ 5 9.2.5.3.1 Conditional statements ................................. 5 9.2.5.3.2 Iteration statements ...................................... 5 9.2.5.3.3 Return statements ......................................... 6 9.2.5.4 Execution order ................................................................ 6 9.2.5.5 Simulate ............................................................................ 7 9.3 Exercise 9.1 – Use the ST language ................................................................. 7
3BSE 022 850/A
9-i
Course T530 Chapter 9 Structured Text (ST) Language
This page is intentionally left blank
9-ii
3BSE 022 850/A