Problem with PINChange Interrupts

Good Evening together,

I am currently trying to work with PINChange Interrupts. But it does not quite work yet. I connected an Ultrasonic Sensor with Trigger to A8(PK0) and the Echo to D53(PB0/ PCINT0). I cant find my mistake but I guys it has to do with the way I enable the PCI.
Here is the code:

volatile byte portBstate, portBpast, changedbits;
volatile boolean intcalled = false;
volatile int count=0;

ISR (PCINT0_vect)
{
  portBpast = portBstate;
  portBstate = PINB;
  changedbits = portBpast ^ portBstate;
  
  if (changedbits & 0b00000001)
  {
    count++;
    if (count = 2)
    {
    intcalled = true;
    count = 0;
    }
  }
  else ;
  
}

void setup ()
{
  DDRB = B00000000;
  PORTB = B00000000;
  portBstate = PINB;
  Serial.begin(115200);
  PCICR |= (1<<PCIE0);
  PCMSK0 |= (1<<PCINT0);
}
void loop ()
{ PORTK = B00000000;         //D30 bis D37 auf LOW
  delay(2);
  PORTK = B11111111;         //D30 bis D37 auf HIGH
  delay(5);
  PORTK = B00000000;         //D30 bis D37 auf LOW
  delay(100);
  
  if (intcalled)
  {
    Serial.print ("D53");
    intcalled=false;
  }
  else
  {
    Serial.print ("DX");
  }
}

I am thankful for any help :slight_smile:

notoriou5

    if (count = 2)

Assigning a value of 2 to count in an if statement hardly seems useful.

Thats true ;)

I kicked the count out of the code and tried to define and enable the interrupts a different way, still no sucess. I put a Serial.print in the Interrupt function and noticed it never gets called. So the problem must be the way I enabled them. Do you guys see the mistake I made?

Here the modified code:

#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))


volatile byte portBstate, portBpast, changedbits;
volatile boolean intcalled = false;
volatile int count=0;

ISR (PCINT0_vect)
{
  portBpast = portBstate;
  portBstate = PINB;
  changedbits = portBpast ^ portBstate;
  if (changedbits & B00000001)
  {
    intcalled = true;
  }
  else ;
  
}

void setup ()
{
  DDRB = B00000000;
  PORTB = B00000000;
  portBstate = PINB;
  Serial.begin(115200);
  sbi (PCICR, PCIE0);
  sbi (PCMSK0, PCINT0);
  //PCICR |= (1<<PCIE0);
  //PCMSK0 |= (1<<PCINT0);
}
void loop ()
{ PORTK = B00000000;         //A8 bis A15 auf LOW
  delay(2);
  PORTK = B11111111;         //A8 bis A15 auf HIGH
  delay(5);
  PORTK = B00000000;         //A8 bis A15 auf LOW
  delay(100);
  
  if (intcalled)
  {
    Serial.print ("Interrupt called");
    intcalled=false;
  }
  else
  {
    Serial.print ("ERROR");
  }
}[\code]

notoriou5
  else ;

Seriously? Why? If there is nothing to do, you do not NEED an else statement.

  if (changedbits & B00000001)
  {
    intcalled = true;
  }

Wrong. The interrupt was called even if changedbits anded with 1 is false.

What is connect to the pins that you think are triggering the pin change interrupt? Which pins do you think are triggering the pin change interrupt?

Why are you using pin change interrupts instead of using an external interrupt pin?

Has this even the slightest connection to your other Thread ?

...R