11164| 6
|
[讨论] 温湿度传感器SI7021代码求教 |
我的SI7021连接UNO,使用如下代码正常测得数据,连接2560后,出现的负值,而且数值一直无变动,请问需要修改哪里? #include <Wire.h> const int ADDR =0x40; int X0,X1,Y0,Y1,Y2,Y3; double X,Y,X_out,Y_out1,Y_out2; void setup() { Serial.begin(9600); Wire.begin(); delay(100); Wire.beginTransmission(ADDR); Wire.endTransmission(); pinMode(13,OUTPUT); } void loop() { /**发送温度测量命令**/ Wire.beginTransmission(ADDR); Wire.write(0xE3); //发送读温度命令 Wire.endTransmission(); Serial.print("Temp");Serial.print("\t");Serial.println("RH"); /**读取温度数据**/ digitalWrite(13,HIGH); //LED亮开始读数据 Wire.requestFrom(ADDR,2); //回传数据 if(Wire.available()<=2); { X0 = Wire.read(); X1 = Wire.read(); X0 = X0<<8; X_out = X0+X1; } /**计算并显示温度**/ X=(175.72*X_out)/65536; X=X-46.85; Serial.print(X);Serial.print("C");Serial.print("\t"); /**发送湿度测量命令**/ Wire.beginTransmission(ADDR); Wire.write(0xE5); Wire.endTransmission(); /**读取湿度数据**/ Wire.requestFrom(ADDR,2); if(Wire.available()<=2); { Y0 = Wire.read();Y2=Y0/100; Y0=Y0%100; Y1 = Wire.read(); Y_out1 = Y2*25600; Y_out2 = Y0*256+Y1; } /**计算并显示湿度**/ Y_out1 = (125*Y_out1)/65536; Y_out2 = (125*Y_out2)/65536; Y = Y_out1+Y_out2; Y=Y-6; Serial.print(Y);Serial.println("%"); digitalWrite(13,LOW); delay(1000); } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed