HC-SR501传感器接入STM32F10x后始终显示0,求排查问题
HC-SR501运动传感器始终输出0的排查方案
硬件排查步骤
- 电源验证:HC-SR501需5V供电,用万用表确认开发板5V引脚输出正常,传感器GND与开发板GND可靠连接,无虚焊或接触不良。
- 输出引脚检测:直接测量传感器OUT引脚电平,触发运动时观察是否能跳转为高电平(3.3V/5V均可,STM32F10x兼容5V输入)。同时确认OUT引脚确实连接到代码中读取的GPIOA_Pin_0。
- 传感器参数调节:旋转传感器上的灵敏度电位器,适当调高检测灵敏度;传感器通电后需约1分钟初始化时间,等待完成后再测试运动检测。
- 硬件替换:更换同型号HC-SR501传感器,排除传感器本身损坏的可能。
代码逻辑排查
- GPIO配置合理性:代码中将GPIOA_Pin_0配置为
GPIO_Mode_IN_FLOATING(浮空输入),若传感器输出低电平,浮空输入可能受环境干扰。建议改为GPIO_Mode_IPU(上拉输入)或GPIO_Mode_IPD(下拉输入)测试,匹配传感器输出特性。 - 读取逻辑验证:主循环中通过
GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)轮询电平,但代码中无延时,LCD刷新过快可能掩盖瞬间变化。可添加短延时(比如delay(100000)),或改为串口打印电平值,排除LCD显示的干扰。 - 冗余代码清理:代码中保留了EXTI中断相关配置和
EXTI9_5_IRQHandler函数,但EXTI_Configure()被注释,这部分代码完全未生效,建议删除或启用(若想用中断检测运动)。若启用中断,需注意:- 取消
//EXTI_Configure();注释 - 确认传感器OUT引脚连接到GPIOE_Pin_8(代码中EXTI配置的引脚),而非GPIOA_Pin_0
- 调整EXTI触发方式为
EXTI_Trigger_Rising(传感器检测到运动时输出高电平,上升沿触发)
- 取消
- 引脚冲突检查:确认GPIOA_Pin_0未被其他外设初始化占用,代码中ADC使用的是PC2(ADC通道12),无冲突,但需核对开发板原理图。
测试用极简代码
若怀疑LCD显示问题,可先使用以下极简代码验证STM32是否能正确读取传感器电平:
#include "stm32f10x.h" #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" void RCC_Configure(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); } void GPIO_Configure(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void USART1_Init(void) { // 初始化USART1用于打印,根据自己的开发板配置 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; // TX引脚配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // USART配置 USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } void USART_SendChar(USART_TypeDef* USARTx, char ch) { while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); USART_SendData(USARTx, (uint16_t)ch); } void USART_SendString(USART_TypeDef* USARTx, char* str) { while(*str) { USART_SendChar(USARTx, *str++); } } void delay(uint32_t t) { for(uint32_t i=0; i<t; i++); } int main(void) { SystemInit(); RCC_Configure(); GPIO_Configure(); USART1_Init(); while(1) { uint32_t inputData = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0); if(inputData) { USART_SendString(USART1, "Motion detected!\r\n"); } else { USART_SendString(USART1, "No motion!\r\n"); } delay(1000000); } }
用户原始代码
#include <stdbool.h> #include "stm32f10x.h" #include "stm32f10x_exti.h" #include "stm32f10x_gpio.h" #include "stm32f10x_usart.h" #include "stm32f10x_rcc.h" #include "core_cm3.h" #include "stm32f10x_adc.h" #include "lcd.h" #include "touch.h" #include "misc.h" /* function prototype */ void RCC_Configure(void); void GPIO_Configure(void); void USART1_Init(void); void USART2_Init(void); void NVIC_Configure(void); void EXTI9_5_IRQHandler(void); uint16_t value, x, y; bool motionDetected = false; int color[12] = {WHITE, CYAN, BLUE, RED, MAGENTA, LGRAY, GREEN, YELLOW, BROWN, BRRED, GRAY}; //--------------------------------------------------------------------------------------------------- void RCC_Configure(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); } void GPIO_Configure(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void ADC_Configure(void) { ADC_InitTypeDef ADC_12; ADC_12.ADC_ContinuousConvMode = ENABLE; ADC_12.ADC_DataAlign = ADC_DataAlign_Right; ADC_12.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_12.ADC_Mode = ADC_Mode_Independent; ADC_12.ADC_NbrOfChannel = 1; ADC_12.ADC_ScanConvMode = DISABLE; ADC_Init(ADC1, &ADC_12); ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_239Cycles5); ADC_ITConfig(ADC1,ADC_IT_EOC, ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); ADC_SoftwareStartConvCmd(ADC1, ENABLE); } void EXTI_Configure(void) { EXTI_InitTypeDef EXTI_InitStructure; GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource8); EXTI_InitStructure.EXTI_Line = EXTI_Line8; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); } void NVIC_Configure(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // need modify NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; // TODO NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0; // TODO NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void ADC1_2_IRQHandler() { if(ADC_GetITStatus(ADC1,ADC_IT_EOC) != RESET) { value = ADC_GetConversionValue(ADC1); } ADC_ClearITPendingBit(ADC1, ADC_IT_EOC); } void delay(uint32_t t) { for (int i = 0; i < t; i++) {} } void EXTI9_5_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line8) != RESET) { if (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_8) != Bit_RESET) { if (!motionDetected) { motionDetected = true; } } EXTI_ClearITPendingBit(EXTI_Line8); } } int main(void) { SystemInit(); RCC_Configure(); GPIO_Configure(); ADC_Configure(); //EXTI_Configure(); NVIC_Configure(); LCD_Init(); Touch_Configuration(); Touch_Adjust(); LCD_Clear(WHITE); while (1) { /* if 사운드 < threshold: 블루투스로 전송 IF MOSITION == 1: 블루투스로 전송 */ uint32_t inputData = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0); LCD_ShowNum(0, 0, (uint32_t) inputData, 10, BLACK, WHITE); LCD_ShowNum(80,120,value,4,BLACK,WHITE); if (motionDetected) { motionDetected = false; LCD_ShowString(80, 150, "motion detected", BLACK, WHITE); } else { LCD_ShowString(80, 150, "motion not detected", BLACK, WHITE); } //delay(1000000); } return 0; }
内容的提问来源于stack exchange,提问作者Daulet Suyenish
相关产品推荐
相关产品推荐

