Why Are Interrupts Better Than Polling?
In embedded systems, peripherals constantly generate events.
A UART receives a byte.
A timer expires.
A GPIO pin changes state.
An ADC conversion completes.
The processor needs to respond to these events.
There are two common approaches:
- Polling
- Interrupts
At first, polling looks simple. But interrupts usually provide a more efficient way for the CPU to respond to asynchronous events.
What Is Polling?
In polling, the CPU repeatedly checks whether an event has occurred.
For example:
while (1)
{
if (UART_RX_READY)
{
read_uart_data();
}
}
The processor keeps asking the UART:
"Did data arrive?"
If no data has arrived, the CPU checks again.
And again.
And again.
Imagine UART data arrives only once every 100 ms.
During those 100 ms, the CPU may check the same status flag thousands of times even though nothing has happened.
That CPU time could have been used for something else.
What Changes With Interrupts?
With interrupts, the CPU does not continuously check the peripheral.
Instead, the peripheral informs the CPU when an event occurs.
For example:
CPU executing main application
|
|
UART receives data
|
v
UART generates interrupt
|
v
CPU pauses current work
|
v
UART ISR executes
|
v
CPU resumes previous work
The CPU can spend most of its time doing useful work.
Only when the UART actually receives data does the CPU handle it.
A Simple Analogy
Imagine waiting for a food delivery.
Polling
You keep opening the door every 10 seconds.
Check door
↓
Nobody there
Check again
↓
Nobody there
Check again
↓
Nobody there
This wastes your time.
Interrupt
Instead, the delivery person rings the doorbell.
You continue doing something else until:
Doorbell rings
↓
You respond
The doorbell is similar to an interrupt.
The event tells you when your attention is required.
Why Are Interrupts More Efficient?
1. Better CPU Utilization
With polling, the CPU spends time repeatedly checking status registers.
With interrupts, the CPU can execute:
Application code
Communication tasks
Control algorithms
Background processing
until an event occurs.
2. Lower Power Consumption
This becomes especially important in battery-powered systems.
With polling:
CPU awake
↓
Check peripheral
↓
Check peripheral
↓
Check peripheral
The CPU remains active.
With interrupts:
CPU enters sleep
↓
Peripheral event occurs
↓
Interrupt wakes CPU
↓
CPU processes event
The processor can remain in a low-power state until something actually requires attention.
3. Faster Response to Asynchronous Events
Some events can happen at unpredictable times.
Examples include:
- UART receive
- GPIO input
- sensor data ready
- external fault signal
- timer expiration
Waiting for the next polling cycle introduces delay.
For example:
CPU polls GPIO
|
|---- does other processing ----|
|
GPIO changes here
|
|---- waiting ----|
|
Next GPIO poll
The event may not be detected immediately.
With an interrupt:
GPIO changes
↓
Interrupt generated
↓
CPU responds
The response can occur much sooner.
But Interrupts Are Not Free
An interrupt also introduces overhead.
When an interrupt occurs, the processor typically needs to:
Pause current execution
↓
Save execution context
↓
Find the ISR
↓
Execute ISR
↓
Restore context
↓
Resume previous execution
So if an event occurs extremely frequently, generating an interrupt for every event may become inefficient.
This is sometimes called an interrupt storm.
When Can Polling Be Better?
Polling is not inherently bad.
It can be useful when:
- the event occurs very frequently
- the CPU has nothing else important to do
- deterministic timing is required
- the operation takes only a few cycles
- interrupt overhead would be larger than the polling cost
For example, firmware may briefly poll a hardware status bit:
while (!(STATUS_REG & READY_BIT))
{
}
If the peripheral becomes ready within only a few microseconds, setting up an interrupt may not provide much benefit.
Interrupts vs Polling
| Polling | Interrupt |
|---|---|
| CPU repeatedly checks the peripheral | Peripheral alerts the CPU |
| CPU time may be wasted | CPU can perform other work |
| Often keeps CPU active | CPU can sleep |
| Response depends on polling interval | Response occurs when event is raised |
| Simple implementation | Requires ISR and interrupt configuration |
| Useful for very frequent/simple events | Useful for asynchronous events |
A Real Embedded-System Example
Suppose a microcontroller receives UART data once every second.
Polling approach
CPU
|
| Check UART
| Check UART
| Check UART
| Check UART
| Check UART
|
| DATA ARRIVES
|
| Read UART
Most checks returned nothing.
Interrupt approach
CPU runs application
|
|
|
UART data arrives
|
v
Interrupt
|
v
Read UART
|
v
Resume application
The processor responds only when necessary.
The Key Idea
Polling asks:
"Did something happen?"
over and over again.
Interrupts say:
"Tell me when something happens."
That simple difference allows embedded systems to use CPU time and power much more efficiently.
One Important Design Rule
Interrupts are most effective when the ISR remains short.
A typical design is:
Interrupt occurs
↓
ISR captures the event/data
↓
ISR sets a flag / sends an event
↓
ISR exits quickly
↓
Main task performs the heavier processing
Comments
Post a Comment
Thanks for reading through the Embedded Systems World.
Feel free to ask questions or share your thoughts on the topic.