/***************STM32F103C8T6**********************
 * ļ  DHT11.c
 *     DHT11
 * ע    : DHT11¶ʪȴ
 * ӿ    PA11-DATA

********************LIGEN*************************/

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

#define DT GPIO_Pin_0
      
//λDHT11
void DHT11_Rst(void)	   
{                 
	DHT11_IO_OUT(); 	//SET OUTPUT
	DHT11_DQ_OUT(0); 	//DQ
	DelayXms(20);    	//18ms
	DHT11_DQ_OUT(1); 	//DQ=1 
	DelayUs(30);     	//20~40us
}

//ȴDHT11ĻӦ
//1:δ⵽DHT11Ĵ
//0:
uint8_t DHT11_Check(void) 	   
{   
	uint8_t retry=0;
	DHT11_IO_IN();//SET INPUT	 
    while (DHT11_DQ_IN&&retry<100)//DHT1140~80us
	{
		retry++;
		DelayUs(1);
	};	 
	if(retry>=100)return 1;
	else retry=0;
    while (!DHT11_DQ_IN&&retry<100)//DHT11ͺٴ40~80us
	{
		retry++;
		DelayUs(1);
	};
	if(retry>=100)return 1;	    
	return 0;
}

//DHT11ȡһλ
//ֵ1/0
uint8_t DHT11_Read_Bit(void) 			 
{
 	uint8_t retry=0;
	while(DHT11_DQ_IN&&retry<100)//ȴΪ͵ƽ
	{
		retry++;
		DelayUs(1);
	}
	retry=0;
	while(!DHT11_DQ_IN&&retry<100)//ȴߵƽ
	{
		retry++;
		DelayUs(1);
	}
	DelayUs(40);//ȴ40us
	if(DHT11_DQ_IN)return 1;
	else return 0;		   
}

//DHT11ȡһֽ
//ֵ
uint8_t DHT11_Read_Byte(void)    
{        
	uint8_t i,dat;
	dat=0;
	for (i=0;i<8;i++) 
	{
		dat<<=1; 
		dat|=DHT11_Read_Bit();
	}						    
	return dat;
}

//DHT11ȡһ
//temp:¶ֵ(Χ:0~50)
//humi:ʪֵ(Χ:20%~90%)
//ֵ0,;1,ȡʧ
uint8_t DHT11_Read_Data(uint8_t *temp,uint8_t *humi)    
{        
 	uint8_t buf[5];
	uint8_t i;
	DHT11_Rst();
	if(DHT11_Check()==0)
	{
		for(i=0;i<5;i++)//ȡ40λ
		{
			buf[i]=DHT11_Read_Byte();
		}
		if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
		{
			*humi=buf[0];
			*temp=buf[2];
		}
	}
	else return 1;
	return 0;	    
}

//ʼDHT11IO DQ ͬʱDHT11Ĵ
//1:
//0:    	 
uint8_t DHT11_Init(void)
{	 
 	GPIO_InitTypeDef  GPIO_InitStructure;	
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	 //ʹPG˿ʱ
 	GPIO_InitStructure.GPIO_Pin = DT;				 //PG11˿
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_Init(GPIOA, &GPIO_InitStructure);				 //ʼIO
 	GPIO_SetBits(GPIOA,DT);						 //PG11 
			    
	DHT11_Rst();  //λDHT11
	return DHT11_Check();//ȴDHT11ĻӦ
} 

