Arduino Mega 2560 interrupt numbers vs. schematic?

This is both a software and hardware question - apologies if it doesn't belong here, but I didn't see a more appropriate forum area. The Mega 2560 documentation says, for example, that pin2 is interrupt 0, and pin21 interrupt is 2. Yet the schematic shows pin2 connected to INT4 and pin21 to INT0. My tests verify that the documented interrupt numbers and pin numbers work with attachInterrupt(), so this isn't a huge deal except that the Atmega interrupt priorities are determined by lowest INT number. So am I correct in concluding that pin21 is the highest priority (INT0) even though it is neither the lowest nor highest interrupt number in Arduino land?

I think if your application needs simultaneous interrupts to be processed in a particular order you should probably be writing in AVR code rather than using the Arduino abstractions. :slight_smile:

pschimpf: This is both a software and hardware question - apologies if it doesn't belong here, but I didn't see a more appropriate forum area. The Mega 2560 documentation says, for example, that pin2 is interrupt 0, and pin21 interrupt is 2. Yet the schematic shows pin2 connected to INT4 and pin21 to INT0. My tests verify that the documented interrupt numbers and pin numbers work with attachInterrupt(), so this isn't a huge deal except that the Atmega interrupt priorities are determined by lowest INT number. So am I correct in concluding that pin21 is the highest priority (INT0) even though it is neither the lowest nor highest interrupt number in Arduino land?

It can be confusing and misleading. There is no direct relationship from the AVR user interrupt pin names (INT0, etc) and the Arduino external interrupt numbers used with the AttachInterrupt function (numbers 0-5), you just have to live with the pin mapping the arduino uses. Also I don't think there is any implied 'priority' between the interrupts, it's just first come first service, and when inside a active interrupt service routine all other interrupts are disabled until the active ISR returns.

So stick to this from the arduino reference when wiring up interrupts:

Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).

Lefty

My question is answered but I believe I should clarify for other readers. I have verified that the documented interrupt numbers and pins for Mega DO work with the Arduino attachInterrupt construct. I also understand how to use interrupts in AVR code directly. I have used AVR development for a non-Arduino board, but prefer to have my students use the Arduino constructs where possible, and it is possible in this application (yes, I am an educator). I also understand that further interrupts are disabled by default. There are indeed fixed interrupt priorities established by Atmega - this is easy to find in the Atmega datasheets. When does this matter? It matters when multiple interrupts are pending (either by being in an active state or because event-type interrupts are latched). The interrupt with the highest priority will be serviced when interrupts are enabled. It can be important. Unless you're trying to dynamically enable and disable individual interrupts there is no reason multiple interrupt sources can't be handled with Arduino constructs (with care, of course). So if anyone else needs to worry about multiple interrupt priorities, the answer is that you need to look at the schematic for your Arduino board and determine which Atmel pin the Arduino pin is tied to - do NOT rely on Arduino pin numbers OR Arduino interrupt numbers.