Interrupt functionality is frequently used in PLC programs. An interrupt is equivalent to a subroutine, and a subroutine is equivalent to a main program. In reality, interrupts in a PLC are rarely used. For example, pulse processing can achieve its function without interrupts, using only a 1ms timer, but it is not as accurate as interrupt-based timing. After all, without interrupts, the program scan is constrained by the scan cycle.
However, some programs require interrupts, a typical example being PID control, where the detection and reading cycle must be precise. Whether it's writing your own PID interrupt program in the past or using an integrated PID interrupt block now, precise timing control is used to process interrupted information immediately.
In this article, we'll discuss how to use the PLC interrupt function.
To understand the interrupt function of a PLC, one must first understand the PLC's operation cycle or scan cycle. It's necessary to explain the PLC's sequential control cycle execution flow, which is a prerequisite for understanding interrupts and must be mastered. This flow is divided into three parts: input processing, program processing, and output processing.
1. Input processing: Before executing the program, the programmable controller reads the ON/OFF status of all input terminals into the input image area. Even if the input changes during program execution, the content of the input image area will not change. The change is read when the input processing of the next cycle is executed.
2. Program processing: The PLC reads the ON/OFF status of each soft element from the input image area and the image areas of other soft elements according to the instructions in the program memory. Then, it starts the calculation sequentially from step 0, and writes the result of each step into the image area. Therefore, the image areas of each soft element gradually change their contents as the program executes. In addition, the internal contacts of the output relays perform actions according to the contents of the output image area.
3. Output processing: After all instructions are executed, the ON/OFF status in the output Y image area will be transferred to the output latch memory, which will then serve as the actual output of the programmable controller.
The time required to execute one action is called the operation cycle or scan time. So what is the relationship between interrupts and scan cycles? Interrupts are not executed in a complete order from top to bottom. Instead, interrupt routines run independently and are processed immediately without participating in the entire cycle operation.
What is the purpose of interrupts? We know that the PLC scan cycle is very short, making it difficult to observe the sequential execution process. Let's assume a scan cycle of 10 seconds, meaning the entire program takes 10 seconds to execute. Consider a simple program: LDX0, OUTY0. According to the diagram, when X0 is ON, Y0 doesn't output immediately; it outputs after 10 seconds. Similarly, when X0 is OFF, Y0 doesn't immediately close either; both require the scan to complete. In practical applications, this would render the PLC unusable and the device unable to operate. This is where interrupt handling comes in. Input interrupts allow for immediate execution of inputs. While the PLC's computation time is typically only a few milliseconds, sufficient for most needs, some operations, such as high-frequency pulse inputs and pulse capture, which take microseconds, will inevitably be affected by the cycle.
Therefore, if many ON/OFF state processes need to be completed within a single cycle, the interrupt function must be used. In general, interrupts are rarely used. Even high-speed PLC instructions already have interrupt functionality, eliminating the need to write dedicated interrupt subroutines. For example, a high-speed counter uses an interrupt method to process high-speed pulses from a specific input relay, allowing counting to continue regardless of the computation time. The counting result is either immediately output using a dedicated comparison instruction for the high-speed counter, or the high-speed counter's count value is processed using a specific program executed via an interrupt.
Instead of using a high-speed counter, we use an interrupt function to count high-speed inputs for counting high-frequency pulses. For example, the main program runs from EI to FEND, and the interrupt subroutine runs from pointer I101 (the interrupt pointer for input X0) to IRET. Whenever a rising edge of X1 is detected, the subroutine is executed to immediately transfer the value to D10, thus avoiding the influence of the PLC's operation cycle.
Disclaimer: This article is a reprint. If it involves copyright issues, please contact us promptly for deletion (QQ: 2737591964). We apologize for any inconvenience.