Skip to content
You Start with one site
Amounts in Start with one site
Microcontrollers · Concept

Interrupts

One idea Beginner 5 min read
Found this useful?

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.

Always?Only if the program does nothing else and checks without stopping.Rarely true in practice
Never?Some pulses arrive at the moment of a check.Not never
SometimesOnly when a check falls inside the 50 ms.One pulse in four

See it

Predict first

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?

See it: one short pulse, and a program that is busy

Result
Delay before the program reacts
Share of 50 ms pulses seen by checking

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

The question

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
  1. Find how often the program looks

    One check in each pass means one look every 200 ms. Each look lasts an instant.

  2. 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%.

  3. 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

The ISR is not the place to do the work. While an ISR runs, the main program is stopped and other interrupts wait. An ISR that prints a message makes the whole system late, and events that come during it can be lost. Keep it to a few lines: store a number or set a flag, then return. The main program does the long work.

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

Question 1

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?

Question 2

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?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.