Arduino使用SHT2x

#include <Wire.h>

#define SHT2x 0b1000000
#define CMD_TEMP 0b11100011
#define CMD_RH 0b11100101
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Serial.print(getRH());         // print the character
  Serial.println("%");
  delay(500);
  Serial.print(getTempC());
  Serial.println("C");
  delay(500);
}
double getRH()
{
  Wire.beginTransmission(SHT2x);
  Wire.write(CMD_RH);
  Wire.endTransmission();
  
  Serial.println("endTransmission");
  
  Wire.requestFrom(SHT2x, 2);    // request 6 bytes from slave device #2
  unsigned long data=0;
  for(int i=0;i<2&&Wire.available();++i)    // slave may send less than requested
  { 
    unsigned char c = Wire.read(); // receive a byte as character
    data+=(unsigned long)c<<((1-i)*8);
  }
  double RH;
  RH=125.0*(double)data/65536-6.0;
  return RH;
}
double getTempC()
{
  Wire.beginTransmission(SHT2x);
  Wire.write(CMD_TEMP);
  Wire.endTransmission();
  
  Serial.println("endTransmission");
  
  Wire.requestFrom(SHT2x, 2);    // request 6 bytes from slave device #2
  unsigned long data=0;
  for(int i=0;i<2&&Wire.available();++i)    // slave may send less than requested
  { 
    unsigned char c = Wire.read(); // receive a byte as character
    data+=(unsigned long)c<<((1-i)*8);
  }
  double tempC;
  tempC=175.72*(double)data/65536-46.85;
  return tempC;
}

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)

本文链接:https://twd2.me/archives/5483QrCode