#define LED PORTD.F0
void interrupt(void) org IVT_ADDR_INT0 { //INT0 ISR is carried out when there is a logical change on INT0
LED = !PIND.F2; //If INT0 low, then light LED and vice versa
//Interrupt flag is automatically cleared by hardware
}
void main(void) {
MCUCR = 1; //Logical change on INT0 generates interrupt
INT0_bit = 1; //Enable INT0 interrupt
SREG_I_bit = 1; //Enable Global interrupt
/*
Interrupt is set for logical change.
This means whenever output changes from 0 to 1 or 1 to 0, interrupt occurs
Other possible interrupt selections are logical low level, ie when pin is 0
falling edge - from 1 to 0
rising edge - from 0 to 1
Whenever interrupt occurs, the program is halted, the program counter goes to the ISR
Each interrupt has its own service routine and its own vector IVT_ADDR_INT0 is for INT0, address = 0x02
Whenever interrupt occurs, the corresponding ISR - interupt service routine - is carried out
There are 3 external interrupt modules on ATMEGA32, I used INT0
Whenever pin goes from 0 to 1 or 1 to 0 (INT0 pin, PORTD2 - PD2), ISR - address 0x02 - is carried out
*/
DDRD.F2 = 0; //INT0 pin input
DDRD.F0 = 1; //PD0 output for LED
//PORTD.B2 = 1; //Enable pull-up for INT0 pin (not necessary since your sensor can source current)
while (1); //Stay here forever, waiting for interrupt
}