1. Overview of PLC user program execution

PLC user program execution plays a vital role in device control. It achieves precise control of various devices by processing input signals, executing user programs, and outputting control signals.
PLC adopts centralized processing in the process of user program execution, which processes the three stages of input scanning, user program execution and output refresh in batches. In the input scanning stage, PLC reads the input signals on all input terminals in a scanning manner and stores them in the input image area. For example, in digital input/output processing, when digital input is usually used to receive switch signals, such as limit switches, buttons, etc., these signals are read and stored in the input scanning stage to provide a data basis for subsequent program execution.
In the stage of executing user programs, PLC scans and executes one by one from top to bottom and from left to right in the order of ladder diagram programs. For example, during program execution, PLC takes out the current state of input variables from the input image area, performs logical operations or other operations, and then stores the operation results in the corresponding internal relays. This process is like an orderly production line, operating according to the established procedures and sequence.
In the output refresh phase, the PLC sends the output variables in the output image area to the output latch, and then the latch generates the control output of this cycle through the output module. For example, when the state of the output relay is "1", the output relay contact is closed, driving the corresponding output device to work.
This centralized processing feature enables the PLC to efficiently process a large number of input and output signals, and ensures the stability and reliability of the system. At the same time, the working mode of different stages also enables the PLC to respond to changes in input signals in a timely manner, thereby achieving precise control of the equipment.

2. Input Scanning Phase

(I) Reading and Storing Input Signals

In the input scanning phase, the PLC scans and reads the input signals on all input terminals in an efficient and orderly manner. This process is like a careful observer who does not miss any details. When the PLC reads the input signals, it will store them in the input image area, and the input image memory will be refreshed. In the program execution phase and the output refresh phase, the input image memory is like an independent fortress, isolated from the outside world, and its content remains unchanged. This ensures that during the execution of the program, the PLC always uses the data input into the image area during this sampling, rather than directly using the input signal at the time of the scene. This method provides a reliable data basis for the stable execution of the program.

(II) The influence of input signals on internal relays

The state of the input device has a direct impact on the internal input relay. If the input device can form a closed loop at the PLC input terminal, then the internal input relay corresponding to the input terminal number is saved as "1", which is equivalent to the relay coil being turned on. During the execution of the program, the contact corresponding to the number will act. On the contrary, if the input device makes the input open circuit, the internal input relay is saved as "0", which is equivalent to the relay coil not being turned on, and the corresponding contact will not act. This clear correspondence enables the PLC to accurately control the execution path of the program according to the input signal.

(III) Collection of short pulse signals

PLC often fails to collect input signals of less than ten milliseconds. This is because the input scan of PLC has a certain cycle. If the duration of the input signal is very short, such as less than one scan cycle, then the signal may not be captured during the input scan stage. Specifically, when the input signal appears, if it is not in the input scan stage, then the signal cannot be read in. Only when the duration of the input signal is greater than one scan cycle can it be ensured that it will be collected by PLC during the input scan stage. For example, some fast pulse signals, if their width is less than the scan cycle of PLC, are likely to be missed, thus affecting the system's response to certain rapidly changing input states.

3. Execution phase

(I) Scan and execute in sequence

PLC strictly follows the ladder diagram program sequence from top to bottom and from left to right to scan and execute one by one in the execution phase. In this process, it is like a rigorous executor, completing each instruction step by step. If you encounter a jump instruction, for example, in the S7-1200 PLC, there are logical result jump instructions, jump instructions that define jump lists JMP_LIST, and jump branch instructions SWITCH. When the result of the logic operation is 1, the jump instruction JMP is executed. When the result of the logic operation is 0, the jump instruction JMPN is executed. The label instruction used in conjunction is LABLE. The label instruction is marked with a label number, which can be a number, letter or Chinese character. The JMP_LIST instruction can define multiple conditional jumps, and the program that jumps to the label segment specified by the value of the K pin continues to execute. It can be increased to 32 pins at most. The SWITCH instruction defines the jump at multiple program labels to be executed based on the results of one or more comparison instructions. When jumping, the CPU no longer scans and executes the program between the jump instruction and the label instruction.

(II) Operation and storage results

During the execution phase, the PLC takes out the current state of the input variable from the input image area for operation. For example, in the Siemens 200PLC four arithmetic instruction program, the four arithmetic instructions of the S7-200/300/400 PLC are mainly to perform addition, subtraction, multiplication and division operations on two operands. The operands can be integers, double integers, floating point numbers, etc. The PLC stores the operation results in the corresponding internal relays to provide data support for subsequent program execution and output control. Just like a data processing center, it continuously processes and stores the input data.

(III) Influence of logical results

In the same scan cycle, the previous and next logical results may affect each other. For example, in the double coil problem often encountered by PLC beginners, if the coil of the same component is used twice or more and executed in the same scan cycle, an output abnormality problem will occur. Take pressing different input buttons to control the same output Q0.0 as an example. When I0.0 is 1, the logical operation result of Q0.0 in the first line is 1, but when the program scans down to the line I0.1, because I0.1 is 0, the logical operation result of Q0.0 is refreshed to 0 again. At this time, the latest result will overwrite the previous result. This situation will affect the contact state of the next scan cycle. If the logic operation result of the last coil determines the state of Q0.0, then at the beginning of the next scan cycle, the initial state of Q0.0 will be affected by this result.

4. Output refresh phase

(I) Output of storage results
In the output refresh phase, the operation results of the program execution phase are sent from the output image area to the output latch. This process is like sending a carefully prepared gift to the recipient. PLC is like a precise courier, ensuring that the operation results are accurately delivered to the output latch. Take an actual industrial control system as an example. Suppose that the PLC controls an automated production line. In the program execution phase, complex logical operations and data processing are performed based on the input signals of various sensors to determine the control signals of each output device. In the output refresh phase, these control signals are sent to the output latch, and then the latch generates the control output of this cycle through the output module, such as starting the motor, opening the valve, etc. In this way, the PLC achieves precise control of the production line.

(II) Output device status retention

The state of the internal output relay directly determines the working state of the output device. When the state of the internal output relay is "1", the output relay contacts are closed, driving the corresponding output device to work; when the state of the internal output relay is "0", the output relay contacts are disconnected, and the output device stops working. The state of the output device must be maintained for one scan cycle because the PLC works in a sequential scan and continuous cycle. In one scan cycle, the output variable in the output image area is sent to the output latch during the output refresh phase, and then the output latch controls the state of the output device. In this scan cycle, the state of the output device remains unchanged until the output refresh phase of the next scan cycle, when the state of the output device is updated according to the new calculation result. For example, in an automated warehousing system, the PLC controls the operation of the stacker. If in one scan cycle, the PLC determines that the stacker needs to move to the left, then in this scan cycle, the PLC will update the state of the output device according to the new calculation result.During the cycle, the output device (motor controller) will control the stacker to move to the left until the output refresh phase of the next scan cycle, and the next action of the stacker will be determined based on the new calculation result. This way of maintaining the state of the output device for a scan cycle ensures the stability and reliability of the system, and also enables the PLC to respond promptly to changes in the input signal.