Edutech IoT Team / BMP180

Fork of BMP180 by Kevin Gillepsie

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BMP180.cpp Source File

BMP180.cpp

00001 #include "BMP180.h"
00002 
00003 //******************************************************************************
00004 BMP180::BMP180(PinName sda, PinName scl)
00005 {
00006     i2c_ = new I2C(sda, scl);
00007     i2c_owner = true;
00008 
00009     i2c_->frequency(400000);
00010 }
00011 
00012 //******************************************************************************
00013 BMP180::BMP180(I2C *i2c) :
00014     i2c_(i2c)
00015 {
00016     i2c_owner = false;
00017 }
00018 
00019 //******************************************************************************
00020 BMP180::~BMP180()
00021 {
00022     if(i2c_owner) {
00023         delete i2c_;
00024     }
00025 }
00026 
00027 //******************************************************************************
00028 int BMP180::init(void)
00029 {
00030     char addr;
00031     char data[22];
00032     int i;
00033 
00034     if (checkId() != 0) {
00035       
00036         return -1;
00037     }
00038 
00039     addr = REG_ADDR_AC1;
00040     if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
00041         return -1;
00042     }
00043 
00044     if (i2c_->read(I2C_ADDR, data, 22) != 0) {
00045         return -1;
00046     }
00047 
00048     for (i = 0; i < 11; i++) {
00049         calib.value[i] = (data[2*i] << 8) | data[(2*i)+1];
00050     }
00051 
00052     return 0;
00053 }
00054 
00055 //******************************************************************************
00056 int BMP180::reset(void)
00057 {
00058     char data;
00059 
00060     data = REG_ADDR_RESET;
00061     if (i2c_->write(I2C_ADDR, &data, 1) != 0) {
00062         return -1;
00063     }
00064 
00065     data = 0xB6;
00066     if (i2c_->write(I2C_ADDR, &data, 1) != 0) {
00067         return -1;
00068     }
00069 
00070     return 0;
00071 }
00072 
00073 //******************************************************************************
00074 int BMP180::checkId(void)
00075 {
00076     char addr;
00077     char data;
00078 
00079     addr = REG_ADDR_ID;
00080     if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
00081         return -1;
00082     }
00083 
00084     if (i2c_->read(I2C_ADDR, &data, 1) != 0) {
00085         return -1;
00086     }
00087 
00088     if (data != 0x55) {
00089         return -1;
00090     }
00091 
00092     return 0;
00093 }
00094 
00095 //******************************************************************************
00096 int BMP180::startPressure(BMP180::oversampling_t oss)
00097 {
00098     char data[2];
00099 
00100     data[0] = REG_ADDR_CTRL;
00101     data[1] = CTRL_REG_PRESS_0 | ((oss & 0x3) << 6);
00102     oss_ = oss;
00103 
00104     if (i2c_->write(I2C_ADDR, data, 2) != 0) {
00105         return -1;
00106     }
00107 
00108     return 0;
00109 }
00110 
00111 //******************************************************************************
00112 int BMP180::getPressure(int *pressure)
00113 {
00114     char addr, byte[3];
00115     uint32_t up;
00116     int32_t b6, x1, x2, x3, b3, p;
00117     uint32_t b4, b7;
00118 
00119     addr = REG_ADDR_DATA;
00120     if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
00121         return -1;
00122     }
00123 
00124     if (i2c_->read(I2C_ADDR, byte, 3) != 0) {
00125         return -1;
00126     }
00127 
00128     up = ((byte[0] << 16) | (byte[1] << 8) | byte[2]) >> (8 - oss_);
00129 
00130     b6 = b5 - 4000;
00131     x1 = (b6 * b6) >> 12;
00132     x1 *= calib.b2;
00133     x1 >>= 11;
00134     x2 = calib.ac2 * b6;
00135     x2 >>= 11;
00136     x3 = x1 + x2;
00137     b3 = (((((int32_t)calib.ac1) * 4 + x3) << oss_) + 2);
00138     b3 >>= 2;
00139 
00140     x1 = (calib.ac3 * b6) >> 13;
00141     x2 = (calib.b1 * ((b6 * b6) >> 12)) >> 16;
00142     x3 = (x1 + x2 + 2) >> 2;
00143     b4 = (calib.ac4 * (uint32_t)(x3 + 32768)) >> 15;
00144     b7 = ((uint32_t)up - b3) * (50000 >> oss_);
00145     p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));
00146     x1 = p >> 8;
00147     x1 *= x1;
00148     x1 = (x1 * 3038) >> 16;
00149     x2 = (-7357 * p) >> 16;
00150     p += (x1 + x2 + 3791) >> 4;
00151 
00152     *pressure = p;
00153 
00154     return 0;
00155 }
00156 
00157 //******************************************************************************
00158 int BMP180::startTemperature(void)
00159 {
00160     char data[2] = { REG_ADDR_CTRL, CTRL_REG_TEMP };
00161 
00162     if (i2c_->write(I2C_ADDR, data, 2) != 0) {
00163         return -1;
00164     }
00165 
00166     return 0;
00167 }
00168 
00169 //******************************************************************************
00170 int BMP180::getTemperature(float *tempC)
00171 {
00172     char addr, byte[2];
00173     uint16_t ut;
00174     int32_t x1, x2;
00175 
00176     addr = REG_ADDR_DATA;
00177     if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
00178         return -1;
00179     }
00180 
00181     if (i2c_->read(I2C_ADDR, byte, 2) != 0) {
00182         return -1;
00183     }
00184 
00185     ut = (byte[0] << 8) | byte[1];
00186 
00187     x1 = ((ut - calib.ac6) * calib.ac5) >> 15;
00188     x2 = (calib.mc << 11) / (x1 + calib.md);
00189     b5 = x1 + x2;
00190 
00191     *tempC = (float)(b5 + 8) / 160;
00192 
00193     return 0;
00194 }
00195 
00196 //******************************************************************************
00197 int BMP180::getTemperature(int16_t *tempCx10)
00198 {
00199     char addr, byte[2];
00200     uint16_t ut;
00201     int32_t x1, x2;
00202 
00203     addr = REG_ADDR_DATA;
00204     if (i2c_->write(I2C_ADDR, &addr, 1) != 0) {
00205         return -1;
00206     }
00207 
00208     if (i2c_->read(I2C_ADDR, byte, 2) != 0) {
00209         return -1;
00210     }
00211 
00212     ut = (byte[0] << 8) | byte[1];
00213 
00214     x1 = ((ut - calib.ac6) * calib.ac5) >> 15;
00215     x2 = (calib.mc << 11) / (x1 + calib.md);
00216     b5 = x1 + x2;
00217 
00218     *tempCx10 = (b5 + 8) >> 4;
00219 
00220     return 0;
00221 }