0
\$\begingroup\$

I want to connect 4 push buttons using interrupts on all of them, with my atmega328p, but when I read the datasheet, I found that the atmega328p has only 2 external interrupts (INT0 and INT1). Are there any solutions?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

The obvious: Connect two buttons to one interrupt and use a different pin and code to figure out which one was actually pressed in the Interrupt Handler.

That's pretty standard procedure in hardware design.

Also, note that humans are incredibly slow compared to microcontrollers. It's usually totally unnecessary to use an interrupt line for something like a button unless it's for waking up from extreme deep sleep modes (which the ATMega 328 doesn't have, as far as I know).

So, unless you really need to have high-speed interrupts, it's usually not desirable to use interrupt pins for button IO – buttons ring, and that means that pressing the button once will trigger a whole torrent of interrupts, of which you only want the first one.

Instead, simply loop your code, and occasionally check your IO pins.

\$\endgroup\$
7
  • \$\begingroup\$ Thank you sir, I use the four push buttons to send specific messages using a GSM module linked to my microcontroller, so to mimimize the energy consumption of my system, I thought to use the interrupts to leave the Microcontroller in the sleep mode \$\endgroup\$ Aug 20, 2017 at 18:00
  • \$\begingroup\$ hi uhours, the 0.1µA mode is not "deep sleep" by my (admittedly high) standards, because it's not that much lower than the other sleep states from which you'd want to wake up by a button press. It's still correct what you say, the ATMega328 does have sleep states from which you can wake up through an interrupt. \$\endgroup\$ Aug 20, 2017 at 18:23
  • 1
    \$\begingroup\$ @YassineKardid are you deactivating the complete GSM module while you sleep? Otherwise, the power saving in your ATMega will be negligible compared to what the GSM module needs. \$\endgroup\$ Aug 20, 2017 at 18:24
  • \$\begingroup\$ Your answer is very helpful . My GSM module remains in sleep mode so long as it does not ask to send a message. \$\endgroup\$ Aug 20, 2017 at 18:57
  • \$\begingroup\$ If I connect two push buttons to an external interrupt, when the interrupt is triggered I can't identify which button is pressed, because it is already released. \$\endgroup\$ Aug 20, 2017 at 19:48
2
\$\begingroup\$

All pins can trigger interrupts when change state (high to low or low to high). Only INT0 and INT1 can be configured to trigger interrupts when there is a low level on the pins (this is not the same as changing state), and will trigger continuously until the input is no longer low. INT0 and INT1 can also be configured to to trigger on any change, or only rising or only falling edges.

The pins are grouped into three pin change interrupt vectors, so one of those interrupts will trigger if any pin on that vector changes state, and has interrupts enabled.

\$\endgroup\$
5
  • \$\begingroup\$ Thank you sir, but I found in some forums that it is not advisable to use a push button on the Pin Change Interrupt of the microcontroller. \$\endgroup\$ Aug 20, 2017 at 18:06
  • \$\begingroup\$ @YassineKardid -- mostly for debouncing reasons... \$\endgroup\$ Aug 20, 2017 at 18:09
  • \$\begingroup\$ @YassineKardid If power consumption is a concern, you should absolutely use interrupts, and debounce after it triggers. Alternatively, use a 32.768kHz crystal to wake it up from power-save mode, or use the WDT to wake it from power-down mode (the WDT has shit tolerance, so don't use it for accurate measurements). \$\endgroup\$
    – uhours
    Aug 20, 2017 at 18:17
  • \$\begingroup\$ Huh, there's also a standby mode. I've never really looked at that one. You won't get the most savings possible, but you don't need external parts either, and it doesn't look like you'll need interrupts. \$\endgroup\$
    – uhours
    Aug 20, 2017 at 19:04
  • \$\begingroup\$ I'll do some research on that. \$\endgroup\$ Aug 20, 2017 at 19:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.