写一段stm32单片机数字量输入代码,选择pb1口输入
时间: 2023-06-08 16:07:10 浏览: 44
您好,以下是一段使用PB1口输入数字量的STM32单片机代码示例:
```c
#include "stm32f4xx.h"
int main(void) {
// Enable the clock for GPIOB
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
// Configure PB1 as input with pull-up
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
while (1) {
// Read the input from PB1
int pb1_input = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1);
// Do something with the input
// ...
// Delay for a short period of time
for (int i = 0; i < 1000000; i++);
}
}
```
请注意,此代码仅提供参考,具体实现应根据具体情况进行修改和测试。感谢您的提问,希望能对您有所帮助!