#include <Wire.h>
const int address = 0x60;
const float Tempconst = -5.35;
//温度係数
float Temp;
//算出温度格納変数
float read_coefficients(int total_bits, int fractional_bits, int
zero_pad) {
unsigned char msb, lsb;
msb = Wire.read();
lsb = Wire.read();
return ((float) ((msb << 8) + lsb) / ((long)1 << 16 - total_bits +
fractional_bits + zero_pad));
}
unsigned int read_adc() {
unsigned char msb, lsb;
msb = Wire.read();
lsb = Wire.read();
return (((unsigned int)msb << 8) + lsb) >> 6;
}
float a0, b1, b2, c12, c11, c22;
void setup () {
Wire.begin();
Serial.begin(9600);
Wire.beginTransmission(address);
Wire.write(0x04); // Read coefficient data
Wire.endTransmission();
Wire.requestFrom(address, 12); // Request
12 bytes
if (Wire.available()) {
a0 = read_coefficients(16, 3,
0);
b1 = read_coefficients(16, 13,
0);
b2 = read_coefficients(16, 14,
0);
c12 = read_coefficients(14, 13,
9);
c11 = read_coefficients(11, 10,
11);
c22 = read_coefficients(11, 10,
15);
}
}
void loop () {
Wire.beginTransmission(address);
Wire.write(0x12); // Start both
conversions(Pressure and Temperature)
Wire.write(0x01);
Wire.endTransmission();
delay(5);
Wire.beginTransmission(address);
Wire.write((uint8_t)0x00); //
Read pressure and temperature
Wire.endTransmission();
Wire.requestFrom(address, 4);
// Request 4 bytes
if(Wire.available()) {
unsigned int Padc =
read_adc();
unsigned int Tadc =
read_adc();
float Pcomp = a0 + (b1 +
c11 * Padc + c12 * Tadc) * Padc + (b2 + c22 * Tadc) * Tadc;
float Pha = Pcomp * 650 /
1023 + 500;
Temp
= ((int)Tadc - 605)/Tempconst; //温度の計算
Serialprint("T=");
Serialprint(Temp);
Serialprint("h=");
Serial.println(Pha);
}
delay(1000);
}
|