Intuitive Explanation
An interrupt is an interruption of the flow of a processor’s main program by an event. Let us look at two examples from everyday life:
Imagine you are listening to a podcast on your smartphone and a call comes in. You accept the call. The call interrupts the podcast for some time, and the podcast pauses. Then you talk on the phone. After you have ended the call, the podcast automatically continues at the point where it was interrupted. In this analogy, the podcast is the main program currently running on the processor. The interruption is the call.
Let us look at another example: you are studying for electrical engineering and boiling eggs at the same time. After 6 minutes the eggs are just the way you like to eat them. When the eggs are ready, you have to briefly interrupt your studying to take the eggs out of the water. In this analogy, studying is the main program currently running on the processor. The event that the eggs are ready is the interruption.
You have two ways of reacting to external events:
1. You can keep interrupting your main activity to check whether the event has occurred. The smartphone would then have to interrupt the podcast, for example every second, and check whether someone is calling. Or you would have to interrupt your studying every 10 seconds and look at the clock to see whether the eggs are done. In computer science we call this approach “polling”. Polling describes actively checking whether an event has occurred.
2. Or you can grant a call the right to interrupt the podcast at any time when necessary. The event itself becomes active and interrupts the main process. You can set an alarm clock for 6 minutes. When the alarm rings, you interrupt your studying and take out the eggs. These would be interrupt solutions for the two scenarios.
Let us compare the two approaches: It is difficult to choose the polling interval correctly. If you check too often whether the event has occurred, the main program is interrupted unnecessarily often. If you check too rarely, you may miss the event or react too late. The caller may already have hung up if we only check every 10 seconds whether someone has tried to reach you. The time at which the external event is handled depends on the time interval between two checks. An interrupt interrupts the main program only once, at exactly the right time. In return, the interruption can happen at any time, even during the most exciting scene of the podcast. The main process cannot prepare for the interruption.
For many applications, interrupts are superior to polling. That is why it is important that you master event handling with interrupts.
After the interrupt, the main program must continue without errors exactly where it was interrupted. Apart from a time delay, it must be exactly as if there had never been an interruption. It would be annoying if the podcast started again from the beginning or continued at a different point. It would be even worse if you had to start studying again from the beginning after the interruption.
In the following chapters you will learn how interrupts are implemented in an MCU.