Arduino abstraction usually works well, hiding the often useless details and making code run on many different MCUs with next to no modification. 0 and 1 are always hardware serial, pin 13 always has an led to blink. A0 is always the first analog input, external interrupt 0 is always on pin 2. See? But it is not always like this. Here is an example where it fails and creates confusions:
On MEGA2560, Arduino external interrupt 0 and 1 are on pins are 2 and 3, just like on UNO. But if you couple this with sleep mode, you will likely hit a snag that I hit today.
On UNO, you can wake it with LOW, FALLING, or RISING of external interrupt 0 and 1. On MEGA, you can only do LOW, not FALLING or RISING. I tested my code to run perfectly on UNO, but failed to run on MEGA. The reason? ATMEGA can also wake on the above 3 modes with INT0 and INT1. But Arduino MEGA2560 pin 2 is NOT INT0, it is INT4. The Arduino abstraction made it Arduino interrupt 0 and pin 2. This doesn't mean you have the same behavior as UNO'S interrupt 0 on pin 2. So just be cautious when you go beyond simple Arduino libraries and basic functions. You need the spec sheet.
hey Liudr longtime!
an excerpt from the ATmega2560 datasheet says that it has external interrupts on 8 pins namley INT7.......INT0 vectors
also its specifically written that almost all of the pins on atmega2560 support 3 levels of interrupt.
I think the problem is that the pin is not being 0 or 1 for the minimum time needed to generate an interrupt successfully.
or your configuration of the interrupts is not done correctly.
can you confirm my manually modding the interrupt setup registers to configure them to do other 2 level interrupts on the pin that supports one of the external interrupt vectors ?
Been a while.
Yes to INT7-0. If you read the chapter for power management, there is a table of all sleep modes. A foot note [3] in the column for wake-up source is attached to external interrupt and pin change interrupt. The note says INT7-4 level only. Should not make assumptions unless you consulted the spec sheet.
Again, this only affects these interrupts when you want the MCUS to wake up on them. Normal interrupts aren't affected. Confused me a whole lot until I started to tear down my code into one-thing only. That was when I realized the interrupt works fine but won't wake MCU. Then I read the spec sheet.
The note says INT7-4 level only. Should not make assumptions unless you consulted the spec sheet.
gone through that ain't making any assumptions. I was reading and consulting the External interrupts and maybe didnt get your problem.
maybe your way of expressing is different and Imay not be getting what you actually said.
now I went to the Table 22. and read footnote and got what you saying.
liudr:
You need the spec sheet.
Even that is not necessarily enough. The datasheet for the Atmega328 in the Uno has a similar footnote for wake-up: "For INT1 and INT0, only level interrupt". It's a documentation error. But if you had dutifully followed the datasheets then you would have never designed a system that used anything other than a level interrupt for this in the first place.
It makes me wonder if it is also an error in the 2560 datasheet. Are you 100% certain that the processor doesn't wake-up but fail to generate an interrupt?
It's a documentation error.
hmm.. maybe have to reasearch more.
Its also written there that it needs a certain minimum time to trigger.
I think he only meant that the Arduino IDEs method of attachInterrupt that offers a rising or falling edge interrupt in faulty if you move over from INT0 or INT1 interrupts to other INT2 and so on INterrupts because Arduino actually began with INT0 and INT1 for ATmega328p
I tested on two Arduinos with MEGA 2560. RISING or FALLING will not trigger interrupt while the MCU is sleeping, with either pin 2 or 3. Changing to LOW triggers interrupt and wakes the MCU.
With an Arduino using 328, I can trigger interrupt with RISING and FALLING while the MCU is sleeping to wake the MCU. I guess documentation error only occurred on 328.
BTW, the reason you don't see 8 external interrupt pins on Arduino MEGA 2560 is because they didn't break the two pins out. I blame sparkfun for their initial effort on Arduino MEGA using 1280.
Regarding timing, according to Arduino IDE's boards.txt, Arduino MEGA 2560 uses a fuse settings for crystal oscillator in low power mode that allows the lowest wake up time 16K CK or about 1ms. On the other hand, I've seen newer MEGA 2560 with ceramic oscillator so I have to probe their fuse setting to know what they're using.
It's quite a lot of reading. I'm glad that I did and thankful for jboyton's mention of doc error exists.
I'll have to dig deeper. On 328P, the INT0,1 pins also have PCINT18 and 19. Maybe Arduino IDE enabled PCINT too? Probably not. Other than that, 328 and 2560 docs say the same thing about the reason their certain external interrupt won't detect edge, i.e. all I/O clocks are halted. 2560 has INT3:0 with async module so won't need I/O clock. It is pretty convincing to me.
liudr:
I tested on two Arduinos with MEGA 2560. RISING or FALLING will not trigger interrupt while the MCU is sleeping, with either pin 2 or 3. Changing to LOW triggers interrupt and wakes the MCU.
With an Arduino using 328, I can trigger interrupt with RISING and FALLING while the MCU is sleeping to wake the MCU. I guess documentation error only occurred on 328.
You didn't really answer my question. You need to be able to tell the difference between waking up with an interrupt and waking up but not getting the interrupt. Maybe the signal is such that the latter event is impossible. But that's what I was wondering if you were 100% sure about.
I think I'm reading this backwards -- there is no case where you can wake but have no interrupt if it is rising or falling.
By the way, the documentation error was verified by email from Atmel. Look here and search for "atmel".
Great! Someone from atmel confirmed, for 328 though. I did a number of tests with FALLING and RISING tests with 2560 to no avail. Only LOW wakes it up (INT5:4). I think the data sheet is correct on MEGA2560.
BTW, I CAN tell whether MCU wakes with or without triggering an interrupt. I have a counter that increments in ISR and another time that compares with it in loop. There is a chance an incomplete LOW level wakes the MCU but doesn't generate an interrupt. Since my hardware is already designed and assembled, I'll have to account for that. I'll do some tests to see if I observe it in my system. It's counting pulses form a a rain gauge. Too bad the other interrupts are on serial and I2C pins. Those should work with all modes.
I'll post some test results once I have them.
I believe you. I just thought it was worth asking since Atmel made a mistake documenting a similar thing with another chip.
Probably mistake from some under-appreciated engineers making documentation by copy and paste. For 1284, the doc is even worth. They messed up the table detailing the sleep modes and wake up sources and the column names didn't display in last revision. This revision still has problems with that same table.
For my project, I might have to use PCINT. I do have a pin with PCINT brought out to a screw terminal block.