Pin Change Interrupt


/*
Bismillah Hir Rahmaanir Raheem.
ATMega88.
AVR-GCC Pin Change Interrupt.
=============================
[[[ Led is attached to Pin14 and
Interrupt pin is Pin4(PCINT18/PD2).
Blink led on interrupt.]]]
*/

#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(DDRB, BIT(0));//Led attached to PB0(Pin 14), set as output.

bit_set( PCICR, BIT(PCIE2) );//Set Pin Change Interrupt Control Register. Datasheet p. 68
bit_set( PCMSK2, BIT(PCINT18) );//Select pin. Set Pin Change Mask Register 2. Datasheet p. 69
sei();

for(;;){

}
}

ISR(PCINT2_vect){ //Set interrupt function. Datasheet page. 57
//bit_flip(PORTB, BIT(0));
//if ( bit_get( PIND, BIT(2) ) == 0b00000100){

if ( bit_get( PIND, BIT(2) ) == BIT(2)){
bit_set(PORTB, BIT(0));
} else {
bit_clear(PORTB, BIT(0));
}

}

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