0

I am using an attiny85 to read the fan speed reported by a pair of 4-wire fans. I had planned to attach the TACH output from the fans to pins 1 and 2, but I ran into a problem: I was able to handle interrupts on pin 1 without a problem, but I can't get the ISR to trigger for changes on pin 2. Code demonstrating the problem looks like this:

#define PIN_LED 0
#define PIN_TACH2 2

void setup() {
  cli();
  pinMode(PIN_TACH2, INPUT_PULLUP);
  pinMode(PIN_LED, OUTPUT);

  GIMSK |= _BV(PCIE);
  PCMSK |= _BV(digitalPinToPCMSKbit(PIN_TACH2));
  sei();
}

void loop() {
  digitalWrite(PIN_LED, 1);
  delay(2000);    
}

ISR(PCINT0_vect) {
  byte tach2;

  tach2 = (PINB & _BV(PIN_TACH2));

  if (! tach2) {
    digitalWrite(PIN_LED, 0);
  }
}

If I replace the definitions of PIN_TACH2 and PIN_TACH2_INT with pin 1, such as...

#define PIN_TACH2 1
#define PIN_TACH2_INT PCINT1

...it works just fine (interrupts also works for pins 0 and 3).

Why can't I handle pin change interrupts for pin 2?

for @chrisl

This is just testing code. The LED remains on until the ISR fires, at which point it will go out for up to 2 seconds. With interrupts attached to pins 1 or 3, it works as expected: a falling edge on the pin causes the LED to go off.

On pin 2, the ISR never triggers.

  • I don't quite understand, what your code should do. You are only turning the LED on every 2s. Explain further what you expect, what happens instead and what can you see using pins 1 or 3 – chrisl May 15 '18 at 13:50
  • PCINT should turn LED off. Within 2 seconds it will come on again. Simple testing code. With PCINT1/3 it apparently works (LED can be turned off) but not with PCINT2. Looks to me like it should work, but what do I know? – Majenko May 15 '18 at 13:55
  • I assume there is a 1:1 mapping between pin numbers and PORTB bits? – Majenko May 15 '18 at 13:56
  • @majenko it looks like it based on the datasheet. – larsks May 15 '18 at 13:59
  • 1
    You won't find that in the datasheet. You'll only find it in the source code or documentation for whatever core / board definition you are using. – Majenko May 15 '18 at 14:00
2

So, it turns out that it was a really stupid problem and entirely my fault, but I'm going to leave this question here in the hopes that it will prevent someone else from running into the same issue.

The answer is...

DISCONNECT THE PROGRAMMER BEFORE TESTING YOUR SKETCH

That's it. With the programmer disconnected, it all behaves as expected.

  • Since this is the solution for your problem, please accept your answer as the correct one, so that others can see, that this was right – chrisl May 15 '18 at 16:27
  • @chrisl, you have to wait for two days to accept your own answer. – larsks May 15 '18 at 17:15

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.