32的dht11温湿度代码

#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");
	
		
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值