How does the interrupt mechanism make PLC run efficiently?

Perhaps you haven't noticed, but every time you hit a key on the keyboard or click the mouse, the CPU on the computer motherboard triggers an interrupt to process your request. Interrupts are the foundation of microcomputers' ability to handle complex signals and execute tasks in an "event-driven" manner. PLCs in industrial settings, essentially microcontrollers (MCUs), also possess the capability of interrupt handling. In today's article, we will take Siemens' S7 series PLC as an example to discuss the interrupt management mechanism of PLCs.




After all that talk, what exactly is an "interrupt"? Interrupt, also known by its English name "interrupt," refers to the process where the CPU (PLC) stops the current task it's executing and switches to execute another task. The CPU was originally busy with a task, but due to a request (interrupt request), it sets aside the current task to handle this request (interrupt handling). This process is called an "interrupt." Let me give you an example: Imagine I'm boasting to my colleague about how much I can drink, and suddenly my wife calls. Should I answer or not? (You understand the consequences of not answering.) Of course, I obediently pick up the phone and say, "Hello, my dear..."

No laughing allowed, this is a typical interrupt process: When an interrupt request (my wife's phone call) occurs, the CPU first saves the data of the currently running task's registers to the interrupt stack (saving the scene, I pause the conversation with my colleague and remember where we left off); then it jumps to the entry point of the interrupt handling function to begin executing the interrupt handling function (answering the phone); after handling is complete, it returns to the original function, retrieves the saved data from the stack, and continues executing the original program (restoring the scene from the interrupt, continuing the original conversation topic).

In simple terms, the interrupt process includes: interrupt request - save scene - handle interrupt - restore scene. The tasks of "saving the scene" and "restoring the scene" are completed by the operating system, and we only need to focus on completing the task of "interrupt handling."

In the Siemens S7 series PLC, depending on the type of interrupt, it can be divided into: time-of-date interrupt, time-delay interrupt, cyclic interrupt, hardware interrupt, asynchronous error interrupt, synchronous error interrupt. The figure below shows the configurable interrupt types displayed in the CPU317 hardware configuration:




Taking the example of the Time-of-day interrupt, its "Execution" can be set to options such as "Once," "Every Minute," "Hourly," and so forth. Once you've selected the execution method and checked the "Active" option in front, you can activate this interrupt function by downloading it to the PLC. See the image below for reference:



Once the interrupt function is activated, how does the PLC proceed to handle the interrupt?

S7 PLCs utilize specific organization blocks (OBs) to execute the tasks related to interrupt handling. For instance, OB 10 in the image above is designated for handling time-of-day interrupts. When an interrupt occurs, the operating system (PLCs all have operating systems, and the programs we write using Step7 are user programs) invokes OB 10 and executes the code contained within (user program).

For example, let's consider a task such as "calling parents every day at 7:00 PM." You can select "Daily" in the "Execution" field and set the "Start time" to "2017-2-24 19:00," as shown in the image below:


Next, we'll add "Organization Block OB10" and write "Call parents" inside (note that this is pseudo code).

So, every day at 7:00 PM, a time-of-day interrupt will be triggered. The CPU will then execute the code inside OB10, prompting you to call your parents (remember to visit home often when you have time, and if you're busy, make sure to call them).

The execution mechanism for delay interrupts, cyclic interrupts, and others is similar (although the methods of utilization may vary, we'll discuss them later).

The use of interrupt mechanisms saves PLC runtime resources and improves operational efficiency. Why is this said? We know that whether it's a PLC or a microcontroller, its program has an entry function (main function), and the code inside it is executed sequentially from top to bottom, from left to right. Without interrupts, if we want to know whether an event has occurred, we would have to continually query the status of that event. These empty loop codes would consume precious CPU runtime, reducing program efficiency. With the interrupt mechanism in place, the CPU can focus on processing other tasks. It only needs to handle interrupts when they occur, which significantly improves operational efficiency.