1
\$\begingroup\$

I want to turn on LEDs 0, 1 and 2 when pinc1, pinc2 and pinc3 are triggered. Also, I want to use a pin change interrupt.

Problem: only LED 1 is on.

This is my code and I am using atmega328p and Proteus for simulation:

#include <avr/io.h>
#include <avr/interrupt.h>

volatile uint8_t bemf = 0 ;
ISR(PCINT1_vect)
{

if (PINC & 00000010){bemf=1;}
if (PINC & 00000100){bemf=2;}
if (PINC & 00001000){bemf=3;}

}

int main(void)
{

DDRD = 0xFF ; 
PORTD = 0x00;
PCMSK1 |= 1<<PCINT10 | 1<<PCINT11 | 1<<PCINT9 ; // pinc1 pinc2 and pinc3
PCICR |= 1<<PCIE1 ;
sei();

while (1) 
{

  if (bemf==1){PORTD= 1<<PIND0;}  // TURN ON LED 0
  if (bemf==2){PORTD= 1<<PIND1;}  // TURN ON LED 1
  if (bemf==3){PORTD= 1<<PIND3;}  // TURN ON LED 2


}
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Hint: PORTD = 1 << PIND3 erases the values for the other pins being set on/off. You need to combine the flags, not overwrite them. \$\endgroup\$ – Ron Beyer Dec 25 '17 at 2:28
2
\$\begingroup\$

the solution is to use this

PINC & 0b00000010

instead of

PINC & 00000010
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.