Documente Academic
Documente Profesional
Documente Cultură
Topics:
Objectives:
19.1 INTRODUCTION
If you know how to program in any high level language, such as Basic or C, you
will be comfortable with Structured Text (ST) programming. ST programming is part of
the IEC 61131 standard. An example program is shown in Figure 19.1. 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 declarations. 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.
plc st - 19.2
PROGRAM main
VAR
i : INT;
END_VAR
i := 0;
REPEAT
i := i + 1;
UNTIL i >= 10;
END_REPEAT;
END_PROGRAM
ST has been designed to work with the other PLC programming languages. For
example, a ladder logic program can call a structured text subroutine. At the time of writ-
ing, Allen Bradley offers limited support for ST programming, but they will expand their
support in the future.
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
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 19.4 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.
Declaration Description
Basic numbers are shown below. Note the underline _ can be ignored, it can be
used to break up long numbers, ie. 10_000 = 10000.
plc st - 19.6
example description
Date values are given below. These are meant to be used to compare to system
time and date clocks.
description examples
The precedence of operations are listed below from highest to lowest. As normal
expressions 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.)
!
()
XPY, **
SQR, TOD, FRD, NOT, NEG, LN, LOG, DEG, RAD, SIN, COS, TAN, ASN, ACS, ATN
*, /, MOD
highest priority
+, -
AND (for word)
XOR (for word)
OR (for word)
>, >=, =, <=, <, <>
AND (bit)
XOR (bit)
OR (bit)
ladder instructions
Consider the program below to find the average of five values in floating point
memory.
plc st - 19.11
F8:10 := 0;
FOR (N7:0 := 0 TO 4) DO
F8:10 := F8:10 + F8:[N7:0];
END_FOR;
Consider the program below to find the average of five values in floating point
memory.
F8:10 := 0;
WHILE (N7:0 < 5) DO
F8:10 := F8:10 + F8:[N7:0];
N7:0 := N7:0 + 1;
END_WHILE;
The example below will set different outputs depending upon the stat of an input.
plc st - 19.12
IF (I:000/00 = 1) THEN
O:001/00 := 1;
ELSIF (I:000/01 = 1 AND T4:0/DN = 1) THEN
O:001/00 := 1;
IF (I:000/02 = 0) THEN
O:001/01 := 1;
END_IF;
ELSE
O:001/01 := 1;
END_IF;
The example below will set output bits 00-03 depending upon the value of the
integer in N7:0, if the value is not between 0 and 3 the outputs will all go off.
CASE N7:0 OF
0:
O:000/00 := 1;
1:
O:000/01 := 1;
2:
O:000/02 := 1;
3:
O:000/03 := 1;
ELSE
O:000 := 0;
END_CASE;
The example below accepts a BCD input from (I:000) and will use it to change
plc st - 19.13
the delay time for an on delay timer that will examine input I:002/00 drive output O:001/
00.
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.
Normal ladder logic output functions can be used except for those listed below.
not valid output functions: JMP, END, MCR, FOR, BRK, NXT, MSG, SDS, DFA,
AND, OR, XOR, TND
valid output functions include: OTL, OTU, OTE, TON, TOF, RTO, CTU, CTD, RES,
ADD, SUB, MUL, DIV, etc...
The list below gives a most of the IEC1131-3 defined functions with arguments.
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 line preset values for timers. When arguments are left out they default to values,
typically 0.
plc st - 19.16
Function Description
Function Description
Write a structured text program to sort a set of ten integer numbers and then find the
median value.
....
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 SUMMARY
plc st - 19.20