Professional Documents
Culture Documents
ep
rin
no
t fo
rr
TM246
t
ep
rin
rr
t fo
Requirements
Training modules:
no
Software:
None
Hardware:
None
TM246
Table of contents
4
1.1 Objectives
1. INTRODUCTION
ep
rin
2.2 Properties
2.3 Possibilities
8
8
8
9
10
4. COMMAND GROUPS
12
12
14
18
4.4 Decisions
18
27
4.6 Loops
30
t fo
rr
36
39
5. SUMMARY
40
6. EXERCISES
41
7. APPENDIX
42
42
7.2 Functions
43
7.3 Solutions
45
no
7.1 Keywords
Introduction
1. INTRODUCTION
rr
ep
rin
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
industry.
t fo
no
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.
TM246
Introduction
1.1 Objectives
ep
rin
You will learn the individual command groups and how they work together.
no
t fo
Fig. 2 Overview
rr
ep
rin
no
t fo
rr
TM246
2.2 Properties
Structured Text is characterized by the following features:
ep
rin
2.3 Possibilities
rr
no
t fo
Example: Expressions
Fig. 3 Expressions
3.2 Assignment
ep
rin
rr
t fo
Example: Assignment
Fig. 4 Assignment
no
When the code line has been processed, the value of variable "Var1" is
twice as big as the value of variable "Var2".
TM246
3.3 Comments
Example: Comment
ep
rin
no
t fo
rr
The use of several operators in one line brings up the question of priority
(order of execution). The execution is determined by priority.
Operator
ep
rin
()
Function call
Call argument(s)
Examples
Exponent
**
Negation
NOT
Multiplication
Division
MOD
Addition
Subtraction
t fo
Comparisons
Equal to
Not equal to
<>
Boolean AND
AND
Boolean exclusive OR
XOR
Boolean OR
OR
no
10
TM246
Highest priority
rr
Parentheses
Lowest priority
ep
rin
rr
no
t fo
11
Command Groups
4. COMMAND GROUPS
ST has the following command groups:
ep
rin
Boolean operations
Arithmetic operations
Comparison operations
Decisions
Case statements
Logical operation
Examples
NOT
Binary negation
a := NOT b;
AND
Logical AND
a := b AND c;
OR
Logical OR
XOR
Exclusive OR
Input
rr
Truth table:
a := b OR c;
AND
OR
XOR
t fo
a := b XOR c;
no
12
TM246
Command Groups
ep
rin
rr
t fo
no
13
Command Groups
ep
rin
Arithmetic operation
Example
:=
Assignment
a := b;
Addition
a := b + c;
Subtraction
a := b - c;
Multiplication
a := b * c;
Division
a := b / c;
MOD
a := b mod c;
The data type is a very important factor. Note the following table:
Data types
rr
Syntax
Res
Res := 8 / 3;
Res := 8 / 3;
Res := 8.0 / 3;
Op 2
INT
INT
INT
REAL
INT
INT
2.0
REAL
REAL
INT
2.6667
INT
REAL
INT
Error
t fo
Res := 8.0 / 3;
Op 1
Result
You can see the that the result is dependent of the syntax as well as the
data types used.
Note:
no
14
TM246
Command Groups
BOOL
SINT
BOOL
BOOL
SINT
INT
INT
DINT
DINT
USINT
USINT
UINT
UINT
UDINT
UDINT
REAL
REAL
INT
DINT
USINT
UINT
UDINT
REAL
INT
DINT
USINT
UINT
UDINT
REAL
DINT
INT
UINT
UDINT
REAL
DINT
DINT
UDINT
REAL
UINT
UDINT
REAL
UDINT
REAL
DINT
INT
DINT
UINT
DINT
UINT
UDINT
UDINT
UDINT
UDINT
REAL
REAL
REAL
REAL
REAL
REAL
t fo
rr
Data
type
ep
rin
no
15
Command Groups
Example: Overflow?!
ep
rin
no
t fo
rr
The variable DINT_TotalWeight must be the data type DINT. At least one
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.
16
TM246
Command Groups
Task: Aquarium
ep
rin
t fo
rr
Don't forget that analog inputs and outputs must be data type INT.
no
Fig. 15 Aquarium
17
Command Groups
Logical comparison
expression
Example
ep
rin
Symbol
In high level languages like ST, simple constructs can be used to compare
variables. These return either the value TRUE or FALSE.
Equal to
<>
Not equal to
IF a <> b THEN
>
Greater than
IF a > b THEN
>=
IF a >= b THEN
<
Less than
IF a < b THEN
<=
IF a <= b THEN
Note:
IF a = b THEN
4.4 Decisions
rr
t fo
Simple IF statement
IF ELSE statement
IF ELSIF statement
Nested IF
no
Decision
IF THEN
Syntax
IF a > b THEN
Result := 1;
ELSIF THEN
ELSE
ELSE
Result := 3;
END_IF
18
TM246
END_IF
Description
1. Comparison
1. Statement(s)
2. Comparison (optional)
2. Statement(s)
Above IF statments are not TRUE (optional)
3. Statement(s)
End of decision
Command Groups
4.4.1 IF
ep
rin
t fo
rr
The IF statement is tested for the result TRUE. If the result is FALSE, the
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..
no
19
Command Groups
4.4.2 ELSE
ep
rin
rr
no
t fo
20
TM246
Command Groups
4.4.3 ELSIF
t fo
rr
ep
rin
no
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
21
Command Groups
ep
rin
t fo
rr
no
Fig. 23 Thermometer
Note:
22
TM246
Command Groups
rr
ep
rin
t fo
no
After three nesting levels, it is better to find another way to structure the
program.
23
Command Groups
ep
rin
no
t fo
rr
24
TM246
Command Groups
rr
ep
rin
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 fo
no
25
Command Groups
no
t fo
rr
ep
rin
26
TM246
Command Groups
ep
rin
The CASE statement compares a step variable with multiple values. If one
of these comparisons is a match, the steps that compare to that step are
executed. If none of the comparisons is a match, there is an ELSE branch
similar to an IF statement that is then executed.
After the statements have been executed, the program continues from after
the END_CASE statement.
Keywords
Syntax
CASE OF
Description
3,4,6..10:
Display := MATERIAL
For 1 and 5
Display := TEMP
For 2
Display := OPERATION
For 3,4,6,7,8,9,10
END_CASE
End of CASE
no
t fo
rr
END_CASE
Beginning of CASE
27
Command Groups
ep
rin
Only one step of the CASE statement is processed per program cycle.
Note:
rr
t fo
no
28
TM246
Command Groups
ep
rin
The fill level of a brewing tank is monitored for low, ok, and high levels.
Use an output for each of the low, ok, and high levels.
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.
no
t fo
rr
29
Command Groups
4.6 Loops
ep
rin
no
t fo
Limited
FOR
Unlimited
WHILE
REPEAT
rr
30
TM246
Command Groups
4.6.1 FOR
Key words
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.
Syntax
END_FOR
The section in {}
is optional.
ep
rin
FOR TO BY DO
Description
Loop body
statement(s)
END_FOR
End of FOR
no
t fo
rr
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".
The termination condition, the loop counter, is evaluated with every
repetition of the loop.
31
Command Groups
Task: Crane
Fig. 32 Crane
ep
rin
no
t fo
rr
32
TM246
Command Groups
4.6.2 WHILE
ep
rin
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
as a particular condition remains TRUE.
Keywords
WHILE DO
Description
WHILE i<4 DO
Boolean condition
Res := value + 1;
Statement
i := i + 1;
Statement
END_ WHILE
End of WHILE
t fo
rr
END_ WHILE
Syntax
no
If the condition never assumes the value FALSE, the statements are
repeated endlessly, resulting in a runtime error.
33
Command Groups
4.6.3 REPEAT
REPEAT
Syntax
REPEAT
UNTIL
Start loop
Res := value + 1;
Statement
i := i + 1;
Statement
UNTIL i > 4
Termination condition
END_ REPEAT
End loop
t fo
rr
END_ REPEAT
Description
ep
rin
Keywords
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.
no
The statements are executed repeatedly until the UNTIL condition is TRUE.
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.
34
TM246
Command Groups
4.6.4 EXIT
ep
rin
The EXIT statement can be used with all types of loops before their
termination condition occurs.
no
t fo
rr
35
Command Groups
ep
rin
rr
no
t fo
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.
36
TM246
Command Groups
ep
rin
no
t fo
rr
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.
37
Command Groups
ep
rin
Create a program that counts the bottles on a conveyor belt. Use the CTU
(up counter) function block found in the STANDARD library.
Note:
no
t fo
rr
The Automation Studio online help comes in handy when working with
function blocks.
38
TM246
Command Groups
ep
rin
no
t fo
rr
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.
39
Summary
5. SUMMARY
rr
ep
rin
no
t fo
After completing this training module, you are ready to program your own
ST tasks. You can always use the module as a reference.
This programming language is especially powerful when using arithmetic
functions and formulating mathematical calculations.
40
TM246
Exercises
6. EXERCISES
ep
rin
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
conveyer belt (doConvLift) is turned on until the box is completely on
the lift (diBoxLift).
Then the lift moves to the unloading position (doLiftUnload). When it
reaches this position (diLiftUnload), the box is moved to the unloading
belt.
t fo
rr
As soon as the box has left the lift, the lift is free for the next request.
no
41
Appendix
7. APPENDIX
7.1 Keywords
ep
rin
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
them; here is a list of more. Key words may not be used as variable names.
Keyword
Description
BIT_CLR
BIT_SET
BIT_TST
BY
CASE
DO
EDGE
rr
ACCESS
EDGENEG
EDGEPOS
ELSE
See IF statement.
ELSIF
t fo
See IF statement.
END_FOR
END_IF
See IF statement.
END_REPEAT
END_WHILE
EXIT
FOR
IF
See IF statement.
REPEAT
RETURN
THEN
See IF statement.
TO
UNTIL
WHILE
no
END_CASE
42
TM246
Appendix
7.2 Functions
Example
ep
rin
Function
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.
ACOS
ADR
AND
ASIN
ASR
ATAN
COS
EXP
EXPT
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
LOG
MAX
MIN
MOD
MOVE
The contents of the input variable are copied to the output variable.
The := symbol is used as the assignment operator.
t fo
rr
ABS
no
MUX
NOT
OR
ROL
43
Appendix
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
SHL
SHR
SIN
sizeof
SQRT
TAN
TRUNC
XOR
no
t fo
rr
ep
rin
ROR
44
TM246
Appendix
7.3 Solutions
Task: Aquarium
ep
rin
rr
no
t fo
45
Appendix
t fo
Task: Crane
rr
ep
rin
no
46
TM246
Appendix
no
t fo
rr
ep
rin
47
Appendix
no
t fo
rr
ep
rin
Notes
48
TM246
rr
ep
rin
Appendix
t fo
no
49
t fo
no
Internationality
50
TM246
Weblink
TM246TRE.00-ENG 0907
2007 by B&R. All rights reserved.
All trademarks presented are the property of their respective company.
We reserve the right to make technical changes.
ep
rin
rr
Appendix