#include #include #include /* * MPL115A2を使った気圧/温度測定 * I2Cの部分はGarettLabさんのWebから頂きました。 * http://garretlab.web.fc2.com/index.html * http://garretlab.web.fc2.com/arduino/lab/barometer_sensor/index.html * * by ラジオペンチ */ LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD シールドのピンアサイン const int addresPsensor = 0x60; // 気圧計のI2Cアドレス float a0, b1, b2, c12, c11, c22; float Pha, Temp; int tempOffset, pressOffset; void setup () { unsigned int x; Wire.begin(); Serial.begin(9600); lcd.begin(16, 2); //LCDのサイズ設定 // EEPROM.write(1, 27); // 補正値書き込み // EEPROM.write(3, 75); x = EEPROM.read(0); // 温度の補正値読み出し unit=0.1℃ x = x << 8; tempOffset = x; //上位8ビット x = EEPROM.read(1); tempOffset = tempOffset | x; // 下位8ビット x = EEPROM.read(2); // 気圧の補正値 x = x << 8; pressOffset = x; // 上位 x = EEPROM.read(3); pressOffset = pressOffset | x; // 下位 Serial.print(tempOffset); Serial.print(", "); Serial.println(pressOffset); byte doC[8] = { // 外字で℃を作る B00011, B00011, B01100, B10010, B10000, B10010, B01100, }; lcd.createChar(0, doC); Wire.beginTransmission(addresPsensor); Wire.write(0x04); // Read coefficient data Wire.endTransmission(); Wire.requestFrom(addresPsensor, 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 () { float sumP, sumT; long outP, outT; // 表示用データ int n; sumP = 0; sumT = 0; for(n=0; n<100; n++){ // バラツキが大きいので100回アベレージング sensData(); sumP = sumP + Pha; sumT = sumT + Temp; } outT = int(sumT /10 + 0.5) + tempOffset; // 小数点以下1桁の値に丸める outP = int(sumP /10 + 0.5) + pressOffset; Serial.print(outT); Serial.print(", "); Serial.println(outP); lcd.setCursor(0 , 1); if(outT < 0){ lcd.print("-"); } else { lcd.print(" "); } if(abs(outT)<100){ lcd.print(" "); } lcd.print(outT/10); lcd.print("."); lcd.print(outT % 10); lcd.write(8); // 本当は0を指定すべき(後日修正要す) // lcd.print("C"); lcd.setCursor(7, 1); if(outP < 10000) { lcd.print(" "); } lcd.print(outP/10); lcd.print("."); lcd.print(outP % 10); lcd.print("hPa"); // delay(500); } void sensData () { Wire.beginTransmission(addresPsensor); Wire.write(0x12); // Start both conversions(Pressure and Temperature) Wire.write(0x01); Wire.endTransmission(); delay(5); Wire.beginTransmission(addresPsensor); Wire.write((uint8_t)0x00); // Read pressure and temperature Wire.endTransmission(); Wire.requestFrom(addresPsensor, 4); // Request 4 bytes if(Wire.available()) { unsigned int Padc = read_adc(); unsigned int Tadc = read_adc(); int signedTadc = Tadc; float Pcomp = a0 + (b1 + c11 * Padc + c12 * Tadc) * Padc + (b2 + c22 * Tadc) * Tadc; Pha = Pcomp * 650 / 1023 + 500; Temp = 25 - (signedTadc - 472) / 5.35; } delay(5); } 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; }