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 3chrisl– chrisl05/15/2018 13:50:48Commented 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?Majenko– Majenko05/15/2018 13:55:05Commented May 15, 2018 at 13:55
-
I assume there is a 1:1 mapping between pin numbers and PORTB bits?Majenko– Majenko05/15/2018 13:56:04Commented May 15, 2018 at 13:56
-
@majenko it looks like it based on the datasheet.larsks– larsks05/15/2018 13:59:32Commented May 15, 2018 at 13:59
-
1You 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– Majenko05/15/2018 14:00:09Commented May 15, 2018 at 14:00
1 Answer 1
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 rightchrisl– chrisl05/15/2018 16:27:41Commented May 15, 2018 at 16:27
-
@chrisl, you have to wait for two days to accept your own answer.larsks– larsks05/15/2018 17:15:48Commented May 15, 2018 at 17:15
-
I think 2 days passed already, its timefrogstair– frogstair10/09/2023 18:18:43Commented Oct 9, 2023 at 18:18