Interrupts
On this page
You can wait for a visitor by walking to the door every few minutes to look outside. Or you can fit a doorbell and continue your work until it rings. An interrupt is the doorbell: a signal from the hardware that makes the processor pause its program, deal with the event at once, and then continue where it stopped.
See it
A program reads sensors, updates a display and sends data. One pass through all of this takes 200 ms, and it checks one input pin once in each pass. A pulse of 50 ms arrives on that pin. Does the program see it?
Each point on the lower bar is a moment when the program looks at the pin. Between two points it is busy and sees nothing. With an interrupt, the pulse itself calls a short routine, wherever the main program is. The routine is drawn wider than it is: it takes a few millionths of a second.
The idea
Checking a pin again and again from the main program is called polling. It is easy to write, and it has two costs. The program can react only as fast as it comes back to the check. And an event that starts and ends between two checks is lost.
With an interrupt, the hardware watches the pin. When the event comes, the processor finishes the instruction it is on, saves its place, and runs a short function that you wrote for that event, the interrupt service routine (ISR). Then it returns to the exact place where it stopped. The delay is a few microseconds, and it does not depend on what the main program was doing.
An interrupt is not free. Each one takes time from the main program, so events that come many thousands of times a second can leave it almost no time to run.
One worked number
A water meter gives one pulse of 50 ms for each litre. A program checks the meter pin once in each pass, and one pass takes 200 ms. Out of 100 litres, how many does the program count?
Show the worked solution, 3 steps
- Find how often the program looks
One check in each pass means one look every 200 ms. Each look lasts an instant.
- Find the share of pulses that a look falls inside
A pulse covers 50 ms of every 200 ms. Share seen = 50 ms ÷ 200 ms = 0.25, which is 25%.
- Apply it to 100 pulses
0.25 × 100 = 25 litres counted, and 75 lost. In the figure, move the arrival slider: the pulse is seen in a quarter of the positions. With an interrupt, all 100 are counted.
The mistake to avoid
A second trap: a mechanical button does not close cleanly. Its contacts bounce for a few milliseconds, and an interrupt is fast enough to count every bounce as a press.
Check yourself
A program checks a pin once in each pass of 100 ms. Pulses of 20 ms arrive at random moments. What share of them does it see?
An ISR counts pulses from a meter. Someone adds a line to it that sends a long message, which takes 50 ms. Pulses arrive every 10 ms. What happens?
A controller has three jobs. Which one needs an interrupt most?
Where you meet it
- Water, fuel and energy metersAn interrupt counts every pulse, whatever the controller is doing.
- Motor shaftsA sensor on the shaft gives hundreds of pulses in each turn, and interrupts count them to find speed and position.
- Regular measurementsA timer interrupts the program at exact intervals, so every measurement is taken at the same spacing.
- Data linksEach byte that arrives from a modem raises an interrupt, so none is lost while the program is busy.
- Polling means the program looks. An interrupt means the hardware calls.
- Polling misses an event that is shorter than the time between two checks.
- An interrupt pauses the main program, runs a short routine (the ISR), and returns.
- Keep the ISR very short: set a flag, and do the work in the main program.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.