demo program of BMP085 pressure sensor

Dependencies:   mbed

Committer:
tylerjw
Date:
Tue Nov 06 02:39:40 2012 +0000
Revision:
2:8c00953d4755
library initial commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 2:8c00953d4755 1 /*
tylerjw 2:8c00953d4755 2 * @file BMP085.cpp
tylerjw 2:8c00953d4755 3 * @author Tyler Weaver
tylerjw 2:8c00953d4755 4 * @author Kory Hill
tylerjw 2:8c00953d4755 5 *
tylerjw 2:8c00953d4755 6 * @section LICENSE
tylerjw 2:8c00953d4755 7 *
tylerjw 2:8c00953d4755 8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
tylerjw 2:8c00953d4755 9 * and associated documentation files (the "Software"), to deal in the Software without restriction,
tylerjw 2:8c00953d4755 10 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
tylerjw 2:8c00953d4755 11 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
tylerjw 2:8c00953d4755 12 * furnished to do so, subject to the following conditions:
tylerjw 2:8c00953d4755 13 *
tylerjw 2:8c00953d4755 14 * The above copyright notice and this permission notice shall be included in all copies or
tylerjw 2:8c00953d4755 15 * substantial portions of the Software.
tylerjw 2:8c00953d4755 16 *
tylerjw 2:8c00953d4755 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
tylerjw 2:8c00953d4755 18 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
tylerjw 2:8c00953d4755 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
tylerjw 2:8c00953d4755 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tylerjw 2:8c00953d4755 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tylerjw 2:8c00953d4755 22 *
tylerjw 2:8c00953d4755 23 * @section DESCRIPTION
tylerjw 2:8c00953d4755 24 *
tylerjw 2:8c00953d4755 25 * BMP085 I2C Temperature/Pressure/Altitude Sensor
tylerjw 2:8c00953d4755 26 *
tylerjw 2:8c00953d4755 27 * Datasheet:
tylerjw 2:8c00953d4755 28 *
tylerjw 2:8c00953d4755 29 * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Pressure/BST-BMP085-DS000-06.pdf
tylerjw 2:8c00953d4755 30 */
tylerjw 2:8c00953d4755 31
tylerjw 2:8c00953d4755 32 #include "BMP085.h"
tylerjw 2:8c00953d4755 33 #include <new>
tylerjw 2:8c00953d4755 34
tylerjw 2:8c00953d4755 35 BMP085::BMP085(PinName sda, PinName scl) : i2c_(*reinterpret_cast<I2C*>(i2cRaw))
tylerjw 2:8c00953d4755 36 {
tylerjw 2:8c00953d4755 37 // Placement new to avoid additional heap memory allocation.
tylerjw 2:8c00953d4755 38 new(i2cRaw) I2C(sda, scl);
tylerjw 2:8c00953d4755 39
tylerjw 2:8c00953d4755 40 init();
tylerjw 2:8c00953d4755 41 }
tylerjw 2:8c00953d4755 42
tylerjw 2:8c00953d4755 43 BMP085::~BMP085()
tylerjw 2:8c00953d4755 44 {
tylerjw 2:8c00953d4755 45 // If the I2C object is initialized in the buffer in this object, call destructor of it.
tylerjw 2:8c00953d4755 46 if(&i2c_ == reinterpret_cast<I2C*>(&i2cRaw))
tylerjw 2:8c00953d4755 47 reinterpret_cast<I2C*>(&i2cRaw)->~I2C();
tylerjw 2:8c00953d4755 48 }
tylerjw 2:8c00953d4755 49
tylerjw 2:8c00953d4755 50 void BMP085::init()
tylerjw 2:8c00953d4755 51 {
tylerjw 2:8c00953d4755 52 get_cal_param();
tylerjw 2:8c00953d4755 53 set_oss(8); // standard over sampling (2 samples)
tylerjw 2:8c00953d4755 54 get_ut(); // initalize values
tylerjw 2:8c00953d4755 55 get_up();
tylerjw 2:8c00953d4755 56 }
tylerjw 2:8c00953d4755 57
tylerjw 2:8c00953d4755 58 void BMP085::get_cal_param()
tylerjw 2:8c00953d4755 59 {
tylerjw 2:8c00953d4755 60 // get calibration values
tylerjw 2:8c00953d4755 61 AC1 = read_int16(0xAA);
tylerjw 2:8c00953d4755 62 AC2 = read_int16(0xAC);
tylerjw 2:8c00953d4755 63 AC3 = read_int16(0xAE);
tylerjw 2:8c00953d4755 64 AC4 = read_uint16(0xB0);
tylerjw 2:8c00953d4755 65 AC5 = read_uint16(0xB2);
tylerjw 2:8c00953d4755 66 AC6 = read_uint16(0xB4);
tylerjw 2:8c00953d4755 67 B1 = read_int16(0xB6);
tylerjw 2:8c00953d4755 68 B2 = read_int16(0xB8);
tylerjw 2:8c00953d4755 69 MB = read_int16(0xBA);
tylerjw 2:8c00953d4755 70 MC = read_int16(0xBC);
tylerjw 2:8c00953d4755 71 MD = read_int16(0xBE);
tylerjw 2:8c00953d4755 72 }
tylerjw 2:8c00953d4755 73
tylerjw 2:8c00953d4755 74 void BMP085::display_cal_param(Serial *pc)
tylerjw 2:8c00953d4755 75 {
tylerjw 2:8c00953d4755 76 pc->printf("AC1 %x\r\n", AC1);
tylerjw 2:8c00953d4755 77 pc->printf("AC2 %x\r\n", AC2);
tylerjw 2:8c00953d4755 78 pc->printf("AC3 %x\r\n", AC3);
tylerjw 2:8c00953d4755 79 pc->printf("AC4 %x\r\n", AC4);
tylerjw 2:8c00953d4755 80 pc->printf("AC5 %x\r\n", AC5);
tylerjw 2:8c00953d4755 81 pc->printf("AC6 %x\r\n", AC6);
tylerjw 2:8c00953d4755 82 pc->printf("B1 %x\r\n", B1);
tylerjw 2:8c00953d4755 83 pc->printf("B2 %x\r\n", B2);
tylerjw 2:8c00953d4755 84 pc->printf("MB %x\r\n", MB);
tylerjw 2:8c00953d4755 85 pc->printf("MC %x\r\n", MC);
tylerjw 2:8c00953d4755 86 pc->printf("MD %x\r\n", MD);
tylerjw 2:8c00953d4755 87 }
tylerjw 2:8c00953d4755 88
tylerjw 2:8c00953d4755 89 void BMP085::get_ut()
tylerjw 2:8c00953d4755 90 {
tylerjw 2:8c00953d4755 91 write_char(0xF4,0x2E);
tylerjw 2:8c00953d4755 92 wait(0.045);
tylerjw 2:8c00953d4755 93 char buffer[3];
tylerjw 2:8c00953d4755 94 read_multiple(0xF6, buffer, 2);
tylerjw 2:8c00953d4755 95
tylerjw 2:8c00953d4755 96 UT = (buffer[0]<<8) | buffer[1];
tylerjw 2:8c00953d4755 97 }
tylerjw 2:8c00953d4755 98
tylerjw 2:8c00953d4755 99 void BMP085::get_up()
tylerjw 2:8c00953d4755 100 {
tylerjw 2:8c00953d4755 101 // set sample setting
tylerjw 2:8c00953d4755 102 write_char(0xF4, oversampling_setting_);
tylerjw 2:8c00953d4755 103
tylerjw 2:8c00953d4755 104 // wait
tylerjw 2:8c00953d4755 105 wait(conversion_time_);
tylerjw 2:8c00953d4755 106
tylerjw 2:8c00953d4755 107 // read the three bits
tylerjw 2:8c00953d4755 108 char buffer[3];
tylerjw 2:8c00953d4755 109 read_multiple(0xF6, buffer, 3);
tylerjw 2:8c00953d4755 110
tylerjw 2:8c00953d4755 111 UP = ((buffer[0]<<16) | (buffer[1]<<8) | (buffer[2])) >> (8 - oss_bit_);
tylerjw 2:8c00953d4755 112 }
tylerjw 2:8c00953d4755 113
tylerjw 2:8c00953d4755 114 void BMP085::set_oss(int oss)
tylerjw 2:8c00953d4755 115 {
tylerjw 2:8c00953d4755 116 switch(oss) {
tylerjw 2:8c00953d4755 117 case 1: // low power
tylerjw 2:8c00953d4755 118 oss_bit_ = 0;
tylerjw 2:8c00953d4755 119 oversampling_setting_ = 0x34;
tylerjw 2:8c00953d4755 120 conversion_time_ = 0.045;
tylerjw 2:8c00953d4755 121 break;
tylerjw 2:8c00953d4755 122 case 2: // standard
tylerjw 2:8c00953d4755 123 oss_bit_ = 1;
tylerjw 2:8c00953d4755 124 oversampling_setting_ = 0x74;
tylerjw 2:8c00953d4755 125 conversion_time_ = 0.075;
tylerjw 2:8c00953d4755 126 break;
tylerjw 2:8c00953d4755 127 case 4: // high resolution
tylerjw 2:8c00953d4755 128 oss_bit_ = 2;
tylerjw 2:8c00953d4755 129 oversampling_setting_ = 0xB4;
tylerjw 2:8c00953d4755 130 conversion_time_ = 0.135;
tylerjw 2:8c00953d4755 131 break;
tylerjw 2:8c00953d4755 132 case 8: // ultra high resolution
tylerjw 2:8c00953d4755 133 oss_bit_ = 3;
tylerjw 2:8c00953d4755 134 oversampling_setting_ = 0xF4;
tylerjw 2:8c00953d4755 135 conversion_time_ = 0.255;
tylerjw 2:8c00953d4755 136 break;
tylerjw 2:8c00953d4755 137 default: // standard
tylerjw 2:8c00953d4755 138 oss_bit_ = 1;
tylerjw 2:8c00953d4755 139 oversampling_setting_ = 0x74;
tylerjw 2:8c00953d4755 140 conversion_time_ = 0.075;
tylerjw 2:8c00953d4755 141 }
tylerjw 2:8c00953d4755 142 }
tylerjw 2:8c00953d4755 143
tylerjw 2:8c00953d4755 144 void BMP085::write_char(char address, char data)
tylerjw 2:8c00953d4755 145 {
tylerjw 2:8c00953d4755 146 char cmd[2];
tylerjw 2:8c00953d4755 147 cmd[0] = address;
tylerjw 2:8c00953d4755 148 cmd[1] = data;
tylerjw 2:8c00953d4755 149 i2c_.write( I2C_ADDRESS , cmd, 2);
tylerjw 2:8c00953d4755 150 }
tylerjw 2:8c00953d4755 151
tylerjw 2:8c00953d4755 152 int16_t BMP085::read_int16(char address)
tylerjw 2:8c00953d4755 153 {
tylerjw 2:8c00953d4755 154 char tx[2];
tylerjw 2:8c00953d4755 155 tx[0] = address;
tylerjw 2:8c00953d4755 156 i2c_.write(I2C_ADDRESS, tx, 1);
tylerjw 2:8c00953d4755 157 i2c_.read(I2C_ADDRESS, tx, 2);
tylerjw 2:8c00953d4755 158 int16_t value = ((tx[0] << 8) | tx[1]);
tylerjw 2:8c00953d4755 159 return value;
tylerjw 2:8c00953d4755 160 }
tylerjw 2:8c00953d4755 161
tylerjw 2:8c00953d4755 162 void BMP085::read_multiple(char address, char* buffer, int bits)
tylerjw 2:8c00953d4755 163 {
tylerjw 2:8c00953d4755 164 char cmd[2];
tylerjw 2:8c00953d4755 165 cmd[0] = address;
tylerjw 2:8c00953d4755 166 i2c_.write(I2C_ADDRESS, cmd, 1);
tylerjw 2:8c00953d4755 167 i2c_.read(I2C_ADDRESS, buffer, bits);
tylerjw 2:8c00953d4755 168 }
tylerjw 2:8c00953d4755 169
tylerjw 2:8c00953d4755 170 uint16_t BMP085::read_uint16(char address)
tylerjw 2:8c00953d4755 171 {
tylerjw 2:8c00953d4755 172 char tx[2];
tylerjw 2:8c00953d4755 173 tx[0] = address;
tylerjw 2:8c00953d4755 174 i2c_.write(I2C_ADDRESS, tx, 1);
tylerjw 2:8c00953d4755 175 i2c_.read(I2C_ADDRESS, tx, 2);
tylerjw 2:8c00953d4755 176 uint16_t value = ((tx[0] << 8) | tx[1]);
tylerjw 2:8c00953d4755 177 return value;
tylerjw 2:8c00953d4755 178 }
tylerjw 2:8c00953d4755 179
tylerjw 2:8c00953d4755 180 int32_t BMP085::get_temperature()
tylerjw 2:8c00953d4755 181 {
tylerjw 2:8c00953d4755 182 get_ut(); // uncompressed temperature
tylerjw 2:8c00953d4755 183 get_up(); // uncompressed pressure
tylerjw 2:8c00953d4755 184
tylerjw 2:8c00953d4755 185 long x1,x2;
tylerjw 2:8c00953d4755 186
tylerjw 2:8c00953d4755 187 //Start temperature calculation
tylerjw 2:8c00953d4755 188 x1 = ((UT - AC6) * AC5) >> 15;
tylerjw 2:8c00953d4755 189 x2 = (MC << 11) / (x1 + MD);
tylerjw 2:8c00953d4755 190 B5 = x1 + x2;
tylerjw 2:8c00953d4755 191 temperature = ((B5 + 8) >> 4);
tylerjw 2:8c00953d4755 192 return temperature;
tylerjw 2:8c00953d4755 193 }
tylerjw 2:8c00953d4755 194
tylerjw 2:8c00953d4755 195 int32_t BMP085::get_pressure()
tylerjw 2:8c00953d4755 196 {
tylerjw 2:8c00953d4755 197 get_up(); // get uncompressed pressure (uncompressed temperature must be called recently)
tylerjw 2:8c00953d4755 198
tylerjw 2:8c00953d4755 199 B6 = B5 - 4000;
tylerjw 2:8c00953d4755 200 X1 = (B2 * ((B6 * B6) >> 12)) >> 11;
tylerjw 2:8c00953d4755 201 X2 = (AC2 * B6) >> 11;
tylerjw 2:8c00953d4755 202 X3 = X1 + X2;
tylerjw 2:8c00953d4755 203 B3 = ((((((long)(AC1) * 4) + X3) << oss_bit_) + 2) >> 2);
tylerjw 2:8c00953d4755 204 X1 = (AC3 * B6) >> 13;
tylerjw 2:8c00953d4755 205 X2 = (B1 * ((B6 * B6) >> 12)) >> 16;
tylerjw 2:8c00953d4755 206 X3 = ((X1 + X2) + 2) >> 2;
tylerjw 2:8c00953d4755 207 B4 = (AC4 * (unsigned long)(X3 + 32768)) >> 15;
tylerjw 2:8c00953d4755 208 B7 = ((unsigned long)(UP - B3) * (50000 >> oss_bit_));
tylerjw 2:8c00953d4755 209 if (B7 < 0x80000000)
tylerjw 2:8c00953d4755 210 pressure = (B7 << 1) / B4;
tylerjw 2:8c00953d4755 211 else
tylerjw 2:8c00953d4755 212 pressure = (B7 / B4) << 1;
tylerjw 2:8c00953d4755 213
tylerjw 2:8c00953d4755 214 X1 = (pressure >> 8);
tylerjw 2:8c00953d4755 215 X1 *= X1;
tylerjw 2:8c00953d4755 216 X1 = (X1 * 3038) >>16;
tylerjw 2:8c00953d4755 217 X2 = (-7357 * pressure) >>16;
tylerjw 2:8c00953d4755 218 pressure += (X1 + X2 + 3791)>>4;
tylerjw 2:8c00953d4755 219
tylerjw 2:8c00953d4755 220 return pressure;
tylerjw 2:8c00953d4755 221 }
tylerjw 2:8c00953d4755 222
tylerjw 2:8c00953d4755 223 double BMP085::get_altitude_m()
tylerjw 2:8c00953d4755 224 {
tylerjw 2:8c00953d4755 225 const double P0 = 1013.25; //pressure at sea level in hPa
tylerjw 2:8c00953d4755 226 double pres_hpa = pressure / 100.0;
tylerjw 2:8c00953d4755 227 altitude = 44330.0 * (1.0 - pow((pres_hpa/P0), 0.190295));
tylerjw 2:8c00953d4755 228 return altitude;
tylerjw 2:8c00953d4755 229 }
tylerjw 2:8c00953d4755 230
tylerjw 2:8c00953d4755 231 double BMP085::get_altitude_ft()
tylerjw 2:8c00953d4755 232 {
tylerjw 2:8c00953d4755 233 return get_altitude_m()*3.28084;
tylerjw 2:8c00953d4755 234 }