Structured Text Expressions and Operators
An expression consists of operands and operators. An operand can be a constant, a variable, function or function block. Operators are defined by the IEC standard. They are listed below. Here are some examples.
Operators
No. | Operation | Symbol | Precedence |
---|---|---|---|
1 | Parenthesis | (expression) |
Highest
⬇
Lowest |
2 | Function/Function block |
F/FB name (Argument list) Examples: LN(A), MAX(X,Y) |
|
3 | Exponentiation | ** | |
4 | Negation | - | |
5 | Complement | NOT | |
6 | Multiply | * | |
7 | Divide | / | |
8 | Modulo | MOD | |
9 | Add | + | |
10 | Subtract | - | |
11 | Comparison | <,>,<=,>= | |
12 | Equality | = | |
13 | Inequality | <> | |
14 | Boolean AND | & | |
15 | Boolean AND | AND | |
16 | Boolean Exclusive OR | XOR | |
17 | Boolean OR | OR |
Operator Details
The formula in the parentheses is calculated first.
Example:
iResult := (2 + 3) * 2;
iResult is 10 because ‘2 + 3’ is calculated first and multiplied by 2.
There are predefined functions and function blocks. User functions and user function blocks are also available.
Example:
rVal := 3;
rResult := 10 ** rVal;
rResult is 1000.
Note: XMC supports the exponentiation only with the REAL and LREAL data types. If you want to execute the exponentiation with the INT data type for instance, please convert the data type like this.
iVal := 3;
iResult := REAL_TO_INT(10 ** INT_TO_REAL(iVal));
iResult is 1000.
Invert the sign flag.
Example:
iVal:= 3;
iResult := - iVal;
iResult is -3.
Invert each bit in the data.
Example1:
xVal := 1;
xResult := NOT xVal;
xResult is 0.
Example2:
wVal := 2#1100001100111100;
wResult := NOT wVal;
wResult is 2#0011110011000011.
Example:
iVal1 := 6;
iVal2 := 7;
iResult := iVal1 * iVal2;
iResult is 42.
Example:
iVal1 := 56;
iVal2 := 7;
iResult := iVal1 / iVal2;
iResult is 8.
Example:
iVal1 := 34;
iVal2 := 6;
iResult := iVal1 MOD iVal2;
iResult is 4.
Example:
iVal1 := 2;
iVal2 := 5;
iResult := iVal1 + iVal2;
iResult is 7.
Example:
iVal1 := 9;
iVal2 := 3;
iResult := iVal1 - iVal2;
iResult is 6.
Compare two values and generate a Boolean value.
Example:
iVal1:= 2;
iVal1:= 3;
xResult1 := iVal1 < iVal2;
xResult2 := iVal1 > iVal2;
xResult3 := iVal1 <= iVal2;
xResult4 := iVal1 >= iVal2;
xResult1 is 1.
xResult2 is 0.
xResult3 is 1.
xResult4 is 0.
Compare two values. If they are the same, generate Boolean value 1. If not, generate Boolean value 0.
Example1:
iVal1:= 2;
iVal1:= 2;
xResult := iVal1 = iVal2;
xResult is 1.
Example2:
iVal1:= 2;
iVal1:= 3;
xResult := iVal1 = iVal2;
xResult is 0.
Compare two values. If they are not the same, generate Boolean value 1. If they are the same, generate Boolean value 0.
Example1:
iVal1:= 2;
iVal1:= 3;
xResult := iVal1 <> iVal2;
xResult is 1.
Example2:
iVal1:= 2;
iVal1:= 2;
xResult := iVal1 <> iVal2;
xResult is 0.
Evaluate two Boolean values. If the both values are 1, generate Boolean value 1. If not, generate Boolean value 0.
Example:
xResult := xVal1 & xVal2;
or
xResult := xVal1 AND xVal2;
xVal1 | xVal2 | xResult |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Evaluate two Boolean values. If these values are different, generate Boolean value 1. If not, generate Boolean value 0.
Example:
xResult := xVal1 XOR xVal2;
xVal1 | xVal2 | xResult |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Evaluate two Boolean values. If one of these values is 1, generate Boolean value 1. If not, generate Boolean value 0.
Example:
xResult := xVal1 OR xVal2;
xVal1 | xVal2 | xResult |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
LM113-2