This document covers the configuration and usage of Pin Change Interrupt 2 (PCINT2) on the ATmega328 microcontroller. It explains how to enable interrupts for pins PD0–PD7 using PCICR and PCMSK2, how to clear the PCIF2 flag, write the ISR, and apply it in practical applications.
| Group | Pins | Port | Interrupt Vector |
|---|---|---|---|
| PCINT2 | PD0–PD7 | PORTD | PCINT2_vect |
Pin Change Interrupt 2 monitors all pins on PORTD. Any logical change (rising or falling edge) on an enabled pin will trigger the interrupt.
| Bit | Name | Description |
|---|---|---|
| 2 | PCIE2 | Enable Pin Change Interrupt for PCINT2 group |
bitSet(PCICR, PCIE2); // Enable pin change interrupt for PORTD (PCINT2 group)| Bit | Name | Pin | Description |
|---|---|---|---|
| 0 | PCINT16 | PD0 | Enable pin change interrupt for PD0 |
| 1 | PCINT17 | PD1 | Enable pin change interrupt for PD1 |
| 2 | PCINT18 | PD2 | Enable pin change interrupt for PD2 |
| 3 | PCINT19 | PD3 | Enable pin change interrupt for PD3 |
| 4 | PCINT20 | PD4 | Enable pin change interrupt for PD4 |
| 5 | PCINT21 | PD5 | Enable pin change interrupt for PD5 |
| 6 | PCINT22 | PD6 | Enable pin change interrupt for PD6 |
| 7 | PCINT23 | PD7 | Enable pin change interrupt for PD7 |
bitSet(PCMSK2, PCINT16); // Enable interrupt for PD0
bitSet(PCMSK2, PCINT17); // Enable interrupt for PD1
bitSet(PCMSK2, PCINT18); // Enable interrupt for PD2
bitSet(PCMSK2, PCINT19); // Enable interrupt for PD3
bitSet(PCMSK2, PCINT20); // Enable interrupt for PD4
bitSet(PCMSK2, PCINT21); // Enable interrupt for PD5
bitSet(PCMSK2, PCINT22); // Enable interrupt for PD6
bitSet(PCMSK2, PCINT23); // Enable interrupt for PD7You can enable multiple pins simultaneously by setting multiple bits.
PCMSK2 |= (1 << PCINT18) | (1 << PCINT20); // Enable PD2 and PD4| Bit | Name | Description |
|---|---|---|
| 2 | PCIF2 | Pin Change Interrupt Flag for PCINT2 |
intFlag_clear(PCIFR, PCIF2); // Clear PCINT2 interrupt flagNote
The flag is automatically cleared when the ISR is executed.
The ISR is triggered on any logical change of enabled pins in PORTD. You must read the current pin state and compare it to the previous state to detect rising or falling edges.
ISR(PCINT2_vect)
{
if (bitCheckHigh(PIND, 4))
{
// Rising edge on PD4
}
else
{
// Falling edge on PD4
}
}void pcint2_Init(void)
{
GPIO_Config_INPUT(DDRD, PD4); // Configure PD4 as input
bitSet(PCICR, PCIE2); // Enable PCINT2 group
bitSet(PCMSK2, PCINT20); // Enable interrupt for PD4
globalInt_Enable; // Enable global interrupts
}| Step | Register | Action |
|---|---|---|
| Configure PDx as input | DDRD | GPIO_Config_INPUT(DDRD, PDx) |
| Enable PCINTx in mask | PCMSK2 | Set PCINTx bit |
| Enable PCINT2 group | PCICR | Set PCIE2 bit |
| Enable global interrupts | SREG | Set SREG_I bit |
| Write ISR | PCINT2_vect | Detect and handle pin changes |
Caution
Pin change interrupts are edge-agnostic. You must track previous pin states to determine rising or falling edges.
If you found this repository useful:
- Subscribe to my YouTube Channel.
- Share this repository with others.
- Give this repository and my other repositories a star.
- Follow my GitHub account.
Feel free to reach out to me through any of the following platforms: