340
Function Description
REAL_TO_INT(A); converts A from real to integer
REPLACE(IN1:=A,IN2:=B,L:= will replace C characters at position D in string A with
C,P:=D); string B
RIGHT(IN:=A,L:=B); will return the right A characters of string B
ROL(IN:=A,N:=B); rolls left value A of length B bits
ROR(IN:=A,N:=B); rolls right value A of length B bits
RS(A,B); RS flip flop with input A and B
RTC(IN:=A,PDT:=B); will set and/or return current system time
R_TRIG(A); a rising edge trigger
SEL(A,B,C); if a=0 output B if A=1 output C
SHL(IN:=A,N:=B); shift left value A of length B bits
SHR(IN:=A,N:=B); shift right value A of length B bits
SIN(A); finds the sine of A
SQRT(A); square root of A
SR(S1:=A,R:=B); SR flipflop with inputs A and B
SUB(A,B); A-B
TAN(A); finds the tangent of A
TOF(IN:=A,PT:=B); off delay timer
TON(IN:=A,PT:=B); on delay timer
TP(IN:=A,PT:=B); pulse timer - a rising edge fires a fixed period pulse
TRUNC(A); converts a real to an integer, no rounding
XOR(A,B,...); logical exclusive or of inputs A,B,...
Figure 281 Structured Text Functions
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 *)
....
FUNCTION TEST : REAL
VAR_INPUT A, B : REAL; C : REAL := 3.14159; END VAR
TEST := (A + B) / C;
END_FUNCTION
Figure 282 Declaration of a Function