Consider the following knock-knock joke:
Figure 1 |
Joker: “Knock, knock”
Innocent Person: “Who’s there?”
Joker: “Interrupting cow”
Innocent Person: “Interrupting co…”
Joker: “Mooooo” (while the innocent person is responding)
We started this discussion of interrupts with a knock knock joke because it captures the essence of what an interrupt is in an embedded system. Consider the main loop of the program as the “Innocent Person” and the event (e.g., sensor) as the “Joker”. The Joker causes the normal routine of the knock knock joke to be broken by the event of the punchline delivery (interrupt).
When an event in a system triggers an interrupt, the program momentarily breaks from whatever it is currently doing in the main loop, executes code in an interrupt service routine, and returns to the place where it left off in the main loop. Since most microcontrollers can only handle one software task at a time, using interrupts allows multiple things to be monitored at the same time.
An interrupt service routine (abbreviated ISR) is the separate program code that is executed after an interrupt is triggered. It is similar to calling a function (except that the calling mechanism is by interrupt, rather than explicit call in the code).
Since interrupts can happen at nearly any time, there is a possibility that interrupts from two or more different sources can be generated at the same time. If the microcontroller finds pending interrupts from multiple different sources, it will use the programmer-assigned priorities for each interrupt source to determine which interrupt service routine to run first. Once the first interrupt is serviced, additional interrupts will be serviced in priority order.
An example UML activity diagram for an interrupt-based system is shown in Figure 1 below.
Figure |
For more information on UML, please see the What is Unified Modeling Languageâ„¢ (UML®)? blog post.
Interrupts are typically active in a specific region of code defined by the programmer. Before the region of code where interrupts should be active, interrupts must be enabled (typically one or more lines of code specific to the microcontroller). After the region of code where interrupts should be active, interrupts must be disabled (typically one or more lines of code specific to the microcontroller).
Based on a blog entry written by Kevin Nichols