ST Supported Functions Coming Soon
This section lists all the supported IEC 61131-3 Structured Text functions.
Standard Functions
Instruction | Example | Description |
---|---|---|
ABS | A := ABS(B); | Absolute value. |
SQRT | A := SQRT(B); | Square root. |
LN | A := LN(B); | Natural logarithm. |
LOG | A := LOG(B); | Logarithm base 10. |
EXP | A := EXP(B); | Natural exponential. |
SIN | A := SIN(B); | Sine of input in radians. |
COS | A := COS(B); | Cosine in radians. |
TAN | A := TAN(B); | Tangent in radians. |
ASIN | A := ASIN(B); | Principal arc sine. |
ACOS | A := ACOS(B); | Principal arc cosine. |
ATAN | A := ATAN(B); | Principal arc tangent. |
Arithmetic Functions
Instruction | Symbol | Example | Description |
---|---|---|---|
ADD | + | A := ADD(B,C); or A := B+C; | Adds two or more numerical values. |
MUL | * | A := MUL(B,C); or A := B*C; | Multiplies two or more numerical values. |
SUB | - | A := SUB(B,C); or A := B-C; | Subtracts the second numerical value from the first. |
DIV | / | A := DIV(B,C); or A := B/C; | Divides the first numerical value by the second. |
MOD | A := MOD(B,C); | Computes the remainder of division of the first numerical value by the second. | |
EXPT | ** | A := EXPT(B,C); or A := B**C; | Raises the first numerical value to the power of the second. |
MOVE | := | A := B; | Assigns the value of one variable to another. |
Bit Shift Functions
Instruction | Example | Description |
---|---|---|
SHL | A := SHL(IN:=B, N:=5); | Left shifted by N bits, zero filled on the right. |
SHR | A := SHR(IN:=B, N:=5); | Right shifted by N bits, zero filled on the left. |
ROR | A := ROR(IN:=B, N:=5); | Right rotated by N bits, circular. |
ROL | A := ROL(IN:=B, N:=5); | Left rotated by N bits, circular. |
Bitwise Boolean Functions
Instruction | Symbol | Example | Description |
---|---|---|---|
AND | & | A := AND(B,C); or A := B & C; | Logical conjunction operator. Returns TRUE if both operands are TRUE, otherwise FALSE. |
OR | A := OR(B,C); or A := B OR C; | Logical disjunction operator. Returns TRUE if at least one operand is TRUE, otherwise FALSE. | |
XOR | A := XOR(A,B); or A := B XOR C; | Logical exclusive OR operator. Returns TRUE if exactly one operand is TRUE, otherwise FALSE. | |
NOT | A := NOT(B); or A := NOT B; | Logical negation operator. Inverts the value of the operand. If the operand is TRUE, NOT returns FALSE; if the operand is FALSE, NOT returns TRUE. |
Selection Functions
Instruction | Example | Description |
---|---|---|
SEL | A := SEL(G:=0,IN0:=X,IN1:=5); | Selects between two inputs based on a Boolean condition. If the condition (G) is true (1), it outputs IN1; if false (0), it outputs IN0. |
MAX | A := MAX(B,C,D); | Returns the maximum value among all its inputs. |
MIN | A := MIN(B,C,D); | Returns the minimum value among all its inputs. |
LIMIT | A := LIMIT(IN:=B,MN:=0,MX:=5); | Limits an input (IN) value between a minimum (MN) and a maximum (MX) bound. |
MUX | A := MUX(0, B, C, D); | Selects one of several inputs based on the index (K) and outputs it. |
Comparison Functions
Instruction | Symbol | Example | Description |
---|---|---|---|
GT | > | A := IN1>IN2; | Output TRUE if one input is greater than another. Otherwise false. |
GE | >= | A := IN1>=IN2; | Output TRUE if one input is greater than or equal to another. Otherwise false. |
EQ | = | A := IN1=IN2; | Output TRUE if one input is exactly equal to another. Otherwise false. |
LE | <= | A := IN1<=IN2; | Output TRUE if one input is less than or equal to another. Otherwise false. |
LT | < | A := IN1<IN2; | Output TRUE if one input is less than another. Otherwise false. |
NE | <> | A := IN1 <> IN2; | Output TRUE if one input is exactly not equal to another. Otherwise, false. |
Character String Functions
Instruction | Example | Description |
---|---|---|
LEN | A := LEN('ASTRING'); | Returns the length of a string. |
LEFT | A := LEFT(IN:='ASTR',L:=3); | Extracts a specified number of characters from the beginning (left side) of a string. It requires two parameters: the string and the number of characters to extract. |
RIGHT | A := RIGHT(IN:='ASTR',L:=3); | Extracts a specified number of characters from the end (right side) of a string. Similar to LEFT, it requires the string and the number of characters to extract. |
MID | A := MID(IN:='ASTR',L:=2,P:=2); | Retrieves a substring from a string, starting at a specified position and with a specified length. It requires three parameters: the original string, the start position, and the number of characters to extract. |
CONCAT | A := CONCAT('AB','CD','E'); | Concatenates two or more strings into one. It takes two or more string parameters and returns a single string that is the combination of all the inputs. |
INSERT | A:=INSERT(IN1:='ABC',IN2:='XY',P=2 ); | Inserts a string into another string at a specified position. It requires three parameters: the string to insert, the target string, and the position at which to insert the first string into the second. |
DELETE | A := DELETE(IN:='ABXYC',L:=2, P:=3); | Removes a specified number of characters from a string, starting at a specified position. It requires three parameters: the original string, the start position, and the number of characters to delete. |
REPLACE | A := REPLACE(IN1:='ABCDE',IN2:='X', L:=2, P:=3) | Replaces part of a string with another string. It typically requires four parameters: the original string, the substring to replace, the replacement string, and sometimes the start position for the replacement. |
FIND | A := FIND(IN1:='ABCBC',IN2:='BC') | Searches for a substring within another string and returns the position of the first occurrence. If the substring is not found, it returns 0. It requires at least two parameters: the substring to find and the string to search within. |
Functions of Time Data Types
Instruction | Example | Description |
---|---|---|
ADD_TIME | A := ADD_TIME(IN1:=T#2h20m, IN2:=T#30m); | Adds a time interval to a time value. |
ADD_TOD_TIME | A := ADD_TOD_TIME(IN1:=TOD#14:00:00, IN2:=T#2h); | Adds a time interval to a time-of-day value, resulting in a new time-of-day value. |
ADD_DT_TIME | A := ADD_DT_TIME(IN1:=DT#2024-04-01-15:00:00, IN2:=T#24h); | Adds a time interval to a datetime value, resulting in a new datetime value. |
SUB_TIME | A := SUB_TIME(IN1:=T#2h, IN2:=T#30m); | Subtracts a time interval from a time value. |
SUB_DATE_DATE | A := SUB_DATE_DATE(IN1:=D#2024-04-10, IN2:=D#2023-04-01); | Subtracts one date from another, resulting in a time interval that represents the difference. |
SUB_TOD_TIME | A := SUB_TOD_TIME(IN1:=TOD#15:00:00, IN2:=T#2h); | Subtracts a time interval from a time-of-day value, resulting in a new time-of-day value. |
SUB_TOD_TOD | A := SUB_TOD_TOD(IN1:=TOD#17:00:00, IN2:=TOD#15:00:00); | Subtracts one time-of-day value from another, resulting in a time interval that represents the difference. |
SUB_DT_TIME | A := SUB_DT_TIME(IN1:=DT#2024-04-02-12:00:00, IN2:=T#12h); | Subtracts a time interval from a datetime value, resulting in a new datetime value. |
SUB_DT_DT | A := SUB_DT_DT(IN1:=DT#2024-04-02-12:00:00, IN2:=DT#2024-04-01-12:00:00) | Subtracts one datetime value from another, resulting in a time interval that represents the difference. |
MULTIME | A := MULTIME(IN1:=T#1h, IN2:=3); | Multiplies a time interval by a factor, resulting in a new time interval. |
DIVTIME | A := DIVTIME(IN1:=T#1h, IN2:=2); | Divides a time interval by a factor, resulting in a new time interval. |
CONCAT_DATE_TOD | A := CONCAT_DATE_TOD(IN1:=D#2024-04-01, IN2:=TOD#12:00:00); | Combines a date and a time-of-day value into a single datetime value. |
DT_TO_TOD | A := DT_TO_TOD(IN:=DT#2024-04-01-15:00:00); | Converts a datetime value into a time-of-day value, essentially extracting the time portion of a datetime. |
DT_TO_DATE | A := DT_TO_DATE(IN:=DT#2023-04-01-15:00:00); | Converts a datetime value into a date value, essentially extracting the date portion of a datetime. |
Bistable Function Blocks
Instruction | Example | Description |
---|---|---|
SR | SR(S:=Set, R:=Reset, Q=>Output); | Sets its output (Q) to TRUE when the Set (S) input is activated. The output remains TRUE until the Reset (R) input is activated, which then sets the output to FALSE. If both inputs are active, the Set input has priority. |
RS | RS(R:=Reset, S:=Set, Q=>Output); | Resets its output (Q) to FALSE when the Reset (R) input is activated. The output remains FALSE until the Set (S) input is activated, which then sets the output to TRUE. If both inputs are active, the Reset input has priority. |
Edge Detection Function Blocks
Instruction | Example | Description |
---|---|---|
R_TRIG | R_TRIG(CLK:=InputSignal, Q=>OutputPulse); | Activates its output for one scan cycle when its input changes from FALSE to TRUE. |
F_TRIG | F_TRIG(CLK:=InputSignal, Q=>OutputPulse); | Activates its output for one scan cycle when its input changes from TRUE to FALSE. |
Counter Function Blocks
Instruction | Example | Description |
---|---|---|
CTU | CTU(CU:=IncrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Increments its count value on the rising edge of its input. |
CTU_DINT | CTU(CU:=IncrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Increments its count value on the rising edge of its input. Uses a double integer (DINT) for the counter value. |
CTU_LINT | CTU(CU:=IncrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Increments its count value on the rising edge of its input. Uses a long integer (LINT) for the counter value. |
CTU_DINT | CTU(CU:=IncrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Increments its count value on the rising edge of its input. Uses a long integer (DINT) for the counter value. |
CTU_UDINT | CTU(CU:=IncrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Increments its count value on the rising edge of its input. Uses a unsigned double integer (UDINT) for the counter value. |
CTU_ULINT | CTU(CU:=IncrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Increments its count value on the rising edge of its input. Uses a unsigned long integer (ULINT) for the counter value. |
CTD | CTD(CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Decrements its count value on the rising edge of its input. |
CTD_DINT | CTD(CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Decrements its count value on the rising edge of its input. Uses a double integer (DINT) for the counter value. |
CTD_LINT | CTD(CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Decrements its count value on the rising edge of its input. Uses a long integer (LINT) for the counter value. |
CTD_UDINT | CTD(CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Decrements its count value on the rising edge of its input. Uses a unsigned double integer (UDINT) for the counter value. |
CTD_ULINT | CTD(CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Decrements its count value on the rising edge of its input. Uses a unsigned long integer (ULINT) for the counter value. |
CTUD | CTUD(CU:=IncrementSignal, CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Can count both up and down, controlled by separate inputs for each direction. |
CTUD_DINT | CTUD(CU:=IncrementSignal, CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Can count both up and down, controlled by separate inputs for each direction. Uses a double integer (DINT) for the counter value. |
CTUD_LINT | CTUD(CU:=IncrementSignal, CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Can count both up and down, controlled by separate inputs for each direction. Uses a long integer (LINT) for the counter value. |
CTUD_ULINT | CTUD(CU:=IncrementSignal, CD:=DecrementSignal, PV:=10, Q=>OutputEnable, CV=>CurrentCount); | Can count both up and down, controlled by separate inputs for each direction. Uses a unsigned long integer (ULINT) for the counter value. |
Timer Function Blocks
Instruction | Example | Description |
---|---|---|
TP | TP(IN:=StartTimer, PT:=T#5s, Q=>TimerActive); | Activates its output for a specified duration when its input is TRUE, then turns off regardless of the input state. |
TON | TON(IN:=StartTimer, PT:=T#5s, Q=>TimerActive); | Begins timing when its input is TRUE, and after the specified delay, turns its output TRUE. |
TOF | TOF(IN:=StartTimer, PT:=T#5s, Q=>TimerActive); | Keeps its output TRUE as long as its input is TRUE, and starts timing when the input becomes FALSE. After the specified delay, it turns its output FALSE. |
Datatype Conversions
Need to add.
IF Statement
Example
ST
D := B*B - 4*A*C;
IF D < 0.0 THEN
NROOTS := 0;
ELSIF D = 0.0 THEN
NROOTS := 1;
X1 := - B/(2.0*A);
ELSE
NROOTS := 2 ;
X1 := (- B + SQRT(D))/(2.0*A);
X2 := (- B - SQRT(D))/(2.0*A);
END_IF ;
CASE Statement
Example
ST
TW := BCD_TO_INT(THUMBWHEEL);
TW_ERROR := 0;
CASE TW OF
1,5: DISPLAY := OVEN_TEMP;
2: DISPLAY := MOTOR_SPEED;
3: DISPLAY := GROSS - TARE; 4,6..10: DISPLAY := STATUS(TW - 4);
ELSE DISPLAY := 0 ;
TW_ERROR := 1;
END_CASE;
QW100 := INT_TO_BCD(DISPLAY);
FOR Loops
Example
ST
J := 101;
FOR I := 1 TO 100 BY 2 DO
IF WORDS[I] = 'KEY' THEN
J := I;
EXIT;
END_IF;
END_FOR;
WHILE Loops
Example
ST
J := 1;
WHILE J <= 100 & WORDS[J] <> 'KEY' DO
J := J+2;
END_WHILE;
REPEAT Loops
Example
ST
J := -1;
REPEAT
J := J+2;
UNTIL J = 101 OR WORDS[J] = 'KEY'
END_REPEAT;
RETURN statement
Example
ST
RETURN;
EXIT statement
Example
ST
EXIT;
EMPTY statement
ST
;