Electrical – Microcontrollers (STM32) and GPIO with momentary buttons

gpiointerruptsstm32

I have six momentary buttons and an STM32 Discovery board.

I want to check the button presses.

As far as I understand, I create a continuous loop that will check each GPIO consecutively. (I am aware of interrupts, however, I want to use GPIO inputs.)

The momentary button sets the GPIO from low to high and low again after release.
I can see this as an issue if the GPIO pin is not being read at the exact time it is high.

Do I need to do multi threading to keep those pins constantly checked? or are interrupts my only alternative?

Best Answer

That momentary button is not actually momentary. Usually you won't be able to perform a shorter press than ~100ms. Now, your controller is running at some frequency between 1MHz and 1Ghz. Now calculate how often your code will get to the point where it detects the button press (hint: many thousands of times). So, detecting a press is easy.

It gets harder then because you will need to debounce it and you'll need to know if it was pressed again or if it's still the same press from your previous loop iteration.

For debouncing I always use the integral method. Something like Kenneth's code from here: https://hackaday.com/2010/11/09/debounce-code-one-post-to-rule-them-all/