0

Experimenting with external interrupts on a Mega. Pins 2 and 3 work with standard

attachInterrupt(digitalPinToInterrupt(iPin),ISR,FALLING);

coding and call my ISR. With pins 18 and 19 I had to add

EIFR = 4;

for pin 19, and

EIFR = 8;

for pin 18 before calling attachInterrupt(...) to get them to work. Why?

BTW, I'll add this table that I filled in by trial and error for anyone interested in Atmel (AVR) external interrupt numbers to Arduino external interrupt numbers (I won't list the AVR chip pin numbers)

AVR INT      5 .  4 .  3 .  2 .  1 .  0
EIFR (Dec)  32 . 16 .  8 .  4 .  2 .  1
Arduino Pin  3 .  2 . 18 . 19 . 20 . 21
Arduino INT  1 .  0 .  5 .  4 .  3 .  2

I know writing a 1 to the EIFR bit for the appropriate interrupt clears the interrupt. When I reset the Arduino without clearing interrupts on pins 2 and 3 it's fine. If I don't clear 18 and 19 after a reset, it throws the interrupt right away. (Yes, I should OR the EIFR with the bit I want to change but I'm just playing) Didn't try 20 and 21 because I'm using them for comm.

4redwings
  • 103
  • 3

1 Answers1

2

It's not that pins 18 or 19 are faulty, you are just lucky the other ones worked.

As I mention on my page about interrupts* you are recommended to always write 1 to the appropriate EIFR bit before enabling interrupts because it (the interrupt condition) may have been set in the past.

I checked the source for attachInterrupt in the file WInterrupts.c and there is no mention of the EIFR register, therefore attachInterrupt never affects that register.


* Also see my answer: How do interrupts work on the Arduino Uno and similar boards?

Nick Gammon
  • 38,901
  • 13
  • 69
  • 125