智能热水器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

139 lines
3.4 KiB

#include "dht11.h"
#include "delay.h"
void DHT11_IO_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ʹ<EFBFBD><EFBFBD>PB<EFBFBD>˿<EFBFBD>ʱ<EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PG11<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>IO<EFBFBD><EFBFBD>
}
void DHT11_IO_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ʹ<EFBFBD><EFBFBD>PB<EFBFBD>˿<EFBFBD>ʱ<EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PG11<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>IO<EFBFBD><EFBFBD>
}
//<EFBFBD><EFBFBD>λDHT11
void DHT11_Rst(void)
{
DHT11_IO_OUT(); //SET OUTPUT
DHT11_DQ_OUT=0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>DQ54<EFBFBD><EFBFBD>ˮ<EFBFBD><EFBFBD>
delay_ms(20); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>18ms
DHT11_DQ_OUT=1; //DQ=1
delay_us(30); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>20~40us
}
//<EFBFBD>ȴ<EFBFBD>DHT11<EFBFBD>Ļ<EFBFBD>Ӧ
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1:δ<EFBFBD><EFBFBD>⵽DHT11<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
u8 DHT11_Check(void)
{
u8 retry=0;
DHT11_IO_IN();//SET INPUT
while (DHT11_DQ_IN&&retry<100)//DHT11<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>40~80us
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_IN&&retry<100)//DHT11<EFBFBD><EFBFBD><EFBFBD>ͺ<EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>40~80us
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
return 0;
}
//<EFBFBD><EFBFBD>DHT11<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD>λ
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>1/0
u8 DHT11_Read_Bit(void)
{
u8 retry=0;
while(DHT11_DQ_IN&&retry<100)//<EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>͵<EFBFBD>ƽ
{
retry++;
delay_us(1);
}
retry=0;
while(!DHT11_DQ_IN&&retry<100)//<EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ
{
retry++;
delay_us(1);
}
delay_us(40);//<EFBFBD>ȴ<EFBFBD>40us
if(DHT11_DQ_IN)return 1;
else return 0;
}
//<EFBFBD><EFBFBD>DHT11<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
u8 DHT11_Read_Byte(void)
{
u8 i,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
//<EFBFBD><EFBFBD>DHT11<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//temp:<EFBFBD><EFBFBD>ֵ(<EFBFBD><EFBFBD>Χ:0~50<EFBFBD><EFBFBD>)
//humi:ʪ<EFBFBD><EFBFBD>ֵ(<EFBFBD><EFBFBD>Χ:20%~90%)
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>0,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>;1,<EFBFBD><EFBFBD>ȡʧ<EFBFBD><EFBFBD>
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==0)
{
for(i=0;i<5;i++)//<EFBFBD><EFBFBD>ȡ40λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
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;
}
//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>DHT11<EFBFBD><EFBFBD>IO<EFBFBD><EFBFBD> DQ ͬʱ<EFBFBD><EFBFBD><EFBFBD>DHT11<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
u8 DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ʹ<EFBFBD><EFBFBD>PB<EFBFBD>˿<EFBFBD>ʱ<EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PG11<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>IO<EFBFBD><EFBFBD>
GPIO_SetBits(GPIOB,GPIO_Pin_15); //PG11 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
DHT11_Rst(); //<EFBFBD><EFBFBD>λDHT11
return DHT11_Check();//<EFBFBD>ȴ<EFBFBD>DHT11<EFBFBD>Ļ<EFBFBD>Ӧ
}