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.

asked May 15, 2018 at 13:37
9
  • 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 Commented May 15, 2018 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? Commented May 15, 2018 at 13:55
  • I assume there is a 1:1 mapping between pin numbers and PORTB bits? Commented May 15, 2018 at 13:56
  • @majenko it looks like it based on the datasheet. Commented May 15, 2018 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. Commented May 15, 2018 at 14:00

1 Answer 1

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.

answered May 15, 2018 at 15:14
3
  • 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 Commented May 15, 2018 at 16:27
  • @chrisl, you have to wait for two days to accept your own answer. Commented May 15, 2018 at 17:15
  • I think 2 days passed already, its time Commented Oct 9, 2023 at 18:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.