STM32 GPIO_ReadInputDataBit 的使用(附详细代码 = 引脚底层配置 + 使用)

本文详细介绍了STM32中GPIO引脚作为输入时的底层配置方法及如何读取引脚电平状态。通过示例代码展示了配置过程,并解释了使用`GPIO_ReadInputDataBit`函数的具体作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


介绍:

  • 函数 GPIO_ReadInputDataBit 读的是 GPIOx_IDR
  • 读的是当 IO 口设置为输入状态时候的 IO 口电平状态值。

引脚底层配置:

  • 输入类型:下拉输入
  • 引脚底层配置代码:
    
    void GPIO_DI_Configration(void)
    {
        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 );
    
    

小结

  • 欢迎纠正
  • ☆⌒(*^-゜)v THX!!
  • 码字不易,记得点小心心 ( •̀ ω •́ )✧
uint8_t Key_GetNum(void) { uint8_t KeyNum = 0; if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6) == 0); Delay_ms(20); KeyNum = 1; } if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7) == 0); Delay_ms(20); KeyNum = 2; } if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8) == 0); Delay_ms(20); KeyNum = 3; } if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9) == 0); Delay_ms(20); KeyNum = 4; } if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8) == 0); Delay_ms(20); KeyNum = 5; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0); Delay_ms(20); KeyNum = 6; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) == 0); Delay_ms(20); KeyNum = 7; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0); Delay_ms(20); KeyNum = 8; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15) == 0); Delay_ms(20); KeyNum = 9; } if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10) == 0); Delay_ms(20); KeyNum = 10; } if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_11) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_11) == 0); Delay_ms(20); KeyNum = 11; } if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_12) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_12) == 0); Delay_ms(20); KeyNum = 12; } if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2) == 0); Delay_ms(20); KeyNum = 13; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0); Delay_ms(20); KeyNum = 14; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6) == 0); Delay_ms(20); KeyNum = 15; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) == 0); Delay_ms(20); KeyNum = 16; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8) == 0); Delay_ms(20); KeyNum = 17; } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9) == 0); Delay_ms(20); KeyNum = 18; } if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4) == 0); Delay_ms(20); KeyNum = 19; } if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5) == 0); Delay_ms(20); KeyNum = 20; } return KeyNum; }我觉得该代码有些重复了,帮我优化一下
03-20
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的小肥丸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值