cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7: Multiple Push Button Interrupts

KMew
Senior III

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.

0693W00000Y9sHFQAZ.png 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Karl Yamashita
Lead II

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.

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.

View solution in original post

9 REPLIES 9
Karl Yamashita
Lead II

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.

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.

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!

gbm
Lead III

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.

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!

There is nothing wrong with using interrupts with buttons. See this video I made using timercallback debounce a button and many other useful things

https://youtu.be/o0qhmXR5LD0

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.
gbm
Lead III

@KMew: timer-only solution does not require C+R at the button; EXTI-only requires them, EXTI+timer is like timer, just bigger.

@Karl Yamashita

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.

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.

Pavel A.
Evangelist III

What about a scan matrix, say, 20 buttons (4 x 5) - what do you recommend?

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.

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.