'External interrupt for AVR is not working

I would like to learn how interrupt works with AVR. I have built up a simple button and LED circuit. It works with the interrupt Arduino library, but does not work with my own code. That means there is no hardware problem. I enabled the interrupt INT0 upon any logical change. Anyone can help?

#include <avr/io.h>
#include <avr/interrupt.h>
#include <HardwareSerial.h>
#include <util/delay.h>
  
ISR(INT0_vect0)
{
    if(PIND == 255)
    {                          
        PORTB = 00000000;
    }
    else if(PIND == 251)
    {
        PORTB = 00000001;
    }  
}

void initInterrupt0(void)
{
    /*Enable INT0*/
    EIMSK |=  1<< INT0;

    /*Trigger when button changes*/
    EICRA |=  (1<<ISC00);

    sei(); 
}

void setup() 
{
    DDRB = 0xff;
    initInterrupt0();
    PORTD = 0xff;
}

void loop() 
{
    delay(200);
}


Solution 1:[1]

it seems that there is a mistake in your code! Take a look at this Line:

ISR(INT0_vect0)
{
  // ...
}

The INT0 interrupt normally is called INT0_vect!

So first try to adapt your code:

ISR(INT0_vect)
{
  // ...
}

The respond of the interrupt with ISC00 set will be on:

Any logical change on INT0 generates an interrupt request.

So check if this is the right setup?! To keep things simple you can write the following code:

ISR(INT0_vect0)
{                      
  PORTB = ~PORTB;
}

to check of the interrupt responds. Eventually there can be done some other optimisations e.g.:

if(PIND == 255)
{                     
    PORTB = 00000000;
}
else if(PIND == 251)
{
    PORTB = 00000001;
} 

could be adapted to:

if(!(PIND & (1<<PIND2)))
{                         
  PORTB = 0x01;
}
else
{
  PORTB = 0x00;
}

instead of working with numbers like 251...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1