1#include "Arduino.h"
2#include "bmp180.h"
3
4bmp180::bmp180(int pin) {
5 pinMode(pin, OUTPUT);
6 _pin = pin;
7 Wire.begin();
8 calibrate();
9}
10
11//Calabration of Barometic sensor
12void bmp180::calibrate() {
13 _ac1 = readInt(0xAA);
14 _ac2 = readInt(0xAC);
15 _ac3 = readInt(0xAE);
16 _ac4 = readInt(0xB0);
17 _ac5 = readInt(0xB2);
18 _ac6 = readInt(0xB4);
19 _b1 = readInt(0xB6);
20 _b2 = readInt(0xB8);
21 _mb = readInt(0xBA);
22 _mc = readInt(0xBC);
23 _md = readInt(0xBE);
24}
25
26
27
28// Read 1 byte from BMP180
29char bmp180::read(unsigned char address) {
30
31 unsigned char data;
32
33 Wire.beginTransmission(bmppin);
34 Wire.write(address);
35 Wire.endTransmission();
36
37 Wire.requestFrom(bmppin, 1);
38 while(!Wire.available());
39
40 return Wire.read();
41}
42
43// Read 2 bytes from the BMP180. First byte from 'address', second from 'address'+1
44int bmp180::readInt(unsigned char address) {
45
46 unsigned char msb, lsb;
47
48 Wire.beginTransmission(bmppin);
49 Wire.write(address);
50 Wire.endTransmission();
51
52 Wire.requestFrom(bmppin, 2);
53 while(Wire.available()<2)
54 ;
55 msb = Wire.read();
56 lsb = Wire.read();
57
58 return (int) msb<<8 | lsb;
59}
60
61// Read the compensated temperature value
62unsigned int bmp180::readTemperature(){
63 unsigned int ut;
64
65 // Write 0x2E into Register 0xF4
66 // This requests a temperature reading
67 Wire.beginTransmission(bmppin);
68 Wire.write(0xF4);
69 Wire.write(0x2E);
70 Wire.endTransmission();
71
72 // Wait at least 4.5ms
73 delay(5);
74
75 // Read two bytes from registers 0xF6 and 0xF7
76 ut = readInt(0xF6);
77
78 long x1, x2;
79
80 x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
81 x2 = ((long)mc << 11)/(x1 + md);
82 b5 = x1 + x2;
83
84 float temp = ((b5 + 8)>>4);
85 temp = temp /10;
86
87 return temp;
88}
89
90
91
92
93
94
95// Calculate pressure given up (Uncompensated Pressure)
96// calibration values must be known
97// b5 is also required so bmp180GetTemperature(...) must be called first.
98// Value returned will be pressure in units of Pa.
99
100// Read the compensated pressure value
101unsigned long bmp180::readPressure(){
102
103 unsigned char msb, lsb, xlsb;
104 unsigned long up = 0;
105
106 // Write 0x34+(OSS<<6) into register 0xF4
107 // Request a pressure reading w/ oversampling setting
108 Wire.beginTransmission(bmppin);
109 Wire.write(0xF4);
110 Wire.write(0x34 + (OSS<<6));
111 Wire.endTransmission();
112
113 // Wait for conversion, delay time dependent on OSS
114 delay(2 + (3<<OSS));
115
116 // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
117 msb = read(0xF6);
118 lsb = read(0xF7);
119 xlsb = read(0xF8);
120
121 up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS); // Calculate uncompensated pressure
122
123
124 long x1, x2, x3, b3, b6, p;
125 unsigned long b4, b7;
126
127 b6 = b5 - 4000;
128 // Calculate B3
129 x1 = (b2 * (b6 * b6)>>12)>>11;
130 x2 = (ac2 * b6)>>11;
131 x3 = x1 + x2;
132 b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
133
134 // Calculate B4
135 x1 = (ac3 * b6)>>13;
136 x2 = (b1 * ((b6 * b6)>>12))>>16;
137 x3 = ((x1 + x2) + 2)>>2;
138 b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
139
140 b7 = ((unsigned long)(up - b3) * (50000>>OSS));
141 if (b7 < 0x80000000)
142 p = (b7<<1)/b4;
143 else
144 p = (b7/b4)<<1;
145
146 x1 = (p>>8) * (p>>8);
147 x1 = (x1 * 3038)>>16;
148 x2 = (-7357 * p)>>16;
149 p += (x1 + x2 + 3791)>>4;
150
151 long pressure = p;
152 return pressure;
153}