barometric pressure sensor BMP085 http://mbed.org/users/okini3939/notebook/barometric-pressure-sensor-bmp085/ http://mbed.org/users/okini3939/notebook/weatherduino-on-mbed/

Dependents:   WeatherPlatform_20110408 WeatherPlatform ENVLogger WeatherStation ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BMP085.cpp Source File

BMP085.cpp

Go to the documentation of this file.
00001 /*
00002  * mbed library to use a Bosch Sensortec BMP085/BMP180 sensor
00003  * Copyright (c) 2010 Hiroshi Suga
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 /** @file BMP085.cpp
00008  * @brief mbed library to use a Bosch Sensortec BMP085/BMP180 sensor
00009  * barometric pressure sensor BMP085/BMP180 (Bosch Sensortec)
00010  * interface: I2C digital
00011  */
00012 
00013 #include "mbed.h"
00014 #include "BMP085.h"
00015 
00016 #define WEATHER_BMP085 0xee
00017 #define xpow(x, y) ((long)1 << y)
00018 
00019 /**
00020  * @brief Initializes interface (private I2C)
00021  * @param p_sda port of I2C SDA
00022  * @param p_scl port of I2C SCL
00023  * @param p_oss parameter of OSS
00024  */
00025 BMP085::BMP085 (PinName p_sda, PinName p_scl, BMP085_oss p_oss) : i2c(p_sda, p_scl) {
00026     init(p_oss);
00027 }
00028 
00029 /**
00030  * @brief Initializes interface (public I2C)
00031  * @param p_i2c instance of I2C class
00032  * @param p_oss parameter of OSS
00033  */
00034 BMP085::BMP085 (I2C& p_i2c, BMP085_oss p_oss) : i2c(p_i2c) { 
00035     init(p_oss);
00036 }
00037 
00038 /**
00039  * @brief Get temperature
00040  * @return temperature (`C)
00041  */
00042 float BMP085::get_temperature() {
00043     return temperature;
00044 }
00045 
00046 /**
00047  * @brief Get pressure
00048  * @return pressure (hPa)
00049  */
00050 float BMP085::get_pressure() {
00051     return pressure;
00052 }
00053 
00054 /**
00055  * @brief Update results
00056  */
00057 void BMP085::update () {
00058     long t, p, ut, up, x1, x2, x3, b3, b5, b6;
00059     unsigned long b4, b7;
00060 
00061     twi_writechar(WEATHER_BMP085, 0xf4, 0x2e);
00062     wait(0.01);
00063     ut = twi_readshort(WEATHER_BMP085, 0xf6);
00064 
00065     twi_writechar(WEATHER_BMP085, 0xf4, 0x34 | (oss << 6));
00066     wait(0.05);
00067     up = twi_readlong(WEATHER_BMP085, 0xf6) >> (8 - oss);
00068 
00069     x1 = (ut - ac6) * ac5 / xpow(2, 15);
00070     x2 = (long)mc * xpow(2, 11) / (x1 + md);
00071     b5 = x1 + x2;
00072     t = (b5 + 8) / xpow(2, 4);
00073     temperature = (float)t / 10.0;
00074 
00075     b6 = b5 - 4000;
00076     x1 = (b2 * (b6 * b6 / xpow(2, 12))) / xpow(2, 11);
00077     x2 = ac2 * b6 / xpow(2, 11);
00078     x3 = x1 + x2;
00079     b3 = ((((unsigned long)ac1 * 4 + x3) << oss) + 2) / 4;
00080     x1 = ac3 * b6 / xpow(2, 13);
00081     x2 = (b1 * (b6 * b6 / xpow(2, 12))) / xpow(2, 16);
00082     x3 = ((x1 + x2) + 2) / xpow(2, 2);
00083     b4 = ac4 * (unsigned long)(x3 + 32768) / xpow(2, 15);
00084     b7 = ((unsigned long)up - b3) * (50000 >> oss);
00085     if (b7 < (unsigned long)0x80000000) {
00086         p = (b7 * 2) / b4;
00087     } else {
00088         p = (b7 / b4) * 2;
00089     }
00090     x1 = (p / xpow(2, 8)) * (p / xpow(2, 8));
00091     x1 = (x1 * 3038) / xpow(2, 16);
00092     x2 = (-7357 * p) / xpow(2, 16);
00093     p = p + (x1 + x2 + 3791) / xpow(2, 4);
00094     pressure = (float)p / 100.0;
00095 }
00096 
00097 void BMP085::init (BMP085_oss p_oss) {
00098     ac1 = twi_readshort(WEATHER_BMP085, 0xaa);
00099     ac2 = twi_readshort(WEATHER_BMP085, 0xac);
00100     ac3 = twi_readshort(WEATHER_BMP085, 0xae);
00101     ac4 = twi_readshort(WEATHER_BMP085, 0xb0);
00102     ac5 = twi_readshort(WEATHER_BMP085, 0xb2);
00103     ac6 = twi_readshort(WEATHER_BMP085, 0xb4);
00104     b1 = twi_readshort(WEATHER_BMP085, 0xb6);
00105     b2 = twi_readshort(WEATHER_BMP085, 0xb8);
00106     mb = twi_readshort(WEATHER_BMP085, 0xba);
00107     mc = twi_readshort(WEATHER_BMP085, 0xbc);
00108     md = twi_readshort(WEATHER_BMP085, 0xbe);
00109     oss = p_oss;
00110 }
00111 
00112 unsigned short BMP085::twi_readshort (int id, int addr) {
00113     unsigned short i;
00114 
00115     i2c.start();
00116     i2c.write(id);
00117     i2c.write(addr);
00118 
00119     i2c.start();
00120     i2c.write(id | 1);
00121     i = i2c.read(1) << 8;
00122     i |= i2c.read(0);
00123     i2c.stop();
00124 
00125     return i;
00126 }
00127 
00128 unsigned long BMP085::twi_readlong (int id, int addr) {
00129     unsigned long i;
00130 
00131     i2c.start();
00132     i2c.write(id);
00133     i2c.write(addr);
00134 
00135     i2c.start();
00136     i2c.write(id | 1);
00137     i = i2c.read(1) << 16;
00138     i |= i2c.read(1) << 8;
00139     i |= i2c.read(0);
00140     i2c.stop();
00141 
00142     return i;
00143 }
00144 
00145 void BMP085::twi_writechar (int id, int addr, int dat) {
00146 
00147     i2c.start();
00148     i2c.write(id);
00149     i2c.write(addr);
00150     i2c.write(dat);
00151     i2c.stop();
00152 }