本文主要是介绍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表示
这篇关于CC2530+DHT11测温度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!