0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ You might look at the following two links: #1, #2. You can debounce up to 8 PB switches at once if you arrange them all on a single I/O port (with 6 or more I/O pins associated with it.) Those examples use a timer interrupt, which comparatively simple to implement. You should get comfortable with them. Some people use an interrupt-on-change and will trigger "early" but insert a delay afterwards -- I consider these to be dangerous and fraught with difficulties. But they are an option. \$\endgroup\$ – jonk Apr 27 at 5:04
2
\$\begingroup\$

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/

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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