0
0
Power-electronicsComparisonBeginner · 4 min read

Polling vs Interrupt in Embedded C: Key Differences and Usage

In embedded C, polling continuously checks a device or flag in a loop to detect events, while interrupts automatically pause the main program to handle events as they occur. Polling wastes CPU time but is simple; interrupts are efficient but require more setup.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of polling and interrupts in embedded C:

FactorPollingInterrupt
CPU UsageHigh, continuously checks statusLow, CPU sleeps or runs other tasks until event
Response TimeSlower, depends on polling frequencyFast, immediate on event occurrence
ComplexitySimple to implementMore complex setup and handling
Power EfficiencyLess efficient, wastes cyclesMore efficient, saves power
Use CaseSimple or low-speed devicesTime-critical or asynchronous events
Code StructureLinear, blocking loopsEvent-driven, uses ISR (Interrupt Service Routine)
⚖️

Key Differences

Polling works by repeatedly checking a hardware flag or input in a loop. The CPU stays active, wasting time if no event occurs. This method is easy to understand and debug but inefficient for power or multitasking.

Interrupts let the hardware notify the CPU immediately when an event happens. The CPU can do other work or sleep until interrupted. Interrupts require writing an ISR (Interrupt Service Routine) and configuring hardware registers, making the code more complex but responsive and power-friendly.

In summary, polling is like waiting at a door and constantly looking outside, while interrupts are like having a doorbell that rings only when someone arrives.

⚖️

Code Comparison

This example shows polling to detect a button press connected to a microcontroller pin.

embedded_c
#include <stdint.h>
#include <stdbool.h>

#define BUTTON_PIN (*(volatile uint8_t*)0x20) // Example input register

int main() {
    while (1) {
        if (BUTTON_PIN & 0x01) { // Check if button pressed (bit 0)
            // Button pressed action
            // For example, toggle an LED or send a signal
        }
        // Other code can run here but CPU is busy looping
    }
    return 0;
}
Output
No direct output; continuously checks button state in a loop.
↔️

Interrupt Equivalent

This example uses an interrupt to detect the same button press, triggering an ISR.

embedded_c
#include <stdint.h>
#include <stdbool.h>

volatile bool button_pressed = false;

// Interrupt Service Routine for button press
void ISR_ButtonPress(void) {
    button_pressed = true;
    // Clear interrupt flag here if needed
}

int main() {
    // Configure button pin interrupt here

    while (1) {
        if (button_pressed) {
            button_pressed = false;
            // Handle button press event
        }
        // CPU can sleep or do other tasks
    }
    return 0;
}
Output
No direct output; ISR sets flag when button pressed, main loop handles event.
🎯

When to Use Which

Choose polling when your application is simple, timing is not critical, and you want straightforward code without complex setup. It works well for low-speed or rarely changing inputs.

Choose interrupts when you need fast response, efficient CPU usage, or your system must handle multiple tasks simultaneously. Interrupts are essential for real-time and power-sensitive embedded applications.

Key Takeaways

Polling wastes CPU time by constantly checking status, while interrupts react instantly to events.
Interrupts require more complex setup but improve responsiveness and power efficiency.
Use polling for simple, low-speed tasks and interrupts for time-critical or multitasking systems.
Interrupts use ISRs to handle events asynchronously, freeing the CPU for other work.
Choosing the right method depends on your application's timing and power needs.