求助贴,我的DHT11无法接受复位信号
在接受DHT11返回代码部分时一直为高电平 所以超时了 试过换成5v电压,没用 注意到初始信号为低电平,初始化为高电平然后delay了200ms后再继续执行也没用 感谢各位师傅交流与解答[抱拳][抱拳] DHT11代码 #include "stm32f10x.h" // Device header #includeDelay.h#include "LCD.h" //向DHT11发送信号 void DHT11_Rst(void){ // ST7735_ShowNum(99 - 32, 32, GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12), 1); GPIO_ResetBits(GPIOB, GPIO_Pin_12); // ST7735_ShowNum(99 - 32, 0, GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12), 1); Delay_ms(20); GPIO_SetBits(GPIOB, GPIO_Pin_12); // ST7735_ShowNum(99 - 32, 16, GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12), 1); Delay_us(30); } //返回0正常 1为低电平响应超时 2为高电平响应超时 uint8_t DHT11_Check(void){ uint16_t retry = 0; for(int i = 0; i < 100; i ++){ if( GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0){ Delay_us(1); retry++; ST7735_ShowNum(99 - 32, 0, retry, 3); } else{ break; } } if(retry == 100){ //超时 return 1; } retry = 0; for(int i = 0; i < 100; i ++){ if( GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 1){ Delay_us(1); retry++; ST7735_ShowNum(99 - 32 - 16, 0, retry, 3); } else{ break; } } if(retry == 100){ //超时 return 2; } return 0; } uint8_t DHT11_ReadBit(void){ while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0); //等待第一段结束 Delay_us(40); if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0){ return 0; } else{ return 1; } } uint8_t DHT11_ReadByte(void){ uint8_t data; for(int i = 0; i < 8; i++){ data <<= 1; data |= DHT11_ReadBit(); } return data; } void DHT11_ReadData(uint8_t data[5]){ for(int i = 0; i < 5; i++){ data[i] = DHT11_ReadByte(); } if(data[0] + data[1] + data[2] + data[3] + data[4] != data[4]){ data[0] = data[1] = data[2] = data[3] = 0x0; } } uint8_t DHT11_Init(void){ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //一开始置为输出模式用于给DHT11通信 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB, GPIO_Pin_12); Delay_ms(100); DHT11_Rst(); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //改为输入模式等待相应 GPIO_Init(GPIOB, &GPIO_InitStructure); return DHT11_Check(); } main函数 #include "stm32f10x.h" // Device header #includeLCD.h#includeDelay.h#include "DHT11.h" /* 主函数 */ int main(void) { ST7735_Init(); ST7735_FillScreen(0x0000); // 黑屏清屏 ST7735_ShowString(115, 0, "temperature:00.00C"); ST7735_ShowString(99, 0, "humidity:00.00%RH"); int state = DHT11_Init(); ST7735_ShowNum(99 - 16, 0, state, 1); uint8_t data[5]; DHT11_ReadData(data); ST7735_ShowNum(115, 12 * 8, data[0], 2); ST7735_ShowNum(115, 15 * 8, data[1], 2); ST7735_ShowNum(99, 9 * 8, data[2], 2); ST7735_ShowNum(99, 12 * 8, data[3], 2); while(1){ } }
#includeDelay.h#
#includeLCD.h#
硬创社
全部评论 默认 最新
654321kJ711t 作者
2025-11-10 11:39:21 来自四川
好消息,别人的驱动跑成功了,不是元件的问题,我再扒拉扒拉
654321kJ711t 作者
2025-11-10 13:10:19 来自四川
太多shownum导致时序出错了,校验和判定,信号检测哪哪都是问题[哭笑]
已折叠部分评论 展开
没有更多啦~