SMT32标准库函数——GPIO_ReadInputDataBit的使用(类比HAL库函数:HAL_GPIO_ReadPin函数)

前言

君不见吴中张翰称达生,秋风忽忆江东行。
且乐生前一杯酒,何须生后千载名? ——李白《行路难·其三》

一、介绍

  • 函数 GPIO_ReadInputDataBit 读的是 GPIOx_IDR

  • 读的是当 IO 口设置为输入状态时候的 IO 口电平状态值;

  • 引脚底层配置

  • 输入类型:下拉输入;

  • 引脚底层配置代码:

{
    GPIO_InitTypeDef  GPIO_InitStructure;
    
    /*使能 APB2 - PD端口时钟*/ 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);	 
    
    /*引脚配置*/
    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_8 ;				// 引脚 PD.8 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		// 频率为50MHz
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;       	// 输入类型(下拉输入)	  
	GPIO_Init(GPIOD, &GPIO_InitStructure); 
}

读取引脚电平:

	uint8_t CurrLevel;					//当前电平状态
	
	/*读取引脚当前电平状态*/
	CurrLevel = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_8 );

HAL库函数:GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)

/**
  * @brief  Reads the specified input port pin. 	
  * @param  GPIOx: where x can be (A..G depending on device used) to select the GPIO peripheral
  * @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.
  */
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
  GPIO_PinState bitstatus;

  /* Check the parameters */
  assert_param(IS_GPIO_PIN(GPIO_Pin));

  if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
  {
    bitstatus = GPIO_PIN_SET;
  }
  else
  {
    bitstatus = GPIO_PIN_RESET;
  }
  return bitstatus;
}
  • 功能: 读取指定引脚的电平;
  • 参数一: GPIOx(x = A……E)
  • 参数二: 对应的引脚;
  • 返回值: 当前引脚的电平;

你可能感兴趣的:(单片机,stm32,嵌入式硬件)