#include <LiquidCrystal.h>
#define commPin 7 // Connect the Communications pin on the module to pin 7 on the Arduino
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Temp & Humidity");
}
void loop()
{
uint8_t data[3]; // We will use this to store and array of the data
uint8_t bitCount = 7; // As data is sent with the most significant bit first we start at the end
uint8_t byteCount = 0; // Keep count of the Bytes
unsigned long t = 0; // Used to time the data pulse
char str[512]; // We will use this to construct a string to send to the Serial port with the readings
for (int i=0; i< 4; i++) data[i] = 0; // Empty the data array
// First we need to tell the module we want it to take a reading, This is done by pulling the
// communications pin low for 18mS, then high for 40uS.
pinMode(commPin, OUTPUT);
digitalWrite(commPin, LOW);
delay(18);
digitalWrite(commPin, HIGH);
delayMicroseconds(40);
pinMode(commPin, INPUT);
// When the module is ready to send its data it will pull the communications pin low then high
while(digitalRead(commPin) == LOW)
delayMicroseconds(10);
while(digitalRead(commPin) == HIGH)
// The module will now start sending 40 bits of data, the first Byte is the Humidity, the second is just null,
// the third is the Temperature, the fourth is null and the 5th is a check sum
// For simplicity we will just look at the first 3 Bytes.
for (int i=0; i<24; i++)
{
while(digitalRead(commPin) == LOW) // Wait for the High pulse to start
t = micros(); // Use variable t to store the time when the high pulse starts.
while(digitalRead(commPin) == HIGH) // Wait for the High pulse to end
// if we now do micros() -t we know how long the High pulse lasted, around 28uS represents a '0' and
// around 70uS represents a '1'. To allow some error in timing we can simply say that if the High Pulse
// is longer than 49uS then it is a '1' and if it is shorter then it is a '0'
if ((micros() - t) > 49) data[byteCount] |= (1 << bitCount);
if (bitCount == 0) // If the bitCount has reached 0 then we are on to the net Byte
{
bitCount = 7; // Reset the bitCount to the MSB
byteCount++; // On to the next Byte
}
else bitCount--; // bitCount not yet 0 so subtract 1
}
int humidity = data[0]/3.75;
int temperature = data[2];
sprintf(str, "%d \t %d", humidity, temperature); // Construct a string out of all three readings
Serial.println(str); // Print it to the serial link
delay(2000); // This is not a quick sensor so give it time to rest :)
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(temperature);
lcd.print("oC ");
lcd.print(humidity);
lcd.print("% ");
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MATLAB.rar (5个子文件)
Serial222.m 682B
sernse data temp_seial.m 3KB
Serial4444.m 743B
Serial333.asv 753B
final.m 757B
共 5 条
- 1
资源评论
Kinonoyomeo
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功