cancel
Showing results for 
Search instead for 
Did you mean: 

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

JMOON.1
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

@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

View solution in original post

3 REPLIES 3
TDK
Guru

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.
Evangelist III

@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
Associate II

Thanks