zoukankan      html  css  js  c++  java
  • GPIO 输入—按键检测

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

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

    /**
      * @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。

  • 相关阅读:
    学习认识Spring原理
    JavaScript
    JSP----九大内置对象
    人到底可以有怎样的意志力,人到底可能有怎样的发挥潜力? ----问自己
    将web项目打成war包部署在tomcat步骤
    微信企业公众号开发之回调模式
    eclipse/myeclipse 中的一些常用的快捷键
    mycelipse中关于编码的配置
    Ext学习系列(1)初识Ext
    AngularJs编写指令
  • 原文地址:https://www.cnblogs.com/yangguang-it/p/6718931.html
Copyright © 2011-2022 走看看