2 #include "baro_bmp085.h"
5 * Calculations used in this code taken from Adafruit BMP085 calculation
6 * https://github.com/adafruit/Adafruit-BMP085-Library/blob/master/Adafruit_BMP085.cpp
8 * Code reused under GPL License, see the Adafruit project for full license information
9 * https://github.com/adafruit/Adafruit-BMP085-Library/
12 void BMP085::initialize()
17 // All the calibration registers are in order of our struct and are 2 bytes long too,
18 // so just read them directly into our calib data
19 readRegister(BMP085_CALIB_COEFFS_START
, (uint8_t *)&m_calib
, sizeof(m_calib
));
22 uint8_t BMP085::getPressureDuration()
24 switch (OVERSAMPLING_PRESSURE
)
26 case 0: return 5; // 4.5ms
27 case 1: return 8; // 7.5ms
28 case 2: return 14; // 13.5ms
29 default: // fallthrough
30 case 3: return 26; // 25.5ms
34 void BMP085::startPressure()
36 uint8_t reg_value
= BMP085_CMD_MEAS_PRESSURE
| (OVERSAMPLING_PRESSURE
<< 6);
37 writeRegister(BMP085_REG_CONTROL
, ®_value
, sizeof(reg_value
));
40 int32_t BMP085::computeB5(int32_t UT
) const
42 int32_t X1
= (UT
- (int32_t)m_calib
.ac6
) * ((int32_t)m_calib
.ac5
) >> 15;
43 int32_t X2
= ((int32_t)m_calib
.mc
<< 11) / (X1
+ (int32_t)m_calib
.md
);
47 uint32_t BMP085::getPressure()
50 readRegister(BMP085_REG_PRESSURE_DATA
, buf
, sizeof(buf
));
51 uint32_t raw
= ((buf
[0] << 16) | (buf
[1] << 8) | buf
[2]) >> OVERSAMPLING_PRESSURE
;
53 int32_t B3
, B5
, B6
, X1
, X2
, X3
, p
;
56 B5
= computeB5(m_temperatureLast
);
59 X1
= ((int32_t)m_calib
.b2
* ((B6
* B6
) >> 12)) >> 11;
60 X2
= ((int32_t)m_calib
.ac2
* B6
) >> 11;
62 B3
= ((((int32_t)m_calib
.ac1
* 4 + X3
) << OVERSAMPLING_PRESSURE
) + 2) / 4;
64 X1
= ((int32_t)m_calib
.ac3
* B6
) >> 13;
65 X2
= ((int32_t)m_calib
.b1
* ((B6
* B6
) >> 12)) >> 16;
66 X3
= ((X1
+ X2
) + 2) >> 2;
67 B4
= ((uint32_t)m_calib
.ac4
* (uint32_t)(X3
+ 32768)) >> 15;
68 B7
= ((uint32_t)raw
- B3
) * (uint32_t)(50000UL >> OVERSAMPLING_PRESSURE
);
70 if (B7
< 0x80000000) {
75 X1
= (p
>> 8) * (p
>> 8);
76 X1
= (X1
* 3038) >> 16;
77 X2
= (-7357 * p
) >> 16;
79 p
= p
+ ((X1
+ X2
+ (int32_t)3791) >> 4);
83 uint8_t BMP085::getTemperatureDuration()
85 // Max. conversion time 4.5ms
89 void BMP085::startTemperature()
91 uint8_t reg_value
= BMP085_CMD_MEAS_TEMPERATURE
;
92 writeRegister(BMP085_REG_CONTROL
, ®_value
, sizeof(reg_value
));
95 int32_t BMP085::getTemperature()
97 m_temperatureLast
= 0;
98 readRegister(BMP085_REG_TEMPERATURE_DATA
, (uint8_t *)&m_temperatureLast
, sizeof(m_temperatureLast
));
99 return m_temperatureLast
;
102 bool BMP085::detect()
104 // Assumes Wire.begin() has already been called
106 m_address
= BMP085_I2C_ADDR
;
107 readRegister(BMP085_REG_CHIPID
, &chipid
, sizeof(chipid
));
108 return chipid
== BMP085_CHIPID
;