Class for using BMP180 Bosch Pressure sensor

Dependents:   ILI9341_Clock_Nucleo IoT-Polytech-Upmc

Revision:
0:b899fe37ce17
Child:
1:4c6b41f1203d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BMP180.cpp	Tue Nov 26 05:40:44 2013 +0000
@@ -0,0 +1,154 @@
+#include "BMP180.h"
+
+BMP180::BMP180(PinName sda, PinName scl) : bmp180i2c(sda,scl)
+{
+    bmp180i2c.frequency(BMP180FREQ);
+    oversampling_setting = OVERSAMPLING_HIGH_RESOLUTION;
+    rReg[0] = 0;
+    rReg[1] = 0;
+    rReg[2] = 0;
+    wReg[0] = 0;
+    wReg[1] = 0;
+    w[0] = 0xF4;
+    w[1] = 0xF4;
+    
+    cmd = CMD_READ_CALIBRATION; // EEPROM calibration command
+    for (int i = 0; i < EEprom; i++) { // read the 22 registers of the EEPROM
+        bmp180i2c.write(BMP180ADDR, &cmd, 1);
+        bmp180i2c.read(BMP180ADDR, rReg, 1);
+        data[i] = rReg[0];
+        cmd += 1;
+        wait_ms(10);
+    }
+
+    // parameters AC1-AC6
+    //The calibration is partioned in 11 words of 16 bits, each of them representing a coefficient
+    ac1 =  (data[0] <<8) | data[1]; // AC1(0xAA, 0xAB)... and so on
+    ac2 =  (data[2] <<8) | data[3];
+    ac3 =  (data[4] <<8) | data[5];
+    ac4 =  (data[6] <<8) | data[7];
+    ac5 =  (data[8] <<8) | data[9];
+    ac6 = (data[10] <<8) | data[11];
+    // parameters B1,B2
+    b1 =  (data[12] <<8) | data[13];
+    b2 =  (data[14] <<8) | data[15];
+    // parameters MB,MC,MD
+    mb =  (data[16] <<8) | data[17];
+    mc =  (data[18] <<8) | data[19];
+    md =  (data[20] <<8) | data[21];
+}
+
+BMP180::~BMP180()
+{
+}
+
+int BMP180::startTemperature()              // Start temperature measurement
+{
+    int errors = 0;
+    errors += bmp180i2c.write(BMP180ADDR, w, 2);
+    wReg[0] = 0xF4;
+    wReg[1] = 0x2E;
+    errors += bmp180i2c.write(BMP180ADDR, wReg, 2);   // write 0x2E in reg 0XF4
+    return(errors);
+}
+
+int BMP180::readTemperature(long *t)        // Get the temperature reading that was taken in startTemperature() but ensure 4.5 ms time has elapsed
+{
+    int errors = 0;
+    cmd = CMD_READ_VALUE;                           // 0xF6
+    errors += bmp180i2c.write(BMP180ADDR, &cmd, 1); // set pointer on 0xF6 before reading it?
+    errors += bmp180i2c.read(BMP180ADDR, rReg, 2);  // read 0xF6 (MSB) and 0xF7 (LSB)// rReg is 3 long though
+    *t = (rReg[0] << 8) | rReg[1];                  // UT = MSB << 8 + LSB
+    return(errors);
+}
+
+int BMP180::startPressure(int oversample)  // Start pressure measurement!  Note oversample will vary the time to complete this measurement. See defines above for oversampling constants to use!
+{
+    int errors = 0;
+    int uncomp_pressure_cmd = 0x34 + (oversample<<6);
+    errors = bmp180i2c.write(BMP180ADDR, w, 2);
+    wReg[0] = 0xF4;
+    wReg[1] = uncomp_pressure_cmd;
+    errors += bmp180i2c.write(BMP180ADDR, wReg, 2);
+    return(errors);
+}
+
+int BMP180::readPressure(long *p)          // Get the pressure reading that was taken in startPressure() but ensure time for the measurement to complete
+{
+    int errors = 0;
+    cmd = CMD_READ_VALUE;                           // 0xF6
+    errors += bmp180i2c.write(BMP180ADDR, &cmd, 1);
+    errors += bmp180i2c.read(BMP180ADDR, rReg, 3);  // read 0xF6 (MSB), 0xF7 (LSB), 0xF8 (XLSB)
+    *p = ((rReg[0] << 16) | (rReg[1] << 8) | rReg[2]) >> (8 - oversampling_setting);
+    return(errors);
+}
+
+/*
+int BMP180::rTemp(long *ut)
+{
+    int errors = 0;
+    errors = bmp180i2c.write(BMP180ADDR, w, 2);
+    wReg[0] = 0xF4;
+    wReg[1] = 0x2E;
+    errors += bmp180i2c.write(BMP180ADDR, wReg, 2); // write 0x2E in reg 0XF4
+    wait_ms(4.5);
+    cmd = CMD_READ_VALUE;                           // 0xF6
+    errors += bmp180i2c.write(BMP180ADDR, &cmd, 1); // set pointer on 0xF6 before reading it?
+    errors += bmp180i2c.read(BMP180ADDR, rReg, 2);  // read 0xF6 (MSB) and 0xF7 (LSB)// rReg is 3 long though
+    *ut = (rReg[0] << 8) | rReg[1];                 // UT = MSB << 8 + LSB
+    return(errors);
+}
+
+int BMP180::rPressure(long *up)
+{
+    int errors = 0;
+    errors = bmp180i2c.write(BMP180ADDR, w, 2);
+    int uncomp_pressure_cmd = 0x34 + (oversampling_setting<<6);
+    wReg[0] = 0xF4;
+    wReg[1] = uncomp_pressure_cmd;
+    errors += bmp180i2c.write(BMP180ADDR, wReg, 2);
+    switch (oversampling_setting) {
+        case OVERSAMPLING_ULTRA_LOW_POWER:
+            wait_ms(4.5);
+            break;
+        case OVERSAMPLING_STANDARD:
+            wait_ms(7.5);
+            break;
+        case OVERSAMPLING_HIGH_RESOLUTION:
+            wait_ms(13.5);
+            break;
+        case OVERSAMPLING_ULTRA_HIGH_RESOLUTION:
+            wait_ms(25.5);
+            break;
+    }
+    cmd = CMD_READ_VALUE; // 0xF6
+    errors += bmp180i2c.write(BMP180ADDR, &cmd, 1);
+    errors += bmp180i2c.read(BMP180ADDR, rReg, 3); // read 0xF6 (MSB), 0xF7 (LSB), 0xF8 (XLSB)
+    *up = ((rReg[0] << 16) | (rReg[1] << 8) | rReg[2]) >> (8 - oversampling_setting);
+    return(errors);
+}  */
+
+int BMP180::readTP(long *t, long *p, int oversample)  // get both temperature and pressure calculations that are compensated
+{
+    int errors = 0;
+    errors += BMP180::startTemperature();
+    wait_ms(4.5);
+    errors += BMP180::readTemperature(t);
+    errors += BMP180::startPressure(oversample);
+     switch (oversampling_setting) {
+        case OVERSAMPLING_ULTRA_LOW_POWER:
+            wait_ms(4.5);
+            break;
+        case OVERSAMPLING_STANDARD:
+            wait_ms(7.5);
+            break;
+        case OVERSAMPLING_HIGH_RESOLUTION:
+            wait_ms(13.5);
+            break;
+        case OVERSAMPLING_ULTRA_HIGH_RESOLUTION:
+            wait_ms(25.5);
+            break;
+    }
+    errors += BMP180::readPressure(p);    
+    return(errors);
+}
\ No newline at end of file