看到论坛里小女子和老男人写的DHT11的程序,也发一个我写的DHT21的驱动程序吧。用MEGA
//***********************************************************************************************//DHT21.c DHT21 bus module
//2010,4,8
//CompilerWINAVR 20100110
//Copyright By zsmbj
//
//**********************************************************************************************
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
//----------------------------------------------------------------------------------------------
// DHT21 Hardware IO defination
//----------------------------------------------------------------------------------------------
//DDR PORT
//0 0 in,high
//0 1 in,r
//1 0 out=0
//1 1 out=1
#define DHT_DAT PINB0
#define DHT_DAT_HIGH() ( DDRB &= ~(1<<DHT_DAT) )
#define DHT_DAT_LOW() ( DDRB |= (1<<DHT_DAT) )
#define IS_DHT_DAT_High() ( PINB & (1<<DHT_DAT) )
//----------------------------------------------------------------------------------------------
//Name: void DHT_Delay(void)
//Parameters: CLK_WIDTHdelay time
//Function: delay us time with nop
//return :none
//Notice: none
//Created: 2010,4,8 zsmbj
//----------------------------------------------------------------------------------------------
void DHT_Delay(unsigned char us)
{
_delay_us(us);
}
//----------------------------------------------------------------------------------------------
//Name: void Init_DHT21(void)
//Parameters: none
//Function: Init_DHT21 bus
//return : none
//Notice: none
//Created: 2010,4,8 zsmbj
//----------------------------------------------------------------------------------------------
void Init_DHT21(void)
{
DHT_DAT_HIGH();
}
//----------------------------------------------------------------------------------------------
//Name: unsigned char Wait_RISE_EDGE(unsigned char* temp)
//Parameters: none
//Function:
//return : none
//Notice: none
//Created: 2010,4,8 zsmbj
//----------------------------------------------------------------------------------------------
unsigned char Wait_RISE_EDGE(unsigned char* temp)
{
unsigned char flag=0,i=0,ack=0;
while(1)
{
if( !(IS_DHT_DAT_High()) )
{
flag = 1;
}
else
{
if(flag == 1)
{
temp = i; //return time :us
break;
}
}
// DHT_Delay(1);
i++;
if(i >= 200) //if wait 200us for nothing then return
{
ack = 1;
break;
}
}
return ack;
}
//----------------------------------------------------------------------------------------------
//Name: unsigned char Wait_FALL_EDGE(unsigned char* temp)
//Parameters: none
//Function:
//return : none
//Notice: none
//Created: 2010,4,8 zsmbj
//----------------------------------------------------------------------------------------------
unsigned char Wait_FALL_EDGE(unsigned char* temp)
{
unsigned char flag=0,i=0,ack=0;
while(1)
{
if( (IS_DHT_DAT_High()) )
{
flag = 1;
}
else
{
if(flag == 1)
{
temp = i; //return time :us
break;
}
}
// DHT_Delay(1);
i++;
if(i >= 200) //if wait 200us for nothing then return
{
ack = 1;
break;
}
}
return ack;
}
//----------------------------------------------------------------------------------------------
//Name: unsigned char Get_DHT_Temp_Humi(unsigned char* temp)
//Parameters: none
//Function:
//return : none
//Notice: none
//Created: 2010,4,8 zsmbj
//
// ----| start | 20-|salve ack(2*80us)| data026-| data1 |stop-------
// ____ _40us_ __80us__ _28us_ __70us__ ____
//DAT : |____500us____| |__80us__| |_50us_| |_50us_| |_50us_|
//----------------------------------------------------------------------------------------------
unsigned char Get_Temp_Humi(unsigned char* temp)
{
unsigned char sum,i,j,ack,data,dat;
//Send start
DHT_DAT_LOW(); //set data low
DHT_Delay(250); //
DHT_Delay(250); //delay 500us
DHT_DAT_HIGH(); //set data high
DHT_Delay(20); //delay 20us
//get ack
ack = Wait_RISE_EDGE(&dat); //senond riseedge,80us,low level
if(ack != 0)
{
return ack;
}
ack += Wait_FALL_EDGE(&dat); //third falledge,80us high level
if(ack != 0)
{
return ack;
}
cli(); //we close interrupt
//get temp and humi 5 byte
for(i = 0;i<5;i++) //we got 40 bit data that is 5 byte
{
data = 0;
for(j = 0;j<8;j++)
{
ack = Wait_FALL_EDGE(&dat);
if(ack != 0)
{
ack = 1;
break;
// return ack;
}
else
{
if( (dat >= 110) && (dat <= 130) ) //bit 1,total 120us; we test is 122us
{
data <<= 1;
data += 1;
}
else if( (dat >= 67) && (dat <= 87) ) //bit 0,total 77us ;we test is 72us
{
data <<= 1;
data += 0;
}
else //an know bit
{
ack = 1;
break;
// return ack;
}
}
}
}
sei(); //we open interrupt
if(ack == 1)
{
return ack;
}
else
{
//calculate sum
sum = data + data + data + data;
if( sum == data )
{
temp = data; //temp msb
temp = data; //temp lsb
temp = data; //humi msb
temp = data; //humi lsb
ack = 0;
}
else
{
temp = 0; //temp msb
temp = 0; //temp lsb
temp = 0; //humi msb
temp = 0; //humi lsb
ack = 1;
}
//Stop
DHT_Delay(50); //delay 50us
DHT_DAT_HIGH(); //set data high
return ack;
}
}
//END OF DHT21.C 奇怪,排版怎么变化了?上传一下文件吧。
点击此处下载 ourdev_590191GFK30X.rar(文件大小:1K) (原文件名:dht21.rar) 累加和数据始终位0,是什么问题? 马克 MARK真好这个我也在做这个 好像和DHT11兼容的 楼主弱弱的问一声。。。不是说8M晶振的最大延时是96us吗 250us不是溢出了吗 不错,支持 谢谢楼主......
页:
[1]