Structured Text Statements
The XMC Motion Controller supports the following statements that are defined in the standard.
No. | Statement type/Reference | Examples |
---|---|---|
1 | Assignment |
A := B; CV := CV + 1; C := SIN(X); |
2 | Function block invocation and function block output usage | CMD_TMR(IN:=%IX0.0.5, PT:=T#300ms, RST:= %IX0.0.6); A := CMD_TMR.Q; |
3 | RETURN | RETURN; |
4 | IF |
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; |
5 | CASE |
TW := WORD_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); |
6 | FOR |
J := 101; FOR I := 1 TO 100 BY 2 DO IF WORDS[I] = 'KEY' THEN J := I; EXIT; END_IF; END_FOR; |
7 | WHILE |
J := 1; WHILE J <= 100 & WORDS[J] <> 'KEY’ DO J := J+2; END_WHILE; |
8 | REPEAT |
J := -1; REPEAT J := J+2; UNTIL J = 101 OR WORDS[J] = 'KEY’ END_REPEAT; |
9 |
EXIT (The EXIT statement works with the FOR, WHILE, and REPEAT statements) |
EXIT; |
10 | Empty Statement | ; |
The XMC Motion Controller also supports the following statements:
No. | Statement type/Reference | Examples |
---|---|---|
1 | End of program |
END_PROGRAM; |
2 | Subroutine |
CALL Subroutine1; END_PROGRAM; SBRT Subroutine1 iVal1:=iVal1+1; END_SBRT; |
The assignment operator ‘:=‘ is used to assign an operand, variable or the evaluation result of an expression to a variable.
(1) Function
Functions can be used in the Structured Text programs directly. The following example shows the ABS function (Absolute value operation) is used directly with an integer type variable.
iResult := ABS(iVal1);
(2) Function block
To use a function block, the function block needs to be invoked and a variable is assigned as the instance. In the following example, the TMR function block (Integration Timer) is invoked and the variable INST_TMR is assigned as the instance.
INST_TMR(IN:=%IX0.0.0, PT:=T#5s, RST:=%IX0.0.1);
When a function block is invoked and a variable is assigned, the predefined member variables for the function block are assigned to the variable also. To access to those member variables, use the format ‘<Variable name>.<Member variable name> ‘ like the following example.
The IF and CASE statements are selection statements.
The following is the basic format of the IF statement:
IF <Condition> THEN
<Statements>;
END_IF;
If <Condition> is true, <Statements> is executed.
The IF statement supports ‘ELSE’ like this.
IF <Condition> THEN
<Statements1>;
ELSE
<Statements2>;
END_IF;
-
If <Condition> is true, <Statements1> is executed.
-
If <Condition> is false, <Statements2> is executed.
The IF statement also supports ELSIF like this:
IF <Condition1> THEN
<Statements1>;
ELSIF <Condition2> THEN
<Statements2>;
ELSE
<Statements3>;
END_IF;
-
If <Condition1> is true, <Statements1> is executed.
-
If <Condition1> is false and <Condition2> is true , <Statements2> is executed.
-
If both <Condition1> and <Condition2> are false, <Statements3> is executed.
The following is the format of the CASE statement:
CASE <Expression> OF
<Case list> : <Case statements>
ELSE
<Statements>
END_CASE;
When <Expression> is evaluated, an INT type number must be generated as the result. <Case list> has the list of numbers that are compared with the result of the <Expression> evaluation. If there is a matched number in the <Case list>, <Case statements> assigned to the number is executed. If all numbers in the <Case list> don’t match, <Statements> is executed.
Example:
CASE iVal1 OF
1, 5: iVal2 := 10;
2: iVal2 := 20;
3: iVal2 := 30;
4, 6..10: iVal2 := 40;
ELSE
iVal2 := 50;
END_CASE;
-
If the value of iVal1 is 1 or 5, 10 is set to iVal2.
-
If the value of iVal1 is 2, 20 is set to iVal2.
-
If the value of iVal1 is 3, 30 is set to iVal2.
-
If the value of iVal1 is 4, 6, 7, 8, 9 or 10, 40 is set to iVal2.
-
If the value of iVal1 is less than 1 or more than 10, 50 is set to iVal2.
The FOR, WHILE and REPEAT statements are iteration statements. The EXIT statement can be used with them also.
The following is the format of the FOR statement:
FOR <Counter> := <Start value> TO <End value> BY <Step value> DO
<Statements>
END_FOR;
<Counter> is an integer type variable. <Start value> is set as the initial value of the counter. If the counter value is lower than or equal to <End value>, <Statements> is executed. The counter keeps increasing by <Step value> and <Statements> is executed until the counter value becomes higher than <End value>.
Note: <Step value> must be a positive number with the XMC.
Example:
iVal2 := 0;
FOR iVal1 := 1 TO 10 BY 1 DO
iVal2 := iVal2 + iVal1;
END_FOR;
-
iVa2 is reset to zero before executing the FOR statement.
-
iVal1 is the counter for this FOR statement and it is set to 1 as the start value.
-
Because the start value is less than the end value (10 in this example), ‘iVal2 := iVal2 + iVal1’ is executed.
-
Then iVal1 keeps increasing by 1 until it exceeds 10 and execute ‘iVal2 := iVal2 + iVal1’.
-
After executing this FOR statement, iVal2 has 55 because the counter values 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 are added to iVal2.
The following is the format of the WHILE statement.
WHILE <Condition> DO
<Statements>
END_WHILE;
<Condition> needs to be a Boolean value. While <Condition> is true, <Statements> is executed. If <Condition> is false before this WHILE statement is executed, <Statements> is not executed.
Example:
iVal1 := 0;
WHILE iVal1 < 10 DO
iVal1 := iVal1 + 1;
END_WHILE;
-
iVa1 is reset to zero before executing the WHILE statement.
-
While iVal1 is less than 10, ‘iVal1 := iVal1 + 1‘ is executed.
-
When iVal1 is 9, ‘iVal1 := iVal1 + 1‘ is executed one more time and this WHILE statement is completed because iVal1 is 10.
The following is the format of the REPEAT statement:
REPEAT
<Statements>
UNTIL <Condition>
END_REPEAT;
<Condition> needs to be a Boolean value. <Statements> is executed until <Condition> becomes true, . If <Condition> is true before this REPEAT statement is executed, <Statements> is executed once.
Example:
iVal1 := 0;
REPEAT
iVal1 := iVal1 + 1;
UNTIL iVal1 > 10
END_REPEAT;
-
iVa1 is reset to zero before executing the REPEAT statement.
-
‘iVal1 := iVal1 + 1‘ is executed until iVal1 exceeds 10.
-
After this REPEAT statement is completed, iVal1 is 11.
The EXIT statement can be used in the loop of the FOR, WHILE and REPEAT statements. After the EXIT statement is executes, these FOR, WHILE and REPEAT statements are terminated immediately.
Example:
iVal2 := 0;
FOR iVal1 := 1 TO 10 BY 1 DO
iVal2 := iVal2 + iVal1;
IF iVal2 > 10 THEN
EXIT;
END_IF;
END_FOR;
-
iVa2 is reset to zero before executing the FOR statement.
-
iVal1 is the counter for this FOR statement and it is set to 1 as the start value.
-
Because the start value is less than the end value (This is 10 in this example), ‘iVal2 := iVal2 + iVal1’ is executed.
-
Then iVal1 keeps increasing by 1 and execute ‘iVal2 := iVal2 + iVal1’.
-
When iVal2 exceeds 10, execute the EXIT statement to terminate this FOR statement.
-
After executing this sample program, iVal1 is 5 and iVal2 is 15. This means the counter values 1, 2, 3, 4 and 5 are added to iVal2.
(1) End of program
The END_PROGRAM statement is used to indicate the end of scan program in the Structured Text program. The XMC doesn’t execute the program written after this statement during the regular program scan.
Example:
END_PROGRAM;
Note: The XMC doesn’t support the PROGRAM statement.
(2) Subroutine
Subroutine programs can be added after the END_PROGRAM statement. The CALL statement is used to call those subroutines.
CALL <Subroutine name>;
END_PROGRAM;
SBRT <Subroutine name>
<Statements>;
END_SBRT;
LM113-3