CC2530+DHT11测温度

DHT11是一款有已校准数字信号输出的温湿度传感器。 其精度湿度+-5%RH, 温度+-2℃,量程湿度20-90%RH, 温度0~50℃。


 

data线连接P0_0

  • DHT11.h
#include 
#define uint unsigned int
#define uchar unsigned char
#define DATA_PIN P0_0

//温湿度定义

uchar ucharFLAG,uchartemp;
uchar shidu_shi,shidu_ge,wendu_shi,wendu_ge=4;
uchar ucharT_data_H,ucharT_data_L,ucharRH_data_H,ucharRH_data_L,ucharcheckdata;
uchar ucharT_data_H_temp,ucharT_data_L_temp,ucharRH_data_H_temp,ucharRH_data_L_temp,ucharcheckdata_temp;
uchar ucharcomdata;

//延时函数
void Delay_us() //1 us 延时
{
asm("nop");//如果太多,很有可能校验通不过
asm("nop");
asm("nop");
asm("nop");
}

void Delay1_10us() //10 us 延时
{
Delay_us();
Delay_us();
}

void Delay_ms(uint Time)//n ms 延时
{
unsigned char i;
while(Time--)
{
for(i=0;i<100;i++)
Delay1_10us();
}
}

//温湿度传感
void COM(void) // 温湿写入
{
uchar i;
for(i=0;i<8;i++)
{
ucharFLAG=2;
while((!DATA_PIN)&&ucharFLAG++);
Delay1_10us();
Delay1_10us();
Delay1_10us();
uchartemp=0;
if(DATA_PIN)uchartemp=1;
ucharFLAG=2;
while((DATA_PIN)&&ucharFLAG++);
if(ucharFLAG==1)break;
ucharcomdata<<=1;
ucharcomdata|=uchartemp;
}
}

void DHT11(void) //温湿传感启动
{
DATA_PIN=0;
Delay_ms(30); //>18MS
DATA_PIN=1;
P0DIR &= ~0x01; //重新配置 IO 口方向 输入
Delay1_10us();
Delay1_10us();
Delay1_10us();
Delay1_10us();
if(!DATA_PIN)
{
ucharFLAG=2;
while((!DATA_PIN)&&ucharFLAG++);
ucharFLAG=2;
while((DATA_PIN)&&ucharFLAG++);
COM();
ucharRH_data_H_temp=ucharcomdata;
COM();
ucharRH_data_L_temp=ucharcomdata;
COM();
ucharT_data_H_temp=ucharcomdata;
COM();
ucharT_data_L_temp=ucharcomdata;
COM();
ucharcheckdata_temp=ucharcomdata;
DATA_PIN=1;
uchartemp=(ucharT_data_H_temp+ucharT_data_L_temp+ucharRH_data_H_temp+ucharRH_data_L_temp);

if(uchartemp==ucharcheckdata_temp)
{
ucharRH_data_H=ucharRH_data_H_temp;
ucharRH_data_L=ucharRH_data_L_temp;
ucharT_data_H=ucharT_data_H_temp;
ucharT_data_L=ucharT_data_L_temp;
ucharcheckdata=ucharcheckdata_temp;
wendu_shi=ucharT_data_H/10;
wendu_ge=ucharT_data_H%10;
shidu_shi=ucharRH_data_H/10;
shidu_ge=ucharRH_data_H%10;
}
else //没用成功读取,重新运行函数
{
  DHT11();
}
}
P0DIR |= 0x01; //IO 口需要重新配置
}




 

调用函数getWS()即可得到当前的温湿度。

  • DHT11.c
#include "DHT11.h"
#include 
#include 

#define uint unsigned int
#define uchar unsigned char
char temp_humidity[4]; 
extern char* getWS(void);
void DelayMS(uint msec)
{
uint i,j;
for (i=0; i

 

你可能感兴趣的:(CC2530)