1. Main program and subroutines

1. Main program and subroutines

The control program of our software consists of the following types of Program Organizing Units (POUs):

Name Description
Main Program The main body of the program (referred to as Main), in which instructions are placed to control the application program. The instructions in the main program are executed in sequence, once per CPU scan cycle.
Subroutine Subroutines are optional instruction sets located in separate program blocks that are only executed when called.
Interrupt program Interrupt program is an optional instruction set located in a separate program block, which is executed only when an interrupt event occurs.

our software provides a program editor and organizes programs by providing separate tabs for each POU in the program editor window. The main program is always the first tab, followed by any subroutines or interrupt programs that may have been created.

lable sequence

图1 lable sequence

1.1. Subroutine

Subroutines are very useful when repeating a certain function. Logic can be written once in a subroutine, and then called multiple times as needed in the main program. There are several advantages to doing this:

  • Your overall code size has decreased.

  • Subroutines are easy to transplant; You can select a feature separately and copy it to other programs without making any modifications or with only minor modifications.

    • Explanation**

The use of memory limits the portability of subroutines, as the memory address assignment of one program may conflict with the assignment in another program. On the contrary, subroutines that only use parameters and local variables are easily portable because there is no need to worry about addressing conflicts

1.2. Interrupt program

Interrupt programs can be written to handle certain predefined interrupt events: interrupt programs are not called by the program (CALL), but only need to be bound to interrupt events once (ATC); Called by PLC when an interrupt event occurs. Because it is impossible to predict when the system will call interrupts, do not program interrupt programs to write to memory that may be used elsewhere in the program. Local variables and program parameters can be used in interrupt programs, and in cases where program parameters are used, address mapping is completed within the ATC instruction.

Subroutines and interrupt programs are not distinguished within the software, but are distinguished in their usage methods:

Subroutines are called by calling the subroutine command (CALL).

Interrupt programs are bound to an interrupt event using the Interrupt Event Binding Command (ATC).