ATtiny85: Using pin interrupts on PB3 makes PB1 unusable

So, I thought I'd try using pin interrupts to make my code more performant. I used this helpful tutorial as a starting point: https://www.instructables.com/ATtiny85-Interrupt-Barebones-Example/

However, it uses PB1 for its input. That's no bueno for me. I need all three PWM outputs to drive an RGB LED, and PB1 is one of them. So, I switched the trigger pin to my usual go-to, PB3. Here's my code:

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

#define INTERRUPT_PIN PCINT1
#define INT_PIN PB3
#define LED_PIN PB4
#define PCINT_VECTOR PCINT0_vect

void setup() {
  pinMode(LED_PIN, OUTPUT);

  cli();
  PCMSK |= (1 << INTERRUPT_PIN);
  GIMSK |= (1 << PCIE);
  pinMode(INT_PIN, INPUT_PULLUP);
  sei();
}

void loop() {}

ISR(PCINT_VECTOR) {
  if (digitalRead(INT_PIN) == LOW) {
    digitalWrite(LED_PIN, HIGH);
  }else{
    digitalWrite(LED_PIN, LOW);
  }
}

And this works very well, thank you. I press the button, the LED lights up. Perfect, right? Right.

Except... the whole thing stops working and refuses to do anything if I set PB1 to an output, or even if I just plug the LED leg into it. I'm pretty sure my wiring is fine because it works nicely when I don't use interrupts. My first thoughts are, maybe it's expecting that pin to be an input too. Maybe I need different values for PCMSK or GIMSK or both. I tried a few, but nothing seems to work.

Is there a way for me to use PB3 as a pin interrupt, and all the remaining GPIO pins as outputs simultaneously?

Show the wiring diagram.

1 Like

It's OK, it's not the wiring, I figured out how to make it work, although frankly why this makes a difference is beyond me. Instead of using the 'or-equals' operator to add the button pin's bit to the PCMSK register, I set the register in its totality with exactly the bits that I want, like this:

PCMSK = 0b00001000;

Now if, as the data sheet suggests, the default PCMSK value is 0b00000000, this ought to not be any different from what I was doing before. However, this actually works as expected. I can use any output pins I want.

Though I'm probably going to change it to use a left-shift operator too, so that I only have to change my button leg's index in one place rather than two.

1 Like

You want to change PCINT1 to PCINT3, update your #define to indicate the actual pin you want to use.

Then the mask will be generated correctly

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.