STM32F103(纯代码)-温湿度传感器Dht11

1.Dht11.h

#ifndef __Dht11_H
#define __Dht11_H

#include "Delay.h"
#include "stm32f10x.h"                  // Device header

#define DHT11_RCC RCC_APB2Periph_GPIOA
#define DHT11_PORT GPIOA
#define DHT11_IO GPIO_Pin_6

extern u8 humiInterget;
extern u8 tempInterget;
extern u8 humiDecimal;
extern u8 tempDecimal;

void DHT11_IO_OUT(void);
void DHT11_IO_IN(void);
void DHT11_RST(void);
u8 DHT11_Check(void);
u8 DHT11_Init(void);
u8 DHT11_Read_Bit(void);
u8 DHT11_Read_Byte(void);
u8 DHT11_Read_Data(u8 *humiInterget,u8 *tempInterget,u8 *humiDecimal,u8 *tempDecimal);


#endif

2.Dht11.c

#include "Dht11.h"

u8 humiInterget;
u8 tempInterget;
u8 humiDecimal;
u8 tempDecimal;

void DHT11_IO_OUT(void)
{ 
	GPIO_InitTypeDef  GPIO_InitStructure; 	
  GPIO_InitStructure.GPIO_Pin = DHT11_IO;                  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
	GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
}

void DHT11_IO_IN(void)
{ 
	GPIO_InitTypeDef  GPIO_InitStructure; 	
  GPIO_InitStructure.GPIO_Pin = DHT11_IO;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿ÕÊäÈë    
	GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
}

//¶Ë¿Ú¸´Î» ·¢³ö³õʼÐźÅ
void DHT11_RST(void)
{ 						
	DHT11_IO_OUT();		//IOΪÊä³ö״̬
	GPIO_ResetBits(DHT11_PORT,DHT11_IO);	//À­×ÜÏßΪµÍµçƽ	
	Delay_ms(20); 											
	GPIO_SetBits(DHT11_PORT,DHT11_IO);		//À­×ÜÏßΪ¸ßµçƽ
	Delay_us(30); 							
}

//µÈ´ýdht11»ØÓ¦ ·µ»Ø1£ºÎ´¼ì²âµ½dht11 ·µ»Ø0£º³É¹¦ (IO½ÓÊܳɹ¦)
u8 DHT11_Check(void)
{ 	 
	u8 retry = 0;			
	DHT11_IO_IN();		//IOΪÊäÈë״̬
	
	while ((GPIO_ReadInputDataBit(DHT11_PORT,DHT11_IO) == 1) && retry < 100)
	{
		retry++;
    Delay_us(1);
  }
	
  if(retry>=100)return 1; 	
	else retry=0;
	 
	while ((GPIO_ReadInputDataBit(DHT11_PORT,DHT11_IO) == 0) && retry < 100)
	{  
    retry++;
		Delay_us(1);
  }
	
	if(retry>=100)return 1;	    
  return 0;
	
}

u8 DHT11_Init(void)
{
	
	RCC_APB2PeriphClockCmd(DHT11_RCC,ENABLE);
	DHT11_RST();		//¶Ë¿Ú¸´Î» ·¢³ö³õʼÐźÅ						
	return DHT11_Check(); 	//µÈ´ýdht11»ØÓ¦	
	
}

u8 DHT11_Read_Bit(void)
{
    u8 retry = 0;
    while((GPIO_ReadInputDataBit(DHT11_PORT,DHT11_IO) == 1) && retry < 100)
    {
        retry++;
        Delay_us(1);
    }
    retry = 0;
    while((GPIO_ReadInputDataBit(DHT11_PORT,DHT11_IO) == 0) && retry < 100)
    {
        retry++;
        Delay_us(1);
    }
    Delay_us(40);
    if(GPIO_ReadInputDataBit(DHT11_PORT,DHT11_IO) == 1)
        return 1;
    else
        return 0;
}

u8 DHT11_Read_Byte(void)
{
    u8 i, dat;
    dat = 0;
    for (i = 0; i < 8; i++)
    {
        dat <<= 1;
        dat |= DHT11_Read_Bit();
    }
    return dat;
}



u8 DHT11_Read_Data(u8 *humiInterget,u8 *tempInterget,u8 *humiDecimal,u8 *tempDecimal)
{
	u8 buf[5];
  u8 i;
  DHT11_RST();	   					
	if(DHT11_Check() == 0)				
  {
    for(i = 0; i < 5; i++) 			
    {
      buf[i] = DHT11_Read_Byte();	
    }
		if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
    {
      *humiInterget = buf[0];	
			*humiDecimal	= buf[1];
      *tempInterget = buf[2];	
			*tempDecimal	= buf[3];
    }

   }else return 1;

    return 0;
}


3.main

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "sys.h"
#include "stdio.h"
#include "string.h"

#include "Led.h"	
#include "Key.h"
#include "Beep.h"
#include "Usart.h"
#include "Adc.h"
#include "OLED.h"

#include "Dht11.h"

int main(void)
{
	Led_Init();
	Key_Init();
	Beep_Init();

	Usart1_Init(115200);
	
	DHT11_Init();
	
	while(1)
	{
		DHT11_Read_Data(&tempInterget,&tempDecimal,&humiInterget,&humiDecimal);
		printf("temp: %d.%d	humi: %d.%d \r\n",tempInterget,tempDecimal,humiInterget,humiDecimal);
		Delay_ms(3000);
	}

}

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值