Structure Text Programming - Codesys 8
Structure Text Programming - Codesys 8
Consider the program in Figure 276 to find the average of five values in a real array ’f[]’. The
FOR loop in the example will loop five times adding the array values. After that the sum is divided to
get the average.
avg := 0;
FOR (i := 0 TO 4) DO
avg := avg + f[i];
END_FOR;
avg := avg / 5;
The previous example is implemented with a WHILE loop in Figure 277. The main differences
is that the initial value and update for ’i’ must be done manually.
avg := 0;
i := 0;
WHILE (i < 5) DO
avg := avg + f[i];
i := i + 1;
END_WHILE;
avg := avg / 5;
The example in Figure 278 shows the use of an IF statement. The example begins with a timer.
These are handled slightly differently in ST programs. In this case if ’b’ is true the timer will be active,
if it is false the timer will reset. The second instruction calls ’TONR’ to update the timer. (Note: ST pro-
grams use the FBD_TIMER type, instead of the TIMER type.) The IF statement works as normal, only
one of the three cases will occur with the ELSE defining the default if the other two fail.