CC2530+DHT11测温度

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


 

data线连接P0_0

  • DHT11.h
#include <ioCC2530.h>
#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 <ioCC2530.h>
#include <string.h>

#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<msec; i++)
for (j=0; j<1070; j++);
}
char* getWS(void){ 
  memset(temp_humidity, 0, 4);
  DHT11();
  temp_humidity[2]=(char)wendu_shi+0x30;//转化为Char类型
  temp_humidity[3]=(char)wendu_ge+0x30;
  temp_humidity[0]=(char)shidu_shi+0x30;
  temp_humidity[1]=(char)shidu_ge+0x30;
  DelayMS(1000); //延时
  return temp_humidity;
}
//temp_humidity[2]和temp_humidity[3]温度char表示
//temp_humidity[0]和temp_humidity[1]湿度char表示

 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值