Read a single pin.
There is no need for de-bounce code, it is assume the buttons in use comes with a hardware de bounce circuit, however a 50ms delay is used to established a decent polling time and avoid querying the button to often or losing a press action
#include "bsp.h"
int main(void)
{
GPIO_InitTypeDef GPIO_InitStruct; /*gpios initial structure*/
uint32_t PinState;
HAL_Init(); /*Init HAL library*/
__HAL_RCC_GPIOB_CLK_ENABLE(); /*Enable clock on port B*/
GPIO_InitStruct.Pin = GPIO_PIN_5; /*pin to set as output*/
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; /*input mode*/
GPIO_InitStruct.Pull = GPIO_NOPULL; /*no pull-up niether pull-down*/
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; /*pin speed*/
/*use the previous parameters to set configuration on pin B5*/
HAL_GPIO_Init( GPIOB, &GPIO_InitStruct );
while(1)
{
/*Read the pin B5*/
PinState = HAL_GPIO_ReadPin( GPIOB, GPIO_PIN_5 );
if( PinState == RESET )
{
/*the pin is set to low, do something*/
}
/*this is just a delay to avoid polling the pin to often*/
HAL_Delay( 50 );
}
}