Define the Number of Instructions for a Logic Program
You can use the Instruction Limit setting to define the maximum number of instructions permitted for a Logic program. The program will be aborted if this number of instructions is reached before the program has been executed. This means that if a program contains code errors that can cause an ‘infinite loop’, the Instruction Limit will stop the program from using all of the CPU resources.
Each program has a number of instructions. The instructions are referred to as op codes. You can see the op codes for a single execution of an ST program by using the Compile with Diagnostics feature (see Compiling an ST Program or ST Library).
When a program is executed, the system performs each instruction in turn. This continues until each of the instructions in the program have been executed or the Instruction Limit has been reached.
Normally, a program runs and has its instructions executed without affecting system performance. This is because the default setting for the Instruction Limit is 10000, and it would take a very large program to require this many instructions.
However, if there is an infinite loop in the program, the program will not be able to complete. The system continues to perform each instruction in turn, but as the code or diagram structure is badly constructed, the list of instructions cannot be completed. This means that eventually the Instruction Limit is reached and the program is aborted.
The Instruction Limit feature helps to prevent programs that contain code errors from using all of the CPU resources.
Example of a Program with an Infinite Loop Logic Error:
The following code is incorrect and contains a Logic error that causes an infinite loop:
PROGRAM NAME
VAR
i:INT := 20;
Count : INT;
END_VAR
WHILE i < 50 DO
Count := COUNT +1;
END_WHILE;
END_PROGRAM
In the example, the WHILE statement is used to increase the Count value by +1 when the i value is less than 50. As the i value is defined as 20, it is less than 50 and so the count will increase by 1—this is an infinite loop as the program cannot end. The system would continue to attempt to execute this program until the Instruction Limit was reached, at which point it would abort the program.
To identify that the code contains a Logic error, you could examine the Logic Execution Status List (a Query list). The list contains a Number of Instructions entry which shows the number of instructions on the last execution of the program. If this number is approaching the Instruction Limit or exceeds it, there may be a Logic error and you will need to examine the code for an infinite loop. Of course, it may be that your program is very large and has intentional loops and a very high number of instructions. If this is the case, you may need to alter the Instruction Limit setting.
To change the Instruction Limit setting:
- Display the configuration Form for the relevant program (see Configure an ST Program, Ladder Diagram, SFC or FBD).
- Select the Logic tab.
- Enter the required maximum number of instructions per program in the Instruction Limit field.
To configure the other Logic tab settings, see Configure an ST Program, Ladder Diagram, SFC or FBD.