Skip to main content
JMOON.1
Associate
December 2, 2020
Solved

How do I detect that the button connected to GPIO is still pressed?

  • December 2, 2020
  • 3 replies
  • 1402 views

I'm using 'stm32f103rc'.

I connected the button to the GPIO PIN.

If the button is still pressed, the HIGH signal will continue to occur.

So, how do you detect how long the HIGH signal has been pressed?

Because when the button is pressed for more than 0.5 seconds, you want to generate a specific function.

I lack knowledge of hardware.

This topic has been closed for replies.
Best answer by Pavel A.

@JMOON.1​ You can use HAL_GetTick() to measure time between press and release. Like this:

uint32_t time_press = HAL_GetTick();
...
uint32_t time_release = HAL_GetTick();
if (time_release - time_press > 500 /*ms*/ ) {
 do_something();
}

-- pa

3 replies

TDK
Super User
December 2, 2020

HAL_GPIO_ReadPin will read the status of a pin. Do this periodically (perhaps in an interrupt called every 5ms) and perform logic based on those results.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Pavel A.
Pavel A.Best answer
Super User
December 2, 2020

@JMOON.1​ You can use HAL_GetTick() to measure time between press and release. Like this:

uint32_t time_press = HAL_GetTick();
...
uint32_t time_release = HAL_GetTick();
if (time_release - time_press > 500 /*ms*/ ) {
 do_something();
}

-- pa

JMOON.1
JMOON.1Author
Associate
December 3, 2020

Thanks