Structured Text Programming PDF Free
Structured Text Programming PDF Free
Topics:
• Basic language structure and syntax
• Variables, functions, values
• Program flow commands and structures
• Function names
• Program Example
Objectives:
• To be able to write functions in Structured Text programs
• To understand the parallels between Ladder Logic and Structured Text
• To understand differences between Allen Bradley and the standard
19.1 INTRODUCTION
If you know how to program in any high level language, such as Basic or C, you will be com-
fortable with Structured Text (ST) programming. ST programming is part of the IEC 61131 standard.
An example program is shown in Figure 261. The program is called main and is defined between the
statements PROGRAM and END_PROGRAM. Every program begins with statements the define the
variables. In this case the variable i is defined to be an integer. The program follows the variable decla-
rations. This program counts from 0 to 10 with a loop. When the example program starts the value of
integer memory i will be set to zero. The REPEAT and END_REPEAT statements define the loop. The
UNTIL statement defines when the loop must end. A line is present to increment the value of i for each
loop.
PROGRAM main
VAR Note: Allen Bradley does not implement
i : INT; the standard so that the programs can be
END_VAR written with text only. When program-
i := 0; ming in RSLogix, only the section indi-
REPEAT cated to the left would be entered. The
i := i + 1; variable ’i’ would be defined as a tag,
UNTIL i >= 10; and the program would be defined as a
END_REPEAT; task.
END_PROGRAM
One important difference between ST and traditional programming languages is the nature of
330
program flow control. A ST program will be run from beginning to end many times each second. A tra-
ditional program should not reach the end until it is completely finished. In the previous example the
loop could lead to a program that (with some modification) might go into an infinite loop. If this were
to happen during a control application the controller would stop responding, the process might become
dangerous, and the controller watchdog timer would force a fault.
ST has been designed to work with the other PLC programming languages. For example, a lad-
der logic program can call a structured text subroutine.
The language is composed of written statements separated by semicolons. The statements use
predefined statements and program subroutines to change variables. The variables can be explicitly
defined values, internally stored variables, or inputs and outputs. Spaces can be used to separate state-
ments and variables, although they are not often necessary. Structured text is not case sensitive, but it
can be useful to make variables lower case, and make statements upper case. Indenting and comments
should also be used to increase readability and documents the program. Consider the example shown in
Figure 262.
FUNCTION sample
GOOD INPUT_VAR
start : BOOL; (* a NO start input *)
stop : BOOL; (* a NC stop input *)
END_VAR
OUTPUT_VAR
motor : BOOL;(* a motor control relay
*)
END_VAR
motor := (motor + start) * stop;(* get the motor output *)
END_FUNCTION
FUNCTION sample
BAD INPUT_VAR
START:BOOL;STOP:BOOL;
END_VAR
OUTPUT_VAR
MOTOR:BOOL;
END_VAR
MOTOR:=(MOTOR+START)*STOP;END_FUNCTION
ST programs allow named variables to be defined. This is similar to the use of symbols when
programming in ladder logic. When selecting variable names they must begin with a letter, but after
that they can include combinations of letters, numbers, and some symbols such as ’_’. Variable names
are not case sensitive and can include any combination of upper and lower case letters. Variable names
must also be the same as other key words in the system as shown in Figure 263. In addition, these vari-
able must not have the same name as predefined functions, or user defined functions.
Invalid variable names: START, DATA, PROJECT, SFC, SFC2, LADDER, I/O, ASCII,
CAR, FORCE, PLC2, CONFIG, INC, ALL, YES, NO, STRUCTURED TEXT
When defining variables one of the declarations in Figure 264 can be used. These define the
scope of the variables. The VAR_INPUT, VAR_OUTPUT and VAR_IN_OUT declarations are used for
variables that are passed as arguments to the program or function. The RETAIN declaration is used to
retain a variable value, even when the PLC power has been cycled. This is similar to a latch application.
As mentioned before these are not used when writing Allen Bradley programs, but they are used when
defining tags to be used by the structured programs.
Declaration Description
Basic numbers are shown in Figure 266. Note the underline ‘_’ can be ignored, it can be used to
break up long numbers, ie. 10_000 = 10000. These are the literal values discussed for Ladder Logic.
example description
Basic time and date values are described in Figure 268 and Figure 269. Although it should be
noted that for ControlLogix the GSV function is used to get the values.
description examples
The math functions available for structured text programs are listed in Figure 270. It is worth
noting that these functions match the structure of those available for ladder logic. Other, more
advanced, functions are also available - a general rule of thumb is if a function is available in one lan-
guage, it is often available for others.
334
Functions for logical comparison are given in Figure 271. These will be used in expressions
such as IF-THEN statements.
Boolean algebra functions are available, as shown in Figure 272. The can be applied to bits or
integers.
335
The precedence of operations are listed in Figure 273 from highest to lowest. As normal expres-
sions that are the most deeply nested between brackets will be solved first. (Note: when in doubt use
brackets to ensure you get the sequence you expect.)
SQR, TOD, FRD, NOT, NEG, LN, LOG, DEG, RAD, SIN, COS, TAN, ASN, ACS, ATN
*, /, MOD
+, -
>, >=, =, <=, <, <>
AND (for word)
XOR (for word)
OR (for word)
AND (bit)
XOR (bit)
OR (bit)
ladder instructions
Consider the program in Figure 276 to find the average of five values in a real array ’f[]’. The
FOR loop in the example will loop five times adding the array values. After that the sum is divided to
get the average.
avg := 0;
FOR (i := 0 TO 4) DO
avg := avg + f[i];
END_FOR;
avg := avg / 5;
The previous example is implemented with a WHILE loop in Figure 277. The main differences
is that the initial value and update for ’i’ must be done manually.
avg := 0;
i := 0;
WHILE (i < 5) DO
avg := avg + f[i];
i := i + 1;
END_WHILE;
avg := avg / 5;
The example in Figure 278 shows the use of an IF statement. The example begins with a timer.
These are handled slightly differently in ST programs. In this case if ’b’ is true the timer will be active,
if it is false the timer will reset. The second instruction calls ’TONR’ to update the timer. (Note: ST pro-
grams use the FBD_TIMER type, instead of the TIMER type.) The IF statement works as normal, only
one of the three cases will occur with the ELSE defining the default if the other two fail.
337
t.TimerEnable := b;
TONR(t);
IF (a = 1) THEN
x := 1;
ELSIF (b = 1 AND t.DN = 1) THEN
y := 1;
IF (I:000/02 = 0) THEN
z := 1;
END_IF;
ELSE
x := 0;
y := 0;
z := 0;
END_IF;
Figure 279 shows the use of a CASE statement to set bits 0 to 3 of ’a’ based upon the value of
’test’. In the event none of the values are matched, ’a’ will be set to zero, turning off all bits.
CASE test OF
0:
a.0 := 1;
1:
a.1 := 1;
2:
a.2 := 1;
3:
a.3 := 1;
ELSE
a := 0;
END_CASE;
The example in Figure 280 accepts a BCD input from ’bcd_input’ and uses it to change the
delay time for TON delay time. When the input ’test_input’ is true the time will count. When the timer
is done ’set’ will become true.
338
Most of the IEC61131-3 defined functions with arguments are given in Figure 281. Some of the
functions can be overloaded, for example ADD could have more than two values to add, and others
have optional arguments. In most cases the optional arguments are things like preset values for timers.
When arguments are left out they default to values, typically 0. ControlLogix uses many of the standard
function names and arguments but does not support the overloading part of the standard.
339
Function Description
Function Description
Control programs can become very large. When written in a single program these become con-
fusing, and hard to write/debug. The best way to avoid the endless main program is to use subroutines
to divide the main program. The IEC61131 standard allows the definition of subroutines/functions as
shown in Figure 282. The function will accept up to three inputs and perform a simple calculation. It
then returns one value. As mentioned before ControlLogix does not support overloading, so the func-
tion would not be able to have a variable size argument list.
....
D := TEST(1.3, 3.4); (* sample calling program, here C will default to 3.14 *)
E := TEST(1.3, 3.4, 6.28); (* here C will be given a new value *)
....
19.3 AN EXAMPLE
The example beginning in Figure 284 shows a subroutine implementing traffic lights in ST for
the ControlLogix processor. The variable ’state’ is used to keep track of the current state of the lights.
Timer enable bits are used to determine which transition should be checked. Finally the value of ’state’
is used to set the outputs. (Note: this is possible because ’=’ and ’:=’ are not the same.) This subroutine
would be stored under a name such as ’TrafficLights’. It would then be called from the main program as
shown in Figure 283.
JSR
Function Name: TrafficLights
SBR();
IF S:FS THEN
state := 0;
green_EW.TimerEnable := 1;
yellow_EW.TimerEnable := 0;
green_NS.TimerEnable := 0;
yellow_NS.TimerEnable := 0; Note: This example is for the AB
END_IF; ControlLogix platform, so it
does not show the normal
TONR(green_EW); TONR(yellow_EW); function and tag definitions.
TONR(green_NS); TONR(yellow_NS); These are done separately in
the tag editor.
CASE state OF
0: IF green_EW.DN THEN state : DINT
state :=1; green_EW : FBD_TIMER
green_EW.TimerEnable := 0; yellow_EW : FBD_TIMER
yellow_EW.TimerEnable := 1; green_NS : FBD_TIMER
END_IF yellow_NS : FBD_TIMER
1: IF yellow_EW.DN THEN light_EW_green : BOOL alias =
state :=2; rack:1:O.Data.0
yellow_EW.TimerEnable := 0; light_EW_yellow : BOOL alias =
green_NS.TimerEnable := 1; rack:1:O.Data.1
END_IF light_EW_red : BOOL alias =
2: IF green_NS.DN THEN rack:1:O.Data.2
state :=3; light_NS_green : BOOL alias =
green_NS.TimerEnable := 0; rack:1:O.Data.3
yellow_NS.TimerEnable := 1; light_NS_yellow : BOOL alias =
END_IF rack:1:O.Data.4
3: IF yellow_NS.DN THEN light_NS_red : BOOL alias =
state :=0; rack:1:O.Data.5
yellow_NS.TimerEnable := 0;
green_EW.TimerEnable := 1;
END_IF
RET();
19.4 SUMMARY
1. Write a structured text program that will replace the following ladder logic.
A active
EQU MOV
Source A 20 Source -2
Source B n Dest n
A LEQ
Source A n active
Source B 20
active
active
ADD
Source A n
Source B 2
Dest n
LOG
Source n
Dest x
SUB
Source A x
Source B 1
Dest x
MUL
Source A x
Source B 2
Dest x
2. Implement the following Boolean equations in a Structured Text program. If the program was for a state
machine what changes would be required to make it work?
4. A temperature value is stored in F8:0. When it rises above 40 the following sequence should occur once.
Write a ladder logic program that implement this function with a Structured Text program.
horn
2 5 11 15 t (s)
5. Write a structured text program that will replace the following ladder logic.
A active
EQU MOV
Source A 20 Source -2
Source B n Dest n
A LEQ
Source A n active
Source B 20
active
active
ADD
Source A n
Source B 2
Dest n
LOG
Source n
Dest x
SUB
Source A x
Source B 1
Dest x
MUL
Source A x
Source B 2
Dest x
345
2. Write a structured text program to control a press that has an advance and retract with limit switches. The
press is started and stopped with start and stop buttons.
3. Write a structured text program to sort a set of ten integer numbers and then find the median value.
346