External Interrupt (INT0 INT1)


/*
Bismillah Hir Rahmaanir Raheem.
ATMega88.
AVR-GCC External Interrupt (INT0 INT1).
Not much tested.
PD2 is the interrupt pin, connected to 5v VCC through 10k resistor. Connect/short GND to generate interrupt.
LED is connected to PD4.
Debounce is needed.
*/
#include <avr/io.h>
#include <avr/interrupt.h>

#define bit_get(p,m) ((p) & (m))
#define bit_set(p,m) ((p) |= (m))
#define bit_clear(p,m) ((p) &= ~(m))
#define bit_flip(p,m) ((p) ^= (m))
#define bit_write(c,p,m) (c ? bit_set(p,m) : bit_clear(p,m))
#define BIT(x) (0x01 << (x))
#define LONGBIT(x) ((unsigned long)0x00000001 << (x))

int main(void){
bit_set(DDRD, BIT(4));//Set as output
bit_set( EIMSK, BIT(INT0) ); //Enable External interrupt for INT0 (PD2)
//bit_set( EICRA, BIT(ISC01) | BIT(ISC00) );// interrupt on INT0 pin rising edge. p. 66
bit_set( EICRA, BIT(ISC01));// interrupt on INT0 pin falling edge. p. 66
sei();

for(;;){
//
}

}

ISR(INT0_vect){ //Set interrupt function. Datasheet page. 57
//if ( bit_get( PIND, BIT(2) ) == BIT(2)){
bit_flip(PORTD, BIT(4));
//}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s