2023-02-02 12:43 PM
Hello,
I am looking to implement multiple push buttons (7, specifically) that are used to turn certain features ON/OFF on a front panel. For example:
PB1 = LED ON/OFF
PB2 = DC Power ON/OFF
PB3 = AC Power ON/OFF
etc.
My current plan for my custom board to do this is to use pins PC6-12 and use interrupts (GPIO_EXTI6 for Push Button 1, GPIO_EXTI7 for Push Button 2, etc.). I did this because, on this resource below, it states you cannot use two pins on one line, so I wanted to make sure each button was on a different pin number.
I have two questions based on this:
1) If these buttons are NOT being pressed simultaneously (There's no application to turn them both on at the same time), is the above resource still a hinderance? Or if I just press one of the multiple buttons on the same pin (i.e. PC6 and PD6), can I distinguish which button was pressed?
2) Is this a logical approach? The tasks are not necessarily time sensitive, so I was wondering if polling made more sense, but both options would work, correct?
For reference, I am using STM32H7B3LIH6Q
Solved! Go to Solution.
2023-02-02 04:06 PM
If multiple buttons are on the same Line number then there is no way for the interrupt to let you know which button was pressed that triggered the same Line. Each button has to be on it's own Line so that the interrupt callback will let you which of the 0-15 bits caused the interrupt.
Polling would make more sense if you have buttons on PC6 and PD6.
2023-02-02 04:06 PM
If multiple buttons are on the same Line number then there is no way for the interrupt to let you know which button was pressed that triggered the same Line. Each button has to be on it's own Line so that the interrupt callback will let you which of the 0-15 bits caused the interrupt.
Polling would make more sense if you have buttons on PC6 and PD6.
2023-02-02 06:04 PM
Hello Karl,
Thank you for confirming! So using PC6-12 is okay then, since it's a different pin number. Thank you for confirming that!
2023-02-02 11:25 PM
Handling the buttons using EXTI is generally a bad idea. That was discussed many times on this forum - just search. You cannot handle buttons with EXTI unless you also use timer interrupt and if you use a timer interrupt - you don't need EXTI for buttons.
2023-02-03 08:03 AM
Hello gbm,
Yes, that is the sense I'm getting as I look into implementation. Thankfully, switching between polling and interrupts introduce no hardware changes, so I can experiment with both!
Thank you for the information!
2023-02-03 08:54 AM
There is nothing wrong with using interrupts with buttons. See this video I made using timercallback debounce a button and many other useful things
2023-02-03 11:29 AM
@KMew: timer-only solution does not require C+R at the button; EXTI-only requires them, EXTI+timer is like timer, just bigger.
Oh, definitely. If you use EXTI and timer interrupt you may end with an equally functional solution as in case of using timer interrupt only. The timer+EXTI code will be longer and more complex than timer-only one but at least it proves that EXTI can be successfully used for buttons. ;)
Ok, kidding, I may also describe a case where EXTI is reasonable. It's just it's not a very common case.
2023-02-19 03:01 PM
By using EXTI, the code can avoid any polling and running a timer during the periods, when no buttons are pressed. Saves some CPU, energy and doesn't wake up the MCU unnecessarily, which can be very important for battery operated devices.
2023-02-19 06:31 PM
What about a scan matrix, say, 20 buttons (4 x 5) - what do you recommend?
2023-02-21 02:28 PM
It all depends on how many other things are going on like other peripheral interrupts. If you don't have much going on then you can use polling but you can also use EXTI as well.