Tm246tre 00-Eng PDF
Tm246tre 00-Eng PDF
Tm246tre 00-Eng PDF
rin
Structured Text (ST)
ep TM246
rr
t fo
no
t
rin
ep
rr
t fo
Requirements
Software: None
Hardware: None
1. INTRODUCTION 4
1.1 Objectives 5
t
2. STRUCTURED TEXT FEATURES 6
rin
2.1 General information 6
2.2 Properties 7
2.3 Possibilities 7
ep
3.2 Assignment 8
3.3 Comments 9
3.4 Operator priorities 10
4. COMMAND GROUPS 12
4.1 Boolean operations 12
4.2 Arithmetic operations 14
rr
4.3 Comparison operators 18
4.4 Decisions 18
4.5 Case statements 27
4.6 Loops 30
t fo
5. SUMMARY 40
6. EXERCISES 41
7. APPENDIX 42
7.1 Keywords 42
no
7.2 Functions 43
7.3 Solutions 45
1. INTRODUCTION
Structured Text is a high level language. For those who are comfortable
programming in Basic, PASCAL or Ansi C, learning Structured Text is
simple. Structured Text (ST) has standard constructs that are easy to
understand, and is a fast and efficient way to program in the automation
t
industry.
rin
ep
rr
Fig. 1 Book printing: then and now
t fo
The following chapters will introduce you to the use of commands, key
words, and syntax in Structured Text. Simple examples will give you a
chance to use these functions and more easily understand them.
no
1.1 Objectives
Participants will get to know the programming language Structured Text
(ST) for programming techincal applications.
t
You will learn the individual command groups and how they work together.
rin
You will get an overview of the reserved keywords in ST.
ep
rr
Fig. 2 Overview
t fo
no
t
programming. ST uses many traditional qualities of high level languages,
including variables, operators, functions, and elements for controlling the
rin
program flow.
ep
No other programming language can replace ST. Every programming
language has its advantages and disadvantages. The main advantage of ST
is that complex mathematical calculations can be programmed easily.
rr
t fo
no
2.2 Properties
Structured Text is characterized by the following features:
t
• Structured programming
• Easy to use standard constructs
rin
• Fast and efficient programming
• Self explanatory and flexible use
• Similar to PASCAL
• Easy to use for people with experience in PC programming
languages
• Conforms to the IEC 61131-3 standard
2.3 Possibilities
•
•
ep
Automation Studio supports the following functions:
• Diagnostic tools
no
3.1 Expressions
An expression is a construct that returns a value after it has been
evaluated. Expressions are composed of operators and operands. An
t
operand can be a constant, a variable, a function call or another
expression.
rin
Example: Expressions
3.2 Assignment
Fig. 3 Expressions
ep
The assignment of a value to a variable through a result of an expression or
rr
a value. The assignment consists of a variable on the left side, which is
designated to the result of a calculation on the right side by the assignment
operator ":=". All assignments must be closed with a semicolon ";".
Example: Assignment
t fo
Fig. 4 Assignment
When the code line has been processed, the value of variable "Var1" is
twice as big as the value of variable "Var2".
no
3.3 Comments
Comments are sometimes left out, but are nevertheless an important
component of the source code. They describe the code and make it more
easy to read. Comments make it possible for you or others to read a
t
program easily, even long after it was written. They are not compiled and
have no influence over the execution of the program. Comments must be
rin
placed between a pair of parenthesis and asterix "(*comment*)".
Example: Comment
ep
Fig. 6 Multi-line comment
rr
t fo
no
t
Expressions are executed starting with the operator of highest priority,
followed by the next highest, and so on until the expression has been
rin
completely executed. Operators with the same priority are executed from
left to right as they appear in the expression.
ep
Examples LN(A), MAX(X), etc.
Exponent **
Negation NOT
Multiplication *
Division /
Modulo division (whole MOD
rr
number remainder of
division)
Addition +
Subtraction -
Comparisons <, >, <=, >=
t fo
Equal to =
Not equal to <>
Boolean AND AND
t
rin
Fig. 7 Order of execution
ep
Example: Operator priorities with parentheses
4. COMMAND GROUPS
ST has the following command groups:
• Boolean operations
• Arithmetic operations
t
• Comparison operations
rin
• Decisions
• Case statements
Boolean operations:
Symbol
NOT
AND
OR
XOR
Binary negation
Logical AND
Logical OR
Exclusive OR
ep
Logical operation Examples
a := NOT b;
a := b AND c;
a := b OR c;
a := b XOR c;
rr
Truth table:
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
t
rin
Fig. 9 AND operation
t
4.2.1 Basic arithmetic operations
rin
ST provides basic arithmetic operations for your operation:
ep
* Multiplication a := b * c;
/ Division a := b / c;
MOD Modulo (display division a := b mod c;
remainder)
The data type is a very important factor. Note the following table:
You can see the that the result is dependent of the syntax as well as the
data types used.
Note:
Left data type := right data type;
no
t
are all converted to the same data type before the expression is resolved.
rin
Data BOOL SINT INT DINT USINT UINT UDINT REAL
type
BOOL BOOL x x x x x x x
USINT
UINT
UDINT
REAL
x
x
USINT
UINT
UDINT
REAL
ep INT
UINT
UDINT
REAL
DINT
DINT
UDINT
REAL
UINT
UDINT
REAL
UINT
UDINT
REAL
UDINT
UDINT
REAL
REAL
REAL
REAL
rr
t fo
t
rin
Example: Overflow?!
ep
Example: Overflow taken into consideration.
The variable DINT_TotalWeight must be the data type DINT. At least one
rr
variable on the right side of the expression must be converted to the data
type DINT. The conversions functions are found in the OPERATOR library.
t fo
no
Task: Aquarium
t
The temperature of an aquarium is measured at two different places. Create a
program that calculates the average temperature and displays it at an analog
rin
output.
Don't forget that analog inputs and outputs must be data type INT.
ep
rr
t fo
Fig. 15 Aquarium
no
t
Symbol Logical comparison Example
expression
rin
= Equal to IF a = b THEN
<> Not equal to IF a <> b THEN
> Greater than IF a > b THEN
>= Greater than or equal to IF a >= b THEN
< Less than IF a < b THEN
<= Less than or equal to IF a <= b THEN
Note:
ep
The comparison operations and logical operations are mainly used as
logical conditions for IF, ELSEIF, WHILE and UNTIL statements.
( IF (a > b) AND (d >= e) THEN )
rr
4.4 Decisions
The IF statement is used to create decisions in the program. You are
already familiar with the comparison operators, and they can be used here.
t fo
• Simple IF statement
• IF – ELSE statement
• IF – ELSIF statement
• Nested IF
4.4.1 IF
This is the most simple IF statment.
t
rin
ep
Fig. 16 Simple IF statement
Fig. 17 Simple IF statement in a program
The IF statement is tested for the result TRUE. If the result is FALSE, the
rr
program advances to the line after the END_IF statement. The function of
the IF statement can be a single comparison, but it can also be multiple
comparisons connected by AND, OR, etc..
t fo
4.4.2 ELSE
The ELSE statement is an extension of the simple IF statement. Only one
ELSE statment can be used per IF statement.
t
rin
Fig. 19 IF – ELSE statment
ep Fig. 20 IF – ELSE in a program
rr
If the IF meets condition A, then the A instruction(s) are executed. If the IF
does not meet condition A, then the B instruction(s) are executed.
t fo
no
4.4.3 ELSIF
One or more ELSE_IF statements allow you to test a number of conditions
without creating a confusing software structure with many simple IF
statements.
t
rin
ep
rr
t fo
At runtime the decisions are processed from top to bottom. If the result of
a decision is TRUE, the corresponding statements are executed. Then the
program continues from after the END_IF. Only those decisions that
t
rin
A temperature sensor measures the outside temperature. The
temperature is read via an analog input (1°=10), and should be
displayed inside the house in text form.
• If the temperature is under 18°C, the display should read "Cold".
• If the temperature is between 18°C and 25°C, the display should
read "Opt".
•
ep
If the temperature is over 25°C, the display should read "Hot".
Fig. 23 Thermometer
no
Note:
t
rin
ep
rr
Fig. 25 Nested IF statement in a program
After three nesting levels, it is better to find another way to structure the
program.
no
t
75% and the temperature is between 18 and 25°C. Otherwise "Temp.
OK" should be displayed.
rin
Solve this task using a nested IF statement.
ep
rr
t fo
no
Two simple IF statements produce nearly the same effect as one nested IF
statement. A marker variable, or flag, can be requested in multiple
statements. The first IF statment describes the flag, which is then utilized
by other IF statements.
t
rin
ep
rr
Fig. 27 Two IF statements
t fo
a CASE statement.
t
rin
ep
rr
t fo
no
t
similar to an IF statement that is then executed.
rin
After the statements have been executed, the program continues from after
the END_CASE statement.
END_CASE END_CASE
ep
3,4,6..10: Display := OPERATION For 3,4,6,7,8,9,10
End of CASE
rr
t fo
no
Only one step of the CASE statement is processed per program cycle.
t
rin
ep
Fig. 29 CASE statement in a program
rr
Note:
• The ranges and values of the step variable(s) may not overlap each
other.
The fill level of a brewing tank is monitored for low, ok, and high levels.
t
Use an output for each of the low, ok, and high levels.
rin
The level of liquid in the tank is read as an analog value and is internally
converted to 0-100%. If the contents sink below 1%, a warning tone
should be triggered.
ep
rr
t fo
no
4.6 Loops
In many applications, it is necessary for sections of code to be executed
multiple times during a cycle. This type of processing is also referred to as
a loop. The code in the loop is executed until a defined termination
t
condition is met.
rin
Loops help make programs shorter and easier to follow. Program
expandability is also an issue here. Loops can be nested.
•
Limited
• FOR
Unlimited
ep
defined number of repetitions or to run to a certain limit.
•
rr
• WHILE
• REPEAT
t fo
no
4.6.1 FOR
The FOR statement is used to run a program section for a limited number
of repetitions. For all other applications, WHILE or REPEAT loops are used.
t
Key words Syntax Description
FOR TO BY DO FOR i:=StartVal TO StopVal {BY Step} DO The section in {}
rin
is optional.
Res := value + 1; Loop body
statement(s)
END_FOR END_FOR End of FOR
The FOR statement raises or lowers the loop counter until it reaches the
end value. The step size is always 1, unless otherwise specified with "BY".
Task: Crane
t
rin
Fig. 32 Crane
ep
Create a solution for this task using a FOR loop.
rr
t fo
no
4.6.2 WHILE
The WHILE loop can be used in the same way as the FOR loop, except that
the condition can be any boolean expression. If the condition is met, then
the loop is executed. The WHILE loop is used to repeat statements as long
t
as a particular condition remains TRUE.
rin
Keywords Syntax Description
WHILE DO WHILE i<4 DO Boolean condition
Res := value + 1; Statement
i := i + 1; Statement
END_ WHILE END_ WHILE End of WHILE
ep
rr
t fo
Note:
If the condition never assumes the value FALSE, the statements are
repeated endlessly, resulting in a runtime error.
4.6.3 REPEAT
The REPEAT loop differs from the WHILE loop in that the termination
condition is only checked once the loop has been executed. This means
that the loop runs at least once, regardless of the termination condition.
t
Keywords Syntax Description
rin
REPEAT REPEAT Start loop
Res := value + 1; Statement
i := i + 1; Statement
UNTIL UNTIL i > 4 Termination condition
END_ REPEAT END_ REPEAT End loop
ep
rr
t fo
The statements are executed repeatedly until the UNTIL condition is TRUE.
no
If the UNTIL condition is true from the beginning, the statements are only
executed once.
Note:
If the UNTIL condition never assumes the value TRUE, the statements
are repeated endlessly, resulting in a runtime error.
4.6.4 EXIT
The EXIT statement can be used with all types of loops before their
termination condition occurs.
t
rin
Fig. 36 EXIT statement
ep Fig. 37 EXIT statement in a program
rr
t fo
t
rin
Fig. 40 Calling a function block
ep
Fig. 41 Calling a function block in a program
rr
Before a function block is called, one must describe the variables that are
to be used as input parameters. The code for calling a function block
occupies one line. Then the outputs of the function block can be read.
t fo
no
t
rin
Fig. 42 Detail view of a function block call
ep
First the function block name is entered, then the transfer parameters are
assigned in parentheses, separated by commas. The code for calling a
function block is closed with a semicolon.
rr
t fo
no
Create a program that counts the bottles on a conveyor belt. Use the CTU
(up counter) function block found in the STANDARD library.
t
rin
Fig. 43 Bottle counter
Note:
ep
The Automation Studio online help comes in handy when working with
rr
function blocks.
t fo
no
t
This procedure is referred to as the referencing or initialization of a
dynamic variable.
rin
As soon as the dynamic variable is initialized, it can be used to access the
memory content to which it now "points".
ep
Fig. 44 Referencing a dynamic variable
As you can see, the operator ADR() is used. It returns the memory address
of the variable in parentheses. The data type of this address is UDINT. The
statement is then closed with a semicolon.
rr
t fo
no
5. SUMMARY
Structured Text is a high level language that offers a wide range of
functionality. It contains all the tools necessary for an application.
t
rin
ep
rr
Fig. 45 Book printing: then and now
After completing this training module, you are ready to program your own
t fo
6. EXERCISES
t
Two conveyor belts (doConvTop, doConvBottom) transport boxes to a
lift.
rin
If the photocell is (diConvTop, diConvBottom) is activated, the
corresponding conveyor belt is stopped and the lift is called.
If the lift has not been called, it returns to the appropriate position
(doLiftTop, doLiftBottom).
When the lift is in the correct position (diLiftTop, diLiftBottom), the lift
ep
conveyer belt (doConvLift) is turned on until the box is completely on
the lift (diBoxLift).
As soon as the box has left the lift, the lift is free for the next request.
rr
t fo
7. APPENDIX
7.1 Keywords
Key words are commands that can be used in ST. In the Automation Studio
Editor, these are displayed in blue. You are already familiar with many of
t
them; here is a list of more. Key words may not be used as variable names.
rin
Keyword Description
ACCESS Assignment of an address to a dynamic variable.
BIT_CLR A := BIT_CLR(IN, POS) A is the value of the variable IN that
results when the bit at position POS is deleted. IN remains
unchanged.
BIT_SET A := BIT_CLR(IN, POS) A is the value of the variable IN that
ep
results when the bit at position POS is set. The IN operand
remains unchanged.
BIT_TST Determination of a bit within a value. A is the state of the bit of
the IN value that is at position POS.
BY See FOR statement.
CASE See CASE statement.
DO See WHILE statement.
rr
EDGE Determines the negative and positive edges of a bit.
EDGENEG Determines the negative edge of a bit.
EDGEPOS Determines the positive edge of a bit.
ELSE See IF statement.
ELSIF See IF statement.
t fo
IF See IF statement.
REPEAT See REPEAT statement.
RETURN Can be used to end a function.
THEN See IF statement.
TO See FOR statement.
UNTIL See REPEAT statement.
WHILE See WHILE statement.
7.2 Functions
There are some functions that can be used in ST that do not require you to
insert a library into the project. In the Automation Studio Editor, these
function calls are displayed in blue. You are already familiar with some of
them. More are listed here.
t
Function Example
rin
ABS Returns the absolute value of a number. ABS(-2) returns 2.
ACOS Returns the cosine of a number. (inverted cosine function).
ADR Returns a variable's address.
AND Logical AND for bit operations.
ASIN Returns the arc sine of a number (inverse function of sine).
ASR
ATAN
COS
EXP
EXPT
ep
Arithmetic shifting of an operand to the right: A := ASR (IN, N) IN is
shifted N bits to the right, the left is filled with the sign bit.
Returns the arc tangent of a number (inverse function of tangent).
Returns the cosine of a number.
Exponential function: A := EXP (IN).
One operand raised to the power of another operand:
A := EXPT (IN1, IN2).
rr
LIMIT Limitation: A = LIMIT (MIN, IN, MAX) MIN is the lower limit, MAX is
the upper limit for the result. If IN is less than MIN, then the MIN
result is returned. If IN is greater than MAX, then the MAX result is
returned. Otherwise, the IN result is returned.
LN Returns the natural logarithm of a number.
LOG Returns the base-10 logarithm of a number.
t fo
ROR Rotates an operand's bits to the right: A := ROR (IN, N); The bits in
IN are shifted N times to the right, the far right bit being pushed in
again from the left.
SEL Binary selection: A := SEL (CHOICE, IN1, IN2) CHOICE must be type
BOOL. If CHOICE is FALSE, then IN1 is returned. Otherwise, IN2 is
t
returned.
SHL Shifts an operand's bits to the left: A := SHL (IN, N); IN is shifted N
rin
bits to the left, the right side is filled with zeroes.
SHR Shifts an operand's bits to the right: A := SHR (IN, N); IN is shifted N
bits to the right, the left side is filled with zeroes.
SIN Returns the sine of a number.
sizeof This function returns the number of bytes required by the specified
variable.
SQRT Returns the square root of a number.
TAN
TRUNC
XOR
ep
Returns the tangent of a number.
Returns the integer part of a number.
Logical EXCLUSIVE OR operation by bit.
rr
t fo
no
7.3 Solutions
Task: Light control
t
rin
Task: Aquarium
ep
rr
Task: Weather station - Part II
t fo
no
t
rin
ep
rr
Task: Crane
t fo
t
rin
ep
rr
t fo
no
Notes
t
rin
ep
rr
t fo
no
t
rin
ep
rr
Overview of training modules
TM246
Weblink
Internationality
no