NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Pin change interrupt

December 24, 2009
by lcruz007
lcruz007's Avatar


I am working in a pin change interrupt code. whenever it is high, it will light up an LED, however, I don't know how to trigger an interrupt whenever the pin goes low. Does someone knows how to do this? This is my code:


volatile int32_t count = 0; int count2 = 0; void setup() {

PCMSK2 |= (1<<PCINT18);

PCICR |= (1<<PCIE2); PCIFR = (1<<PCIF2); sei(); }

ISR(PCINT2_vect) { count=1; }

int main() { setup();

// init lcd lcd_init(); FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE); lcd_home();

uart_init(); FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); stdin = stdout = &uart_stream;

while(1) { if( count ==1 ) { DDRB |= (1<<PB3); } else DDRB &= ~(1<<PB3);

if( count2 >= 2 && count2 <= 4 ) { //DDRB |= (1<<PB3); }

count = 0; }

return 0; }


December 28, 2009
by hevans
(NerdKits Staff)

hevans's Avatar

Hi lcruz,

The pin change interrupt (when enabled) will fire every time a pin switches from high to low, or from low to high. Inside the interrupt handler you should check to see if the pin is low or high, and act accordingly if it matters to your application.

In your code, you should not be setting the PCIFR register. This is the flag register that the software sets when it is requesting an interrupt. You can set this register if you want to trigger an interrupt, or prevent one from firing, but otherwise you should not need to touch it.


Post a Reply

Please log in to post a reply.

Did you know that interrupts can be used to trigger pieces of code when events happen? Learn more...