整理代码

This commit is contained in:
nnhy 2015-12-16 03:11:14 +00:00
parent 26f983cb51
commit f943637664
2 changed files with 12 additions and 13 deletions

View File

@ -43,20 +43,24 @@ bool DHT11::Read(ushort& temp, ushort& humi)
while(DA.ReadInput()); while(DA.ReadInput());
/*开始接收数据*/ /*开始接收数据*/
humi = ReadByte() << 8; byte buf[5];
humi |= ReadByte(); for(int i=0; i<ArrayLength(buf); i++)
buf[i] = ReadByte();
temp = ReadByte() << 8;
temp |= ReadByte();
byte sum= ReadByte();
/*读取结束,引脚改为输出模式*/ /*读取结束,引脚改为输出模式*/
/*主机拉高*/ /*主机拉高*/
DA = true; DA = true;
/*检查读取的数据是否正确*/ /*检查读取的数据是否正确*/
return sum == (humi >> 8) + (humi && 0xFF) + (temp >> 8) + (temp && 0xFF); if(buf[0] + buf[1] + buf[2] + buf[3] != buf[4]) return false;
humi = buf[0] << 8;
humi |= buf[1];
temp = buf[2] << 8;
temp |= buf[3];
return true;
} }
byte DHT11::ReadByte() byte DHT11::ReadByte()
@ -79,10 +83,6 @@ byte DHT11::ReadByte()
temp |= (byte)(0x01 << (7-i)); // 把第7-i位置1 temp |= (byte)(0x01 << (7-i)); // 把第7-i位置1
} }
else //60us后为低电平表示数据“0”
{
temp &= (byte)~(0x01 << (7-i)); // 把第7-i位置0
}
} }
return temp; return temp;
} }

View File

@ -1,7 +1,6 @@
#ifndef _DHT11_H_ #ifndef _DHT11_H_
#define _DHT11_H_ #define _DHT11_H_
#include "I2C.h"
#include "Power.h" #include "Power.h"
// 温湿度传感器 // 温湿度传感器