0% found this document useful (0 votes)
3 views

Structure Text Programming - Codesys 8

The document provides examples of programming constructs in a structured text (ST) environment, including the use of FOR and WHILE loops to calculate the average of five values in an array. It also illustrates the use of an IF statement to manage a timer based on a boolean condition. Key instructions such as RETAIN, IIN, and EXIT are mentioned as special commands in the programming context.

Uploaded by

edlistianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Structure Text Programming - Codesys 8

The document provides examples of programming constructs in a structured text (ST) environment, including the use of FOR and WHILE loops to calculate the average of five values in an array. It also illustrates the use of an IF statement to manage a timer based on a boolean condition. Key instructions such as RETAIN, IIN, and EXIT are mentioned as special commands in the programming context.

Uploaded by

edlistianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

336

RETAIN(); causes a bit to be retentive


IIN(); immediate input update
EXIT; will quit a FOR or WHILE loop
EMPTY

Figure 275 Special Instructions

19.2.2 Putting Things Together in a Program

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;

Figure 276 A Program To Average Five Values In Memory With A For-Loop

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;

Figure 277 A Program To Average Five Values In Memory With A While-Loop

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.

You might also like