#include "dht11.h"
void dht11_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;//复用模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;//引脚
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;//快速
GPIO_Init(GPIOG, &GPIO_InitStruct);
PGout(9) = 1;
}
//发开始信号,检测响应信号
void start_response(void)
{
uint32_t t;
GPIO_InitTypeDef GPIO_InitStruct;
//输入模式
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;//复用模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;//引脚
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;//快速
GPIO_Init(GPIOG, &GPIO_InitStruct);
//输出20ms低电平
PGout(9) = 0;
delay_us(20000);
//输出30us高电平
PGout(9) = 1;
delay_us(30);
//检测低电平(超时判断)
//等电平结束
t = 0;
while(t<1000)
{
if(PGin(9)==0)
break;
t++;
delay_us(1);
}
//检测高电平(超时判断)
//等高电平结束
t = 0;
while(PGin(9)==0)
{
t++;
delay_us(1);
}
t = 0;
while(PGin(9))
{
t++;
delay_us(1);
}
}
uint8_t read_byte(void)
{
uint8_t d=0;
uint32_t i;
for(i=0; i<8; i++)
{
while(PGin(9)==0);
delay_us(40);
if(PGin(9))
{
d |= 1<(7-i);
}
}
return d;
}
void dht11_read_data(uint8_t *pbuf)
{
uint32_t i;
start_response();
for(i=0; i<5; i++)
{
pbuf[i] = read_byte();
}
if(pbuf[4] == pbuf[3] + pbuf[2] + pbuf[1] + pbuf[0])
printf("read success\r\n");
}
32的dht11温湿度代码
最新推荐文章于 2024-06-27 10:18:52 发布