1
/***************************************************
2
This is a library for the BMP085 Barometric Pressure & Temp Sensor
4
Designed specifically to work with the Adafruit BMP085 Breakout
5
----> https://www.adafruit.com/products/391
7
These displays use I2C to communicate, 2 pins are required to
9
Adafruit invests time and resources providing this open source code,
10
please support Adafruit and open-source hardware by purchasing
11
products from Adafruit!
13
Written by Limor Fried/Ladyada for Adafruit Industries.
14
BSD license, all text above must be included in any redistribution
15
****************************************************/
18
#include <util/delay.h>
24
boolean BMP085::begin(uint8_t mode) {
25
if (mode > BMP085_ULTRAHIGHRES)
26
mode = BMP085_ULTRAHIGHRES;
31
if (read8(0xD0) != 0x55) return false;
33
/* read calibration data */
34
ac1 = read16(BMP085_CAL_AC1);
35
ac2 = read16(BMP085_CAL_AC2);
36
ac3 = read16(BMP085_CAL_AC3);
37
ac4 = read16(BMP085_CAL_AC4);
38
ac5 = read16(BMP085_CAL_AC5);
39
ac6 = read16(BMP085_CAL_AC6);
41
b1 = read16(BMP085_CAL_B1);
42
b2 = read16(BMP085_CAL_B2);
44
mb = read16(BMP085_CAL_MB);
45
mc = read16(BMP085_CAL_MC);
46
md = read16(BMP085_CAL_MD);
47
#if (BMP085_DEBUG == 1)
48
Serial.print("ac1 = "); Serial.println(ac1, DEC);
49
Serial.print("ac2 = "); Serial.println(ac2, DEC);
50
Serial.print("ac3 = "); Serial.println(ac3, DEC);
51
Serial.print("ac4 = "); Serial.println(ac4, DEC);
52
Serial.print("ac5 = "); Serial.println(ac5, DEC);
53
Serial.print("ac6 = "); Serial.println(ac6, DEC);
55
Serial.print("b1 = "); Serial.println(b1, DEC);
56
Serial.print("b2 = "); Serial.println(b2, DEC);
58
Serial.print("mb = "); Serial.println(mb, DEC);
59
Serial.print("mc = "); Serial.println(mc, DEC);
60
Serial.print("md = "); Serial.println(md, DEC);
64
uint16_t BMP085::readRawTemperature(void) {
65
write8(BMP085_CONTROL, BMP085_READTEMPCMD);
68
Serial.print("Raw temp: "); Serial.println(read16(BMP085_TEMPDATA));
70
return read16(BMP085_TEMPDATA);
73
uint32_t BMP085::readRawPressure(void) {
76
write8(BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));
78
if (oversampling == BMP085_ULTRALOWPOWER)
80
else if (oversampling == BMP085_STANDARD)
82
else if (oversampling == BMP085_HIGHRES)
87
raw = read16(BMP085_PRESSUREDATA);
90
raw |= read8(BMP085_PRESSUREDATA+2);
91
raw >>= (8 - oversampling);
93
/* this pull broke stuff, look at it later?
94
if (oversampling==0) {
96
raw |= read8(BMP085_PRESSUREDATA+2);
97
raw >>= (8 - oversampling);
101
#if BMP085_DEBUG == 1
102
Serial.print("Raw pressure: "); Serial.println(raw);
108
int32_t BMP085::readPressure(void) {
109
int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
112
UT = readRawTemperature();
113
UP = readRawPressure();
115
#if BMP085_DEBUG == 1
116
// use datasheet numbers!
132
// do temperature calculations
133
X1=(UT-(int32_t)(ac6))*((int32_t)(ac5))/pow(2,15);
134
X2=((int32_t)mc*pow(2,11))/(X1+(int32_t)md);
137
#if BMP085_DEBUG == 1
138
Serial.print("X1 = "); Serial.println(X1);
139
Serial.print("X2 = "); Serial.println(X2);
140
Serial.print("B5 = "); Serial.println(B5);
145
X1 = ((int32_t)b2 * ( (B6 * B6)>>12 )) >> 11;
146
X2 = ((int32_t)ac2 * B6) >> 11;
148
B3 = ((((int32_t)ac1*4 + X3) << oversampling) + 2) / 4;
150
#if BMP085_DEBUG == 1
151
Serial.print("B6 = "); Serial.println(B6);
152
Serial.print("X1 = "); Serial.println(X1);
153
Serial.print("X2 = "); Serial.println(X2);
154
Serial.print("B3 = "); Serial.println(B3);
157
X1 = ((int32_t)ac3 * B6) >> 13;
158
X2 = ((int32_t)b1 * ((B6 * B6) >> 12)) >> 16;
159
X3 = ((X1 + X2) + 2) >> 2;
160
B4 = ((uint32_t)ac4 * (uint32_t)(X3 + 32768)) >> 15;
161
B7 = ((uint32_t)UP - B3) * (uint32_t)( 50000UL >> oversampling );
163
#if BMP085_DEBUG == 1
164
Serial.print("X1 = "); Serial.println(X1);
165
Serial.print("X2 = "); Serial.println(X2);
166
Serial.print("B4 = "); Serial.println(B4);
167
Serial.print("B7 = "); Serial.println(B7);
170
if (B7 < 0x80000000) {
175
X1 = (p >> 8) * (p >> 8);
176
X1 = (X1 * 3038) >> 16;
177
X2 = (-7357 * p) >> 16;
179
#if BMP085_DEBUG == 1
180
Serial.print("p = "); Serial.println(p);
181
Serial.print("X1 = "); Serial.println(X1);
182
Serial.print("X2 = "); Serial.println(X2);
185
p = p + ((X1 + X2 + (int32_t)3791)>>4);
186
#if BMP085_DEBUG == 1
187
Serial.print("p = "); Serial.println(p);
193
float BMP085::readTemperature(void) {
194
int32_t UT, X1, X2, B5; // following ds convention
197
UT = readRawTemperature();
199
#if BMP085_DEBUG == 1
200
// use datasheet numbers!
209
X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) / pow(2,15);
210
X2 = ((int32_t)mc * pow(2,11)) / (X1+(int32_t)md);
212
temp = (B5+8)/pow(2,4);
218
float BMP085::readAltitude(float sealevelPressure) {
221
float pressure = readPressure();
223
altitude = 44330 * (1.0 - pow(pressure /sealevelPressure,0.1903));
229
/*********************************************************************/
231
uint8_t BMP085::read8(uint8_t a) {
234
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
236
Wire.write(a); // sends register address to read from
238
Wire.send(a); // sends register address to read from
240
Wire.endTransmission(); // end transmission
242
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
243
Wire.requestFrom(BMP085_I2CADDR, 1);// send data n-bytes read
245
ret = Wire.read(); // receive DATA
247
ret = Wire.receive(); // receive DATA
249
Wire.endTransmission(); // end transmission
254
uint16_t BMP085::read16(uint8_t a) {
257
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
259
Wire.write(a); // sends register address to read from
261
Wire.send(a); // sends register address to read from
263
Wire.endTransmission(); // end transmission
265
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
266
Wire.requestFrom(BMP085_I2CADDR, 2);// send data n-bytes read
268
ret = Wire.read(); // receive DATA
270
ret |= Wire.read(); // receive DATA
272
ret = Wire.receive(); // receive DATA
274
ret |= Wire.receive(); // receive DATA
276
Wire.endTransmission(); // end transmission
281
void BMP085::write8(uint8_t a, uint8_t d) {
282
Wire.beginTransmission(BMP085_I2CADDR); // start transmission to device
284
Wire.write(a); // sends register address to read from
285
Wire.write(d); // write data
287
Wire.send(a); // sends register address to read from
288
Wire.send(d); // write data
290
Wire.endTransmission(); // end transmission