I was programming for external interrupt using ATmega128. I set external interrupt 0 and 1 to falling edge triggered interrupt. The interrupt has to count the number of times the switch is pressed. But when the key is pressed once, the interrupt is called 2 or 3 times and so it counts higher. What is the problem here ?
My interrupt code is here
ISR(INT1_vect){
cli();
++push_cntr1;printf("p1:%d,",push_cntr1);
sei();
}
ISR(INT0_vect){
cli();
++push_cntr0;printf("p0:%d,",push_cntr0);
sei();
}
void interrupt_init(void) //enable interrupt 0 and 1.
{
EICRA |= (1<<ISC01)|(0<<ISC00)|(1<<ISC11)|(0<<ISC10);
EIFR = 0xFF;
EIMSK |= (1<<INT1)|(1<<INT0);
DDRD = 0x00;
}
int main(void) //main program
{
dev_init();
while(1){
process();
}
return 0;
}
You don't need to clear and set the i bit within the interrupt handlers. It will be taken care of by the compiler.
As for the multiple hits, you could be suffering from "switch bounce." There are many references to this on the web, but the gist is that the voltage in the switch circuit doesn't simply go from 0 to max in a clean jump. This is most often caused by the actual physical bouncing of the electrical contacts.
There are many techniques available to "debounce" a switch. You could try something as simple as recording the tcnt and ignoring interrupts too soon after a previous one.