Class for using BMP180 Bosch Pressure sensor

Dependents:   ILI9341_Clock_Nucleo IoT-Polytech-Upmc

Committer:
harrypowers
Date:
Tue Nov 26 05:40:44 2013 +0000
Revision:
0:b899fe37ce17
Child:
1:4c6b41f1203d
code working now to add the compensation calculations for both temperature and pressure!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harrypowers 0:b899fe37ce17 1 #include "BMP180.h"
harrypowers 0:b899fe37ce17 2
harrypowers 0:b899fe37ce17 3 BMP180::BMP180(PinName sda, PinName scl) : bmp180i2c(sda,scl)
harrypowers 0:b899fe37ce17 4 {
harrypowers 0:b899fe37ce17 5 bmp180i2c.frequency(BMP180FREQ);
harrypowers 0:b899fe37ce17 6 oversampling_setting = OVERSAMPLING_HIGH_RESOLUTION;
harrypowers 0:b899fe37ce17 7 rReg[0] = 0;
harrypowers 0:b899fe37ce17 8 rReg[1] = 0;
harrypowers 0:b899fe37ce17 9 rReg[2] = 0;
harrypowers 0:b899fe37ce17 10 wReg[0] = 0;
harrypowers 0:b899fe37ce17 11 wReg[1] = 0;
harrypowers 0:b899fe37ce17 12 w[0] = 0xF4;
harrypowers 0:b899fe37ce17 13 w[1] = 0xF4;
harrypowers 0:b899fe37ce17 14
harrypowers 0:b899fe37ce17 15 cmd = CMD_READ_CALIBRATION; // EEPROM calibration command
harrypowers 0:b899fe37ce17 16 for (int i = 0; i < EEprom; i++) { // read the 22 registers of the EEPROM
harrypowers 0:b899fe37ce17 17 bmp180i2c.write(BMP180ADDR, &cmd, 1);
harrypowers 0:b899fe37ce17 18 bmp180i2c.read(BMP180ADDR, rReg, 1);
harrypowers 0:b899fe37ce17 19 data[i] = rReg[0];
harrypowers 0:b899fe37ce17 20 cmd += 1;
harrypowers 0:b899fe37ce17 21 wait_ms(10);
harrypowers 0:b899fe37ce17 22 }
harrypowers 0:b899fe37ce17 23
harrypowers 0:b899fe37ce17 24 // parameters AC1-AC6
harrypowers 0:b899fe37ce17 25 //The calibration is partioned in 11 words of 16 bits, each of them representing a coefficient
harrypowers 0:b899fe37ce17 26 ac1 = (data[0] <<8) | data[1]; // AC1(0xAA, 0xAB)... and so on
harrypowers 0:b899fe37ce17 27 ac2 = (data[2] <<8) | data[3];
harrypowers 0:b899fe37ce17 28 ac3 = (data[4] <<8) | data[5];
harrypowers 0:b899fe37ce17 29 ac4 = (data[6] <<8) | data[7];
harrypowers 0:b899fe37ce17 30 ac5 = (data[8] <<8) | data[9];
harrypowers 0:b899fe37ce17 31 ac6 = (data[10] <<8) | data[11];
harrypowers 0:b899fe37ce17 32 // parameters B1,B2
harrypowers 0:b899fe37ce17 33 b1 = (data[12] <<8) | data[13];
harrypowers 0:b899fe37ce17 34 b2 = (data[14] <<8) | data[15];
harrypowers 0:b899fe37ce17 35 // parameters MB,MC,MD
harrypowers 0:b899fe37ce17 36 mb = (data[16] <<8) | data[17];
harrypowers 0:b899fe37ce17 37 mc = (data[18] <<8) | data[19];
harrypowers 0:b899fe37ce17 38 md = (data[20] <<8) | data[21];
harrypowers 0:b899fe37ce17 39 }
harrypowers 0:b899fe37ce17 40
harrypowers 0:b899fe37ce17 41 BMP180::~BMP180()
harrypowers 0:b899fe37ce17 42 {
harrypowers 0:b899fe37ce17 43 }
harrypowers 0:b899fe37ce17 44
harrypowers 0:b899fe37ce17 45 int BMP180::startTemperature() // Start temperature measurement
harrypowers 0:b899fe37ce17 46 {
harrypowers 0:b899fe37ce17 47 int errors = 0;
harrypowers 0:b899fe37ce17 48 errors += bmp180i2c.write(BMP180ADDR, w, 2);
harrypowers 0:b899fe37ce17 49 wReg[0] = 0xF4;
harrypowers 0:b899fe37ce17 50 wReg[1] = 0x2E;
harrypowers 0:b899fe37ce17 51 errors += bmp180i2c.write(BMP180ADDR, wReg, 2); // write 0x2E in reg 0XF4
harrypowers 0:b899fe37ce17 52 return(errors);
harrypowers 0:b899fe37ce17 53 }
harrypowers 0:b899fe37ce17 54
harrypowers 0:b899fe37ce17 55 int BMP180::readTemperature(long *t) // Get the temperature reading that was taken in startTemperature() but ensure 4.5 ms time has elapsed
harrypowers 0:b899fe37ce17 56 {
harrypowers 0:b899fe37ce17 57 int errors = 0;
harrypowers 0:b899fe37ce17 58 cmd = CMD_READ_VALUE; // 0xF6
harrypowers 0:b899fe37ce17 59 errors += bmp180i2c.write(BMP180ADDR, &cmd, 1); // set pointer on 0xF6 before reading it?
harrypowers 0:b899fe37ce17 60 errors += bmp180i2c.read(BMP180ADDR, rReg, 2); // read 0xF6 (MSB) and 0xF7 (LSB)// rReg is 3 long though
harrypowers 0:b899fe37ce17 61 *t = (rReg[0] << 8) | rReg[1]; // UT = MSB << 8 + LSB
harrypowers 0:b899fe37ce17 62 return(errors);
harrypowers 0:b899fe37ce17 63 }
harrypowers 0:b899fe37ce17 64
harrypowers 0:b899fe37ce17 65 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!
harrypowers 0:b899fe37ce17 66 {
harrypowers 0:b899fe37ce17 67 int errors = 0;
harrypowers 0:b899fe37ce17 68 int uncomp_pressure_cmd = 0x34 + (oversample<<6);
harrypowers 0:b899fe37ce17 69 errors = bmp180i2c.write(BMP180ADDR, w, 2);
harrypowers 0:b899fe37ce17 70 wReg[0] = 0xF4;
harrypowers 0:b899fe37ce17 71 wReg[1] = uncomp_pressure_cmd;
harrypowers 0:b899fe37ce17 72 errors += bmp180i2c.write(BMP180ADDR, wReg, 2);
harrypowers 0:b899fe37ce17 73 return(errors);
harrypowers 0:b899fe37ce17 74 }
harrypowers 0:b899fe37ce17 75
harrypowers 0:b899fe37ce17 76 int BMP180::readPressure(long *p) // Get the pressure reading that was taken in startPressure() but ensure time for the measurement to complete
harrypowers 0:b899fe37ce17 77 {
harrypowers 0:b899fe37ce17 78 int errors = 0;
harrypowers 0:b899fe37ce17 79 cmd = CMD_READ_VALUE; // 0xF6
harrypowers 0:b899fe37ce17 80 errors += bmp180i2c.write(BMP180ADDR, &cmd, 1);
harrypowers 0:b899fe37ce17 81 errors += bmp180i2c.read(BMP180ADDR, rReg, 3); // read 0xF6 (MSB), 0xF7 (LSB), 0xF8 (XLSB)
harrypowers 0:b899fe37ce17 82 *p = ((rReg[0] << 16) | (rReg[1] << 8) | rReg[2]) >> (8 - oversampling_setting);
harrypowers 0:b899fe37ce17 83 return(errors);
harrypowers 0:b899fe37ce17 84 }
harrypowers 0:b899fe37ce17 85
harrypowers 0:b899fe37ce17 86 /*
harrypowers 0:b899fe37ce17 87 int BMP180::rTemp(long *ut)
harrypowers 0:b899fe37ce17 88 {
harrypowers 0:b899fe37ce17 89 int errors = 0;
harrypowers 0:b899fe37ce17 90 errors = bmp180i2c.write(BMP180ADDR, w, 2);
harrypowers 0:b899fe37ce17 91 wReg[0] = 0xF4;
harrypowers 0:b899fe37ce17 92 wReg[1] = 0x2E;
harrypowers 0:b899fe37ce17 93 errors += bmp180i2c.write(BMP180ADDR, wReg, 2); // write 0x2E in reg 0XF4
harrypowers 0:b899fe37ce17 94 wait_ms(4.5);
harrypowers 0:b899fe37ce17 95 cmd = CMD_READ_VALUE; // 0xF6
harrypowers 0:b899fe37ce17 96 errors += bmp180i2c.write(BMP180ADDR, &cmd, 1); // set pointer on 0xF6 before reading it?
harrypowers 0:b899fe37ce17 97 errors += bmp180i2c.read(BMP180ADDR, rReg, 2); // read 0xF6 (MSB) and 0xF7 (LSB)// rReg is 3 long though
harrypowers 0:b899fe37ce17 98 *ut = (rReg[0] << 8) | rReg[1]; // UT = MSB << 8 + LSB
harrypowers 0:b899fe37ce17 99 return(errors);
harrypowers 0:b899fe37ce17 100 }
harrypowers 0:b899fe37ce17 101
harrypowers 0:b899fe37ce17 102 int BMP180::rPressure(long *up)
harrypowers 0:b899fe37ce17 103 {
harrypowers 0:b899fe37ce17 104 int errors = 0;
harrypowers 0:b899fe37ce17 105 errors = bmp180i2c.write(BMP180ADDR, w, 2);
harrypowers 0:b899fe37ce17 106 int uncomp_pressure_cmd = 0x34 + (oversampling_setting<<6);
harrypowers 0:b899fe37ce17 107 wReg[0] = 0xF4;
harrypowers 0:b899fe37ce17 108 wReg[1] = uncomp_pressure_cmd;
harrypowers 0:b899fe37ce17 109 errors += bmp180i2c.write(BMP180ADDR, wReg, 2);
harrypowers 0:b899fe37ce17 110 switch (oversampling_setting) {
harrypowers 0:b899fe37ce17 111 case OVERSAMPLING_ULTRA_LOW_POWER:
harrypowers 0:b899fe37ce17 112 wait_ms(4.5);
harrypowers 0:b899fe37ce17 113 break;
harrypowers 0:b899fe37ce17 114 case OVERSAMPLING_STANDARD:
harrypowers 0:b899fe37ce17 115 wait_ms(7.5);
harrypowers 0:b899fe37ce17 116 break;
harrypowers 0:b899fe37ce17 117 case OVERSAMPLING_HIGH_RESOLUTION:
harrypowers 0:b899fe37ce17 118 wait_ms(13.5);
harrypowers 0:b899fe37ce17 119 break;
harrypowers 0:b899fe37ce17 120 case OVERSAMPLING_ULTRA_HIGH_RESOLUTION:
harrypowers 0:b899fe37ce17 121 wait_ms(25.5);
harrypowers 0:b899fe37ce17 122 break;
harrypowers 0:b899fe37ce17 123 }
harrypowers 0:b899fe37ce17 124 cmd = CMD_READ_VALUE; // 0xF6
harrypowers 0:b899fe37ce17 125 errors += bmp180i2c.write(BMP180ADDR, &cmd, 1);
harrypowers 0:b899fe37ce17 126 errors += bmp180i2c.read(BMP180ADDR, rReg, 3); // read 0xF6 (MSB), 0xF7 (LSB), 0xF8 (XLSB)
harrypowers 0:b899fe37ce17 127 *up = ((rReg[0] << 16) | (rReg[1] << 8) | rReg[2]) >> (8 - oversampling_setting);
harrypowers 0:b899fe37ce17 128 return(errors);
harrypowers 0:b899fe37ce17 129 } */
harrypowers 0:b899fe37ce17 130
harrypowers 0:b899fe37ce17 131 int BMP180::readTP(long *t, long *p, int oversample) // get both temperature and pressure calculations that are compensated
harrypowers 0:b899fe37ce17 132 {
harrypowers 0:b899fe37ce17 133 int errors = 0;
harrypowers 0:b899fe37ce17 134 errors += BMP180::startTemperature();
harrypowers 0:b899fe37ce17 135 wait_ms(4.5);
harrypowers 0:b899fe37ce17 136 errors += BMP180::readTemperature(t);
harrypowers 0:b899fe37ce17 137 errors += BMP180::startPressure(oversample);
harrypowers 0:b899fe37ce17 138 switch (oversampling_setting) {
harrypowers 0:b899fe37ce17 139 case OVERSAMPLING_ULTRA_LOW_POWER:
harrypowers 0:b899fe37ce17 140 wait_ms(4.5);
harrypowers 0:b899fe37ce17 141 break;
harrypowers 0:b899fe37ce17 142 case OVERSAMPLING_STANDARD:
harrypowers 0:b899fe37ce17 143 wait_ms(7.5);
harrypowers 0:b899fe37ce17 144 break;
harrypowers 0:b899fe37ce17 145 case OVERSAMPLING_HIGH_RESOLUTION:
harrypowers 0:b899fe37ce17 146 wait_ms(13.5);
harrypowers 0:b899fe37ce17 147 break;
harrypowers 0:b899fe37ce17 148 case OVERSAMPLING_ULTRA_HIGH_RESOLUTION:
harrypowers 0:b899fe37ce17 149 wait_ms(25.5);
harrypowers 0:b899fe37ce17 150 break;
harrypowers 0:b899fe37ce17 151 }
harrypowers 0:b899fe37ce17 152 errors += BMP180::readPressure(p);
harrypowers 0:b899fe37ce17 153 return(errors);
harrypowers 0:b899fe37ce17 154 }