Вы находитесь на странице: 1из 17

Chapter - Structured Text (ST) Programming

P R O G R A M m a in
VA R N o t e : A lle n B r a d le y d o e s n o t im p le m e n t
i : IN T ; th e s ta n d a rd s o th a t th e p ro g ra m s c a n b e
EN D _VA R w r it t e n w it h t e x t o n ly . W h e n p r o g r a m
i := 0 ; m in g in R S L o g ix , o n ly t h e s e c t io n in d i
REPEA T c a t e d t o t h e le f t w o u ld b e e n t e r e d . T h e
i := i + 1 ; v a r ia b le i w o u ld b e d e f in e d a s a t a g ,
U N T IL i > = 1 0 ; a n d t h e p r o g r a m w o u ld b e d e f in e d a s a
EN D _R EP EA T; ta s k .
EN D _PRO G RA M
Structured Programming
F U N C T I O N s a m p le
G O O D IN P U T _ V A R
s ta rt : B O O L ; ( * a N O s t a r t in p u t * )
s to p : B O O L ; ( * a N C s t o p in p u t * )
E N D _V A R
O U TP U T_V A R
m o t o r : B O O L ; ( * a m o t o r c o n t r o l r e la y
*)
E N D _V A R
m o t o r := ( m o t o r + s t a r t ) * s t o p ; ( * g e t t h e m o t o r o u t p u t * )
E N D _ F U N C T IO N

F U N C T I O N s a m p le
B A D IN P U T _ V A R
S T A R T :B O O L ; S T O P : B O O L ;
E N D _V A R
O U TP U T _V A R
M O T O R :B O O L ;
E N D _V A R
M O T O R := ( M O T O R + S T A R T ) * S T O P ; E N D _ F U N C T I O N
Declaration Description

VAR the general variable declaration


VAR_INPUT defines a variable list for a function
VAR_OUTPUT defines output variables from a function
VAR_IN_OUT defines variable that are both inputs and outputs from a function
VAR_EXTERNAL
VAR_GLOBAL a global variable
VAR_ACCESS
RETAIN a value will be retained when the power is cycled
CONSTANT a value that cannot be changed
AT can tie a variable to a specific location in memory (without this vari-
able locations are chosen by the compiler
END_VAR marks the end of a variable declaration
Variable Declarations
Text Program Line Description

VAR AT %B3:0 : WORD; END_VAR a word in bit memory


VAR AT %N7:0 : INT; END_VAR an integer in integer memory
VAR RETAIN AT %O:000 : WORD ; END_VAR makes output bits retentive
VAR_GLOBAL A AT %I:000/00 : BOOL ; END_VAR variable A as input bit
VAR_GLOBAL A AT %N7:0 : INT ; END_VAR variable A as an integer
VAR A AT %F8:0 : ARRAY [0..14] OF REAL; END_VAR an array A of 15 real values
VAR A : BOOL; END_VAR a boolean variable A
VAR A, B, C : INT ; END_VAR integers variables A, B, C
VAR A : STRING[10] ; END_VAR a string A of length 10
VAR A : ARRAY[1..5,1..6,1..7] OF INT; END_VAR a 5x6x7 array A of integers
VAR RETAIN RTBT A : ARRAY[1..5,1..6] OF INT; a 5x6 array of integers, filled
END_VAR with zeros after power off
VAR A : B; END_VAR A is data type B
VAR CONSTANT A : REAL := 5.12345 ; END_VAR a constant value A
VAR A AT %N7:0 : INT := 55; END_VAR A starts with 55
VAR A : ARRAY[1..5] OF INT := [5(3)]; END_VAR A starts with 3 in all 5 spots
VAR A : STRING[10] := test; END_VAR A contains test initially
VAR A : ARRAY[0..2] OF BOOL := [1,0,1]; END_VAR an array of bits
VAR A : ARRAY[0..1,1..5] OF INT := [5(1),5(2)]; an array of integers filled with 1
END_VAR for [0,x] and 2 for [1,x]
Literal Values

number type examples

integers -100, 0, 100, 10_000


real numbers -100.0, 0.0, 100.0, 10_000.0
real with exponents -1.0E-2, -1.0e-2, 0.0e0, 1.0E2
binary numbers 2#111111111, 2#1111_1111, 2#1111_1101_0110_0101
octal numbers 8#123, 8#777, 8#14
hexadecimal numbers 16#FF, 16#ff, 16#9a, 16#01
boolean 0, FALSE, 1, TRUE
Character Strings

example description

a zero length string


, a, $, $$ a single character, a space, or a, or a single quote, or a dollar
sign $
$R$L, $r$l,$0D$0A produces ASCII CR, LF combination - end of line characters
$P, $p form feed, will go to the top of the next page
$T, 4t tab
this%Tis a test$R$L a string that results in this<TAB>is a test<NEXT LINE>
Time Value Examples

25ms T#25ms, T#25.0ms, TIME#25.0ms, T#-25ms, t#25ms


5.5hours TIME#5.3h, T#5.3h, T#5h_30m, T#5h30m
3days, 5hours, 6min, 36sec TIME#3d5h6m36s, T#3d_5h_6m_36s

description examples

date values DATE#1996-12-25, D#1996-12-25


time of day TIME_OF_DAY#12:42:50.92, TOD#12:42:50.92
date and time DATE_AND_TIME#1996-12-25-12:42:50.92, DT#1996-12-25-12:42:50.92
:= a s s ig n s a v a lu e t o a v a r ia b le
+ a d d it io n
- s u b t r a c t io n
/ d iv is io n
* m u lt ip lic a t io n
M O D (A ,B ) m o d u lo - t h is p r o v id e s t h e r e m a in d e r f o r a n in t e g e r d iv id e A / B
S Q R (A ) sq u a re ro o t o f A
F R D (A ) f r o m B C D t o d e c im a l
T O D (A ) t o B C D f r o m d e c im a l
N E G (A ) r e v e r s e s ig n + / -
L N (A ) n a t u r a l lo g a r it h m
L O G (A ) b a s e 1 0 lo g a r it h m
D E G (A ) f r o m r a d ia n s t o d e g r e e s
R A D (A ) t o r a d ia n s f r o m d e g r e e s
S IN (A ) s in e
C O S (A ) c o s in e
T A N (A ) ta n g e n t
A S N (A ) a r c s in e , in v e r s e s in e
A C S (A ) a r c c o s in e - in v e r s e c o s in e
A T N (A ) a r c t a n - in v e r s e t a n g e n t
X P Y (A ,B ) A to th e p o w e r o f B
A **B A to th e p o w e r o f B
> greater than
>= greater than or equal
= equal
<= less than or equal
< less than
<> not equal

A N D (A ,B ) lo g ic a l a n d
O R (A ,B ) lo g ic a l o r
X O R (A ,B ) e x c lu s iv e o r
N O T (A ) lo g ic a l n o t
! lo g ic a l n o t ( n o t e : n o t im p le m e n t e d o n A B c o n t r o lle r s )
! - ( N o t e : n o t a v a ila b le o n A B c o n t r o lle r s )
()
f u n c t io n s
X P Y , **
n e g a t io n
h ig h e s t p r io r ity

S Q R , T O D , F R D , N O T , N E G , L N , L O G , D E G , R A D , S IN , C O S , T A N , A S N , A C S , A T N
* , /, M O D
+ , -
> , > = , = , < = , < , < >
A N D ( fo r w o r d )
X O R ( fo r w o r d )
O R ( fo r w o r d )
A N D ( b it )
X O R ( b it )
O R ( b it )
la d d e r in s t r u c t io n s
IF-THEN-ELSIF-ELSE-END_IF; normal if-then structure
CASE-value:-ELSE-END_CASE; a case switching function
FOR-TO-BY-DO-END_FOR; for-next loop
WHILE-DO-END_WHILE;

RETAIN() causes a bit to be retentive


IIN(); immediate input update
EXIT; will quit a FOR or WHILE loop
EMPTY
a v g := 0 ;
F O R ( i := 0 T O 4 ) D O
a v g := a v g + f [ i] ;
EN D _FO R;
a v g := a v g / 5 ;

a v g := 0 ;
i := 0 ;
W H IL E (i < 5 ) D O
a v g := a v g + f [ i] ;
i := i + 1 ;
E N D _ W H IL E ;
a v g := a v g / 5 ;
t . T im e r E n a b le := b ;
T O N R (t) ;
IF (a = 1 ) T H E N
x := 1 ;
E L S IF (b = 1 A N D t.D N = 1 ) T H E N
y := 1 ;
I F ( I :0 0 0 / 0 2 = 0 ) T H E N
z := 1 ;
E N D _ IF ;
ELSE
x := 0 ;
y := 0 ;
z := 0 ;
E N D _ IF ;
C A S E te s t O F
0:
a . 0 := 1;
1:
a . 1 := 1;
2:
a . 2 := 1;
3:
a . 3 := 1;
ELSE
a := 0 ;
EN D _C A SE;
F R D ( b c d _ in p u t , d e la y _ t im e ) ;
t . P R E := d e la y _ t im e ;
I F ( t e s t _ in p u t ) T H E N
t . E n a b le T im e r := 1 ;
ELSE
t . E n a b le T im e r := 0 ;
E N D _ IF ;
T O N R (t) ;
s e t := t . D N ;
....
D := T E S T ( 1 . 3 , 3 . 4 ) ; ( * s a m p le c a llin g p r o g r a m , h e r e C w ill d e f a u lt t o 3 . 1 4 * )
E := T E S T ( 1 . 3 , 3 . 4 , 6 . 2 8 ) ; ( * h e r e C w ill b e g iv e n a n e w v a lu e * )
....

F U N C T IO N T E S T : R E A L
V A R _ I N P U T A , B : R E A L ; C : R E A L := 3 . 1 4 1 5 9 ; E N D V A R
T E S T := ( A + B ) / C ;
E N D _ F U N C T IO N
S B R ();
I F S :F S T H E N
s t a t e := 0 ;
g r e e n _ E W . T im e r E n a b le := 1 ;
y e llo w _ E W . T im e r E n a b le := 0 ;
g r e e n _ N S . T im e r E n a b le := 0 ;
y e llo w _ N S . T im e r E n a b le := 0 ; N o t e : T h is e x a m p le is f o r t h e A B
E N D _ IF ; C o n t r o lL o g ix p la t f o r m , s o it
d o e s n o t s h o w th e n o r m a l
T O N R ( g r e e n _ E W ) ; T O N R ( y e llo w _ E W ) ; f u n c t io n a n d t a g d e f in it io n s .
T O N R ( g r e e n _ N S ) ; T O N R ( y e llo w _ N S ) ; T h e s e a r e d o n e s e p a r a t e ly in
t h e t a g e d it o r .
C A S E s ta te O F
0: IF g re e n _ E W .D N T H E N s ta te : D I N T
s t a t e := 1 ; g re e n _ E W : F B D _ T IM E R
g r e e n _ E W . T im e r E n a b le := 0 ; y e llo w _ E W : F B D _ T I M E R
y e llo w _ E W . T im e r E n a b le := 1 ; g re e n _ N S : F B D _ T IM E R
E N D _ IF y e llo w _ N S : F B D _ T I M E R
1: I F y e llo w _ E W . D N T H E N lig h t _ E W _ g r e e n : B O O L a lia s =
s t a t e := 2 ; r a c k :1 :O . D a t a . 0
y e llo w _ E W . T im e r E n a b le := 0 ; lig h t _ E W _ y e llo w : B O O L a lia s =
g r e e n _ N S . T im e r E n a b le := 1 ; r a c k :1 :O . D a t a . 1
E N D _ IF lig h t _ E W _ r e d : B O O L a lia s =
2: IF g re e n _ N S .D N T H E N r a c k :1 :O . D a t a . 2
s t a t e := 3 ; lig h t _ N S _ g r e e n : B O O L a lia s =
g r e e n _ N S . T im e r E n a b le := 0 ; r a c k :1 :O . D a t a . 3
y e llo w _ N S . T im e r E n a b le := 1 ; lig h t _ N S _ y e llo w : B O O L a lia s =
E N D _ IF r a c k :1 :O . D a t a . 4
3: I F y e llo w _ N S . D N T H E N lig h t _ N S _ r e d : B O O L a lia s =
s t a t e := 0 ; r a c k :1 :O . D a t a . 5
y e llo w _ N S . T im e r E n a b le := 0 ;
g r e e n _ E W . T im e r E n a b le := 1 ;
E N D _ IF

lig h t _ EW _ g r e e n := ( s t a t e = 0 ) ;
lig h t _ EW _ y e llo w := ( s t a t e = 1 ) ;
lig h t _ EW _ r e d := ( s t a t e = 2 ) O R ( s t a t e = 3 ) ;
lig h t _ N S _ g r e e n := ( s t a t e = 2 ) ;
lig h t _ N S _ y e llo w := ( s t a t e = 3 ) ;
lig h t _ N S _ r e d := ( s t a t e = 0 ) O R ( s t a t e = 1 ) ;

R E T ();

Вам также может понравиться