DHT11编程

实验:用数码管显示温湿度

dht11.h

#ifndef _DHT11_H
#define _DHT11_H
#include "stm32f10x_conf.h"

extern void dht11_init(void);
extern void Get_Dht_Value(char *buf);
#endif

dht11.c

#include"dht11.h"
#include"bitband.h"
#include"delay.h"

void set_dht_out(void)
{
	GPIO_InitTypeDef Gpio_Value;//初始化
	Gpio_Value.GPIO_Mode=GPIO_Mode_Out_PP;//指定模式-推免输出
	Gpio_Value.GPIO_Pin=GPIO_Pin_10;//10号引脚
	Gpio_Value.GPIO_Speed=GPIO_Speed_50MHz;//设置接口速度
	GPIO_Init(GPIOC	,&Gpio_Value);	
}

void set_dht_in(void)
{
	GPIO_InitTypeDef Gpio_Value;//初始化
	Gpio_Value.GPIO_Mode=GPIO_Mode_IPU;//上拉输入模式
	Gpio_Value.GPIO_Pin=GPIO_Pin_10;//10号引脚
	GPIO_Init(GPIOC,&Gpio_Value);	
}

void dht_out_status(int n)
{
	set_dht_out();
	if(n==1)
		PCOut (10)=1;
	else
		PCOut (10)=0;
}

int dht_in_status(void)
{
	set_dht_in();
	return PCIn(10);
}


void dht11_init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
}


void Get_Dht_Value(char *buf)
{
	int ret=0,i=0;
	int times=0;
	u8 data=0;
	dht_out_status(1);//拉高数据线
	dht_out_status(0);//拉低数据线
	delay_ms(20);//至少拉低18ms
	dht_out_status(1);//拉高数据线
	do{    //第一次等用超时处理
		ret=dht_in_status();
		times++;
		delay_us(2);//延时2微秒
	}while(ret==1&&times<=20);//最高延时40us
	if(times>20)
		return  ;
	while(!dht_in_status());//死等拉高
	for(i=0;i<40;i++)
	{
		while(dht_in_status());//死等拉低
	  while(!dht_in_status());//死等拉高
		delay_us(40);
		data<<=1;
		if(dht_in_status()==1)//判断获得的是高电平哥还是低点平	
		{
			data|=1;
		}
	 if((i+1)%8==0)//连续转存
	 {
		 buf[i/8]=data;
		 data=0;
	 }
	}
	dht_out_status(1);//拉高数据线
}

main.c

#include"iwdg.h"
#include"led.h"
#include"fmq.h"
#include"key.h"
#include"delay.h"
#include"inte.h"
#include"hc138.h"
#include"dht11.h"
void h0(void)
{
	Led_On(0);
	Led_On(1);
	Led_On(2);
	
}
void h1(void)
{
	Fmq_On();
}
void h2(void)
{
	Led_Off(0);
	Fmq_Off();
	
}

int main(void)
{
	char buff[5]={0};
	int dht_data=0;
	int i=0,j=0;
	inte_init();
	Led_Init();	
	Key_Init();
	Fmq_Init();
	delay_init();
	iwdg_init(5);
	hc138_init();
	dht11_init();
	
	set_inte_handler(h0,h1,h2);
	while(1)
	{
		Get_Dht_Value(buff);
		dht_data=buff[0]*100+buff[2];
		show_digital_Out(dht_data);
		
	}
	return 0;
}

运行截图

前两位为湿度,后两位为温度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值