Build note
Interrupts: two ways to let hardware call your code
Polling is walking to the door every few seconds to see whether anyone is there. An interrupt is a doorbell. You tell the hardware what condition matters, go back to your own work, and the CPU jumps to your function when it happens.
The chip has two separate units that can ring that bell, and they are not the same hardware.
The rules an ISR must follow
Before the wiring, four rules:
- Keep it short. Other interrupts are blocked while it runs unless you re-enable them.
- Do not call
_delay_ms(). Delays depend on a timer interrupt, which may be blocked inside the ISR. - Mark shared variables
volatile. Otherwise the compiler may cache the value in a register and never notice the ISR changed it. - Do the minimum, then return. Set a flag or a pin, and let the main loop do the slow work.
External interrupt on INT0
This unit watches two specific pins and lets you choose the edge.
void initInterrupt(void)
{
EIMSK |= (1 << INT0); // enable INT0
EICRA |= (1 << ISC00); // ISC01:0 = 01, any logical change
sei(); // global enable
}
| Register | Page | Job |
|---|---|---|
EICRA |
54 | Sense control for INT0 and INT1 |
EIMSK |
55 | Enable each external interrupt |
SREG |
11 | Global interrupt enable, bit I |
ISC01:0 gives four choices: low level, any change, falling edge, rising edge.
Setting only ISC00 selects any change, so a press and a release both trigger
the routine, and the routine reads the pin again to decide which happened.
The button only needs to connect to ground because the pull-up is switched on
with PORTD |= (1 << PD2). That is the register that controls the pull-up when
the pin is an input.
Pin-change interrupt on the same pin
The same button can also be watched by the pin-change unit:
PCICR |= (1 << PCIE2); // enable group 2 (Port D)
PCMSK2 |= (1 << PCINT18); // watch PD2
sei();
The ISR is PCINT2_vect, not INT0_vect. Every pin in the group shares that
one vector, so the routine has to work out which pin changed.
| External unit | Pin-change unit | |
|---|---|---|
| Pins | INT0, INT1 only | Every pin, in three groups |
| Edge choice | Four choices | None |
| Vectors | One per pin | One per port |
| Cost | Simple ISR | Shared vector, must identify the pin |
The external unit gives you control and a dedicated vector. The pin-change unit covers pins the other one cannot reach, at the cost of a busier ISR.
The mistake that costs the most time
Forgetting sei(). You can configure EIMSK, EICRA and PCMSK2 perfectly and
still see nothing, because the global enable bit is off. If a button does
nothing, check sei() before you touch the wiring.
The second mistake is enabling the pull-up with DDRD instead of PORTD.
DDRD makes the pin an output, and then the button fights the output driver.
What the example shows
Two LEDs. One on PB3 blinks from the main loop, so you can see that normal code
keeps running. The other on PB1 changes only when you press the button, so you
can tell the interrupt fired.
That second LED is also how you discover contact bounce. A mechanical button does not make one clean edge; it makes several. An any-change interrupt will happily fire on every one of them. The fix is debouncing, either with a capacitor or by ignoring further edges for a few milliseconds inside the ISR.
How this was made
- Written by: Rupesh, from his own
c-04-isr-externalandc-04-isr-pin-changenotes and code, with AI drafting and editing. - Register details: from the ATmega328P datasheet.
- AI-assisted sections: structure and copy editing.