GPIO 输入—按键检测

来源:互联网 发布:组策略隐藏网络配置 编辑:程序博客网 时间:2024/05/22 09:20

这里要用到一定的模电知识。电容两端电压不能突变,电感两端电流不能突变。这里利用了电容的放电延时实现硬件消抖。按键按下会有抖动,波形有毛刺,使得高低电平显现不明显,而按键按下时,电容放电一下,马上又被充电,此时电容两端的电压不会突然变化,这个延时时间恰好可以达到消抖作用。

这里需要使用一个固件库函数:

 

复制代码
/**  * @brief  Reads the specified input port pin.  * @param  GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F405xx/407xx and STM32F415xx/417xx devices  *                      x can be (A..I) to select the GPIO peripheral for STM32F42xxx/43xxx devices.  *                      x can be (A, B, C, D and H) to select the GPIO peripheral for STM32F401xx devices.   * @param  GPIO_Pin: specifies the port bit to read.  *         This parameter can be GPIO_Pin_x where x can be (0..15).  * @retval The input port pin value.  */uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin){  uint8_t bitstatus = 0x00;  /* Check the parameters */  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));  assert_param(IS_GET_GPIO_PIN(GPIO_Pin));  if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)  {    bitstatus = (uint8_t)Bit_SET;  }  else  {    bitstatus = (uint8_t)Bit_RESET;  }  return bitstatus;}
复制代码

 

在第一次使用这个函数的时候,我没有理解到位,一看到定义bitstatus=0x00,就以为返回的是个八位端口的值,其实这个返回值只有0或者1两种结果。

/** 
* @brief GPIO Bit SET and Bit RESET enumeration 
*/ 
typedef enum

Bit_RESET = 0,
Bit_SET
}BitAction;

在这个枚举中,可以知道,读取IO的数据只可能是0或者1。