最近使用DHt11测温度和湿度,结果显示在1602上面,1602的显示看过没有问题,但是
“8bit湿度整数数据 + 8bit湿度小数数据+8bit温度整数数据 + 8bit温度小数数据”所得结果的末8位不等于8bit校验位等于,求各位大神看看程序具体哪里出错了?
#include <msp430x14x.h>
#include "cry1602.c"
///////////////引脚定义///////////////////////
#define DHT11_OUTPUT P2DIR|=BIT0 //P2.0
#define DHT11_INPUT P2DIR&=~BIT0 //P2.0
#define DHT11_H P2OUT|=BIT0 //P2.0
#define DHT11_L P2OUT&=~BIT0 //P2.0
#define DHT11_IN (P2IN&BIT0)
uchar shuzi[] = {"0123456789."};
uchar tem[2];
///////////////DHT11输出数据//////////////////
unsigned char DHT11T_Data_H; //温度高8位
unsigned char DHT11T_Data_L; //温度低8位
unsigned char DHT11RH_Data_H; //湿度高8位
unsigned char DHT11RH_Data_L; //适度低8位
/////////////函数声明///////////////////////
unsigned char start_DHT11(void);
unsigned char DHT11_ReadChar(void);
void dht11(void);
void Delayus(int n);
////////////////////////////////////////////
//函数名称:Delayus
//功能:延时(us),定时器查询方法
//调用:无
//返回:无
///////////////////////////////////////////
void Delayus(int n)
{
CCR0 = n;
TACTL |= TACLR;
TACTL |= MC_1;
while(!(TACTL & BIT0)); //等待
TACTL &= ~MC_1; //停止计数
TACTL &= ~BIT0; //清除中断标志
}
///////////主函数///////////////////
void main(void)
{
unsigned int readflag = 0;
WDTCTL = WDTPW + WDTHOLD;
TACTL|= TASSEL_2 + ID_3; //时钟选择SMCLK,1/8分频,计数器1M频率
unsigned char humidity=0;
unsigned char temperature = 0;
LcdReset(); //显示提示信息
Disp1Char(8,0,'C'); //显示温度单位
while(1)
{
readflag = start_DHT11();
if(1)
{
temperature = DHT11T_Data_H;//温度值,没有考虑小数
humidity = DHT11RH_Data_H; //湿度值,没有考虑小数
tem[0]=temperature/10; //验证是否执行到此
tem[1]=temperature%10;
Disp1Char(2,0,shuzi[tem[0]]);
Disp1Char(3,0,shuzi[tem[1]]);
}
Disp1Char(1,1,shuzi[readflag]); //读出readflag的值
}
}
////////////////////////////////////////////
//函数名称:start_DHT11
//功能:DHT11初始化及读取湿度值和温度值
//调用:DHT11_ReadChar()
//返回:无
///////////////////////////////////////////
unsigned char start_DHT11(void)
{
unsigned char TData_H_temp = 0; //温度高8位中间变量
unsigned char TData_L_temp = 0; //温度低8位中间变量
unsigned char RHData_H_temp =0; //湿度高8位中间变量
unsigned char RHData_L_temp = 0; //湿度低8位中间变量
unsigned char CheckData_temp = 0; //校验位8位中间变量
char checktemp = 0; //校验值
unsigned char flag = 0;
unsigned int count = 0;
DHT11_OUTPUT; //设置P2.0输出 #define DHT11_OUTPUT P2DIR|=BIT0
DHT11_H; //输出高 #define DHT11_H P2OUT|=BIT0
Delayus(10); //延时10us
DHT11_L; //输出低 #define DHT11_L P2OUT&=~BIT0
//拉低18ms以上
Delayus(30000);
DHT11_H; //输出高
DHT11_INPUT; //设置P2.0输入 #define DHT11_INPUT P2DIR&=~BIT0
Delayus(60); //延时60us
if(!(DHT11_IN)) //检测P2.O输入电平,若为低电平
{
count=2; //计数值设置为2
while((!DHT11_IN)&&count++);
count=2;
while((DHT11_IN)&&count++);
RHData_H_temp = DHT11_ReadChar();
RHData_L_temp = DHT11_ReadChar();
TData_H_temp = DHT11_ReadChar();
TData_L_temp = DHT11_ReadChar();
CheckData_temp = DHT11_ReadChar();
DHT11_OUTPUT; //设置P2.0输出
DHT11_H; //P2.0输出高,释放总线
checktemp = (RHData_H_temp + RHData_L_temp + TData_H_temp + TData_L_temp); //判断校验位是否正确
if (checktemp == CheckData_temp)
{
DHT11RH_Data_H = RHData_H_temp;
DHT11RH_Data_L = RHData_L_temp;
DHT11T_Data_H = TData_H_temp;
DHT11T_Data_L = TData_L_temp;
flag=1;
}
}
return flag; //返回flag,若返回1说明读取正确
}
////////////////////////////////////////////
//函数名称:DHT11_ReadChar
//功能:DHT11读取1字节数据
//调用:
//返回:无
///////////////////////////////////////////
unsigned char DHT11_ReadChar(void)
{
unsigned char dat=0;
unsigned int count; //计数防止死等
unsigned char i;
for(i=0;i<8;i++)
{
count=2;
while((!DHT11_IN)&&count++); //等待50us低电平结束,程序进入死循环退出 #define DHT11_IN (P2IN&BIT0)
Delayus(35);
dat <<= 1;
if(DHT11_IN) //50us低电平+28us高电平表示'0',50us低电平+70us高电平表示'1'
{
dat |= 1;
}
count=2;
while((DHT11_IN)&&count++); //程序进入死循环退出
/*while(DHT11_IN);
if(count==1) //超时则跳出for循环
{
break;
}*/
}
return dat;
}
|