Pin Change Interrupt on PB3. Am I doing something daft?

Hi.

No idea what I am doing wrong. Keep reading then datasheet and believe I have done this correctly.

When I simply run:

Serial.println(digitalRead(PB3));

in loop, I get my expected “0s and 1s” from an IR receiver…

Why is it when I run my code, I don’t seem to have the PinChangeIntterupt working?

#define rxpin PB3
#define bits 16
#define led 14

unsigned int data = 0;
volatile unsigned long val = 0;

void setup() {

  pinMode(rxpin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(115200);
  Serial.println("Ready...");
  setInterrupt();

}

void loop() {
  Serial.println(val);

}

unsigned int manRead() {

  /*

    The Manchester Encoding scheme : 1 = H->L 0 = L->H
    Reading taken in middle of period.
    Can use pin change interrupts + to ascertain the rise or fall Rx Side...

  */

}

void setInterrupt() {
  // Sets the pin change interrupt...
  /*
     PCIE0 in PCICR is for PCINT[7:0]. Ours in on PCINT3.

     Set this and SREG I to 1 for interrupt enable...

     The interrupt vector is PCI0.

     PCMSK0 is the masking register...
  */
  
  cli();
  PCICR = 0;
  PCICR |= (1 << PCIE0);
  PCMSK0 = 0;
  PCMSK0 |= (1 << PCINT3); // PB3 / Pin 3.
  sei();    // Set I bit in SREG...interrupt is now active.
}

ISR(PCINT0_vect) {
  
  //Increment the volatile long val by 1...
  cli();
  val++;
  sei();

}

Thanks in advance! I have probably done something stupid…

I can’t tell you if your register setup is correct. Why don’t you just use the attachedInterrupt() function?

I can tell you that your accessing of ‘val’ in loop() may not be atomic. Try this:

void loop() {
  uint32_t localVal;
  noInterrupts();
  localVal = val;
  interrupts();
  Serial.println(localVal);
}

You might also want a delay in loop() to slow down the print rate.

ISR(PCINT0_vect) {
 
  //Increment the volatile long val by 1...
  cli();
  val++;
  sei();

}

Interrupts are already disabled during an ISR call.

#define rxpin PB3

What value does PB3 have? Does setting that pin number to an INPUT pin make sense?

Which Arduino are you doing this on?

PaulS:

ISR(PCINT0_vect) {

//Increment the volatile long val by 1...
  cli();
  val++;
  sei();

}



Interrupts are already disabled during an ISR call.



#define rxpin PB3



What value does PB3 have? Does setting that pin number to an INPUT pin make sense?

Which Arduino are you doing this on?

Thanks. I assumed when reading the datasheet the interrupts were disabled "on execution" which I didn't know if it meant on the first cycle of or after the entire interrupt vector...so I threw it in "just in case" . Will remove it now I know it means at the start of the execution of the ISR.

I made my own ATMEGA328P-PU based board. 16MHz crystal. ISP breakout.

PB3 is "IDE 11". Should I be using "11" when using the IDE to define pins?

Johnny010:
PB3 is "IDE 11". Should I be using "11" when using the IDE to define pins?

Should be? I'd be looking at the core files to see if I could find that define and make sure.

PB3 is a macro defined by AVR-Libc.

Serial.println( PB3 );

Right. I am really sorry. I did do the stupid...

It was on PD3 and not PB3. My bad...