![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
demo program of BMP085 pressure sensor
main.cpp@0:9c929de0a051, 2012-10-05 (annotated)
- Committer:
- newk8600
- Date:
- Fri Oct 05 08:03:52 2012 +0000
- Revision:
- 0:9c929de0a051
- Child:
- 1:2935199329b2
broken but I'll fix it...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
newk8600 | 0:9c929de0a051 | 1 | /* |
newk8600 | 0:9c929de0a051 | 2 | * |
newk8600 | 0:9c929de0a051 | 3 | * |
newk8600 | 0:9c929de0a051 | 4 | * |
newk8600 | 0:9c929de0a051 | 5 | * 1.8 - 3.6V (Vdd) |
newk8600 | 0:9c929de0a051 | 6 | * 1.62 - 3.6 (Vddio) |
newk8600 | 0:9c929de0a051 | 7 | * |
newk8600 | 0:9c929de0a051 | 8 | * |
newk8600 | 0:9c929de0a051 | 9 | *Altitude = 44330*(1-(p/p0)^(1/5.255)) |
newk8600 | 0:9c929de0a051 | 10 | * set p0 to sealevel pressure |
newk8600 | 0:9c929de0a051 | 11 | * delta p = 1hPa = 8.43m at sea level |
newk8600 | 0:9c929de0a051 | 12 | * |
newk8600 | 0:9c929de0a051 | 13 | */ |
newk8600 | 0:9c929de0a051 | 14 | |
newk8600 | 0:9c929de0a051 | 15 | /* |
newk8600 | 0:9c929de0a051 | 16 | *Pinout: |
newk8600 | 0:9c929de0a051 | 17 | *pin9 = SDA |
newk8600 | 0:9c929de0a051 | 18 | *pin10 = SCL |
newk8600 | 0:9c929de0a051 | 19 | *pin11 = XCLR (digital out; active low; Resets sensor) |
newk8600 | 0:9c929de0a051 | 20 | *pin12 = EOC ("end of conversation"; signal when conversion finished) |
newk8600 | 0:9c929de0a051 | 21 | * |
newk8600 | 0:9c929de0a051 | 22 | * |
newk8600 | 0:9c929de0a051 | 23 | * |
newk8600 | 0:9c929de0a051 | 24 | */ |
newk8600 | 0:9c929de0a051 | 25 | #include "mbed.h" |
newk8600 | 0:9c929de0a051 | 26 | |
newk8600 | 0:9c929de0a051 | 27 | enum bmp085_oss {ULTRALOW, //samples 1 time |
newk8600 | 0:9c929de0a051 | 28 | STANDARD, //samples twice |
newk8600 | 0:9c929de0a051 | 29 | HIGH, //samples 4 times |
newk8600 | 0:9c929de0a051 | 30 | ULTRAHIGH};//samples 8 times |
newk8600 | 0:9c929de0a051 | 31 | |
newk8600 | 0:9c929de0a051 | 32 | |
newk8600 | 0:9c929de0a051 | 33 | I2C i2c(p9, p10); // sda, scl |
newk8600 | 0:9c929de0a051 | 34 | Serial pc(USBTX, USBRX); // tx, rx |
newk8600 | 0:9c929de0a051 | 35 | |
newk8600 | 0:9c929de0a051 | 36 | const int bmp085_address = 0xEE; //address of bmp085 |
newk8600 | 0:9c929de0a051 | 37 | const int P0 = 101325; //pressure at sea level |
newk8600 | 0:9c929de0a051 | 38 | |
newk8600 | 0:9c929de0a051 | 39 | |
newk8600 | 0:9c929de0a051 | 40 | void bmp085_calibration(void); |
newk8600 | 0:9c929de0a051 | 41 | void display_calibration(void); |
newk8600 | 0:9c929de0a051 | 42 | unsigned short read_short(int,int); |
newk8600 | 0:9c929de0a051 | 43 | void write_char(int,int,int); |
newk8600 | 0:9c929de0a051 | 44 | |
newk8600 | 0:9c929de0a051 | 45 | long get_raw_temp(void); |
newk8600 | 0:9c929de0a051 | 46 | void calculations(long); |
newk8600 | 0:9c929de0a051 | 47 | float get_altitude(long); |
newk8600 | 0:9c929de0a051 | 48 | |
newk8600 | 0:9c929de0a051 | 49 | short AC1, AC2, AC3, B1, B2, MB, MC, MD, OSS; |
newk8600 | 0:9c929de0a051 | 50 | unsigned short AC4, AC5, AC6; |
newk8600 | 0:9c929de0a051 | 51 | |
newk8600 | 0:9c929de0a051 | 52 | |
newk8600 | 0:9c929de0a051 | 53 | |
newk8600 | 0:9c929de0a051 | 54 | |
newk8600 | 0:9c929de0a051 | 55 | |
newk8600 | 0:9c929de0a051 | 56 | |
newk8600 | 0:9c929de0a051 | 57 | int main() { |
newk8600 | 0:9c929de0a051 | 58 | |
newk8600 | 0:9c929de0a051 | 59 | //Initialize |
newk8600 | 0:9c929de0a051 | 60 | OSS = STANDARD; //change between enums under bmp085_oss for desired Sampling resolution |
newk8600 | 0:9c929de0a051 | 61 | |
newk8600 | 0:9c929de0a051 | 62 | //calibrate |
newk8600 | 0:9c929de0a051 | 63 | bmp085_calibration(); |
newk8600 | 0:9c929de0a051 | 64 | display_calibration(); |
newk8600 | 0:9c929de0a051 | 65 | |
newk8600 | 0:9c929de0a051 | 66 | long raw_temp = get_raw_temp(); |
newk8600 | 0:9c929de0a051 | 67 | |
newk8600 | 0:9c929de0a051 | 68 | calculations(raw_temp); |
newk8600 | 0:9c929de0a051 | 69 | |
newk8600 | 0:9c929de0a051 | 70 | //float altitude = get_altitude(pressure); |
newk8600 | 0:9c929de0a051 | 71 | |
newk8600 | 0:9c929de0a051 | 72 | //pc.printf("Temperature: %f\n",temperature); |
newk8600 | 0:9c929de0a051 | 73 | //pc.printf("Pressure: %l\n", pressure); |
newk8600 | 0:9c929de0a051 | 74 | //pc.printf("Altitude: %f\n", altitude); |
newk8600 | 0:9c929de0a051 | 75 | |
newk8600 | 0:9c929de0a051 | 76 | |
newk8600 | 0:9c929de0a051 | 77 | } |
newk8600 | 0:9c929de0a051 | 78 | |
newk8600 | 0:9c929de0a051 | 79 | long get_raw_temp(void) |
newk8600 | 0:9c929de0a051 | 80 | { |
newk8600 | 0:9c929de0a051 | 81 | long raw_temp; |
newk8600 | 0:9c929de0a051 | 82 | |
newk8600 | 0:9c929de0a051 | 83 | //Read raw temperature value |
newk8600 | 0:9c929de0a051 | 84 | write_char(bmp085_address, 0xF4, 0x2E); |
newk8600 | 0:9c929de0a051 | 85 | wait_ms(4.5); |
newk8600 | 0:9c929de0a051 | 86 | raw_temp = read_short(bmp085_address, 0xF6); |
newk8600 | 0:9c929de0a051 | 87 | |
newk8600 | 0:9c929de0a051 | 88 | return raw_temp; |
newk8600 | 0:9c929de0a051 | 89 | } |
newk8600 | 0:9c929de0a051 | 90 | |
newk8600 | 0:9c929de0a051 | 91 | void calculations(long raw_temp) |
newk8600 | 0:9c929de0a051 | 92 | { |
newk8600 | 0:9c929de0a051 | 93 | long X1, X2, B5, temperature; |
newk8600 | 0:9c929de0a051 | 94 | |
newk8600 | 0:9c929de0a051 | 95 | //Start temperature calculation |
newk8600 | 0:9c929de0a051 | 96 | X1 = (((long)raw_temp - (long)AC6) * (long)AC5) >> 15; |
newk8600 | 0:9c929de0a051 | 97 | X2 = ((long)MC << 11) / (X1 + MD); |
newk8600 | 0:9c929de0a051 | 98 | B5 = X1 + X2; |
newk8600 | 0:9c929de0a051 | 99 | temperature = ((B5 + 8) >> 4); |
newk8600 | 0:9c929de0a051 | 100 | |
newk8600 | 0:9c929de0a051 | 101 | temperature = ((float)temperature / 10.0); |
newk8600 | 0:9c929de0a051 | 102 | |
newk8600 | 0:9c929de0a051 | 103 | pc.printf("Temperature: %f\n",temperature); |
newk8600 | 0:9c929de0a051 | 104 | |
newk8600 | 0:9c929de0a051 | 105 | |
newk8600 | 0:9c929de0a051 | 106 | long raw_pressure, B6, B3, pressure, X3; |
newk8600 | 0:9c929de0a051 | 107 | unsigned long B4, B7; |
newk8600 | 0:9c929de0a051 | 108 | |
newk8600 | 0:9c929de0a051 | 109 | //Read raw pressure value |
newk8600 | 0:9c929de0a051 | 110 | write_char(bmp085_address, 0xf4, 0x34 | (OSS << 6)); |
newk8600 | 0:9c929de0a051 | 111 | wait_ms(5); |
newk8600 | 0:9c929de0a051 | 112 | raw_pressure = read_short(bmp085_address, 0xF6) >> (8 - OSS); |
newk8600 | 0:9c929de0a051 | 113 | |
newk8600 | 0:9c929de0a051 | 114 | //Start Pressure calculation |
newk8600 | 0:9c929de0a051 | 115 | B6 = B5 - 4000; |
newk8600 | 0:9c929de0a051 | 116 | X1 = (B2 * (B6 * B6) >> 12) >> 11; |
newk8600 | 0:9c929de0a051 | 117 | X2 = (AC2 * B6) >> 11; |
newk8600 | 0:9c929de0a051 | 118 | X3 = X1 + X2; |
newk8600 | 0:9c929de0a051 | 119 | B3 = (((AC1 * 4 + X3) << OSS + 2) / 4); |
newk8600 | 0:9c929de0a051 | 120 | X1 = (AC3 * B6) >> 13; |
newk8600 | 0:9c929de0a051 | 121 | X2 = (B1 * ((B6 * B6) >> 12)) >> 16; |
newk8600 | 0:9c929de0a051 | 122 | X3 = ((X1 + X2) + 2) >> 2; |
newk8600 | 0:9c929de0a051 | 123 | B4 = (AC4 * (unsigned long)(X3 + 32768)) >> 15; |
newk8600 | 0:9c929de0a051 | 124 | B7 = ((unsigned long)(raw_pressure - B3) * (50000 >> OSS)); |
newk8600 | 0:9c929de0a051 | 125 | if (B7 < 0x80000000) |
newk8600 | 0:9c929de0a051 | 126 | pressure = (B7 *2) / B4; |
newk8600 | 0:9c929de0a051 | 127 | else |
newk8600 | 0:9c929de0a051 | 128 | pressure = (B7 / B4) * 2; |
newk8600 | 0:9c929de0a051 | 129 | |
newk8600 | 0:9c929de0a051 | 130 | X1 = (pressure >> 8) * (pressure >>8); |
newk8600 | 0:9c929de0a051 | 131 | X1 = (X1 * 3038) >>16; |
newk8600 | 0:9c929de0a051 | 132 | X2 = (-7357 * pressure) >>16; |
newk8600 | 0:9c929de0a051 | 133 | pressure += (X1 + X2 + 3791)>>4; |
newk8600 | 0:9c929de0a051 | 134 | |
newk8600 | 0:9c929de0a051 | 135 | pc.printf("Pressure: %f\n", pressure); |
newk8600 | 0:9c929de0a051 | 136 | |
newk8600 | 0:9c929de0a051 | 137 | } |
newk8600 | 0:9c929de0a051 | 138 | |
newk8600 | 0:9c929de0a051 | 139 | float get_altitude(long pressure) |
newk8600 | 0:9c929de0a051 | 140 | { |
newk8600 | 0:9c929de0a051 | 141 | float altitude; |
newk8600 | 0:9c929de0a051 | 142 | |
newk8600 | 0:9c929de0a051 | 143 | altitude = (float)44330 * (1 - pow(( pressure/P0), 0.190295)); |
newk8600 | 0:9c929de0a051 | 144 | |
newk8600 | 0:9c929de0a051 | 145 | pc.printf("Altitude: %f\n", altitude); |
newk8600 | 0:9c929de0a051 | 146 | return altitude; |
newk8600 | 0:9c929de0a051 | 147 | |
newk8600 | 0:9c929de0a051 | 148 | } |
newk8600 | 0:9c929de0a051 | 149 | |
newk8600 | 0:9c929de0a051 | 150 | void bmp085_calibration(void) |
newk8600 | 0:9c929de0a051 | 151 | { |
newk8600 | 0:9c929de0a051 | 152 | AC1 = read_short(bmp085_address, 0xAA); |
newk8600 | 0:9c929de0a051 | 153 | AC2 = read_short(bmp085_address, 0xAC); |
newk8600 | 0:9c929de0a051 | 154 | AC3 = read_short(bmp085_address, 0xAE); |
newk8600 | 0:9c929de0a051 | 155 | AC4 = read_short(bmp085_address, 0xB0); |
newk8600 | 0:9c929de0a051 | 156 | AC5 = read_short(bmp085_address, 0xB2); |
newk8600 | 0:9c929de0a051 | 157 | AC6 = read_short(bmp085_address, 0xB4); |
newk8600 | 0:9c929de0a051 | 158 | B1 = read_short(bmp085_address, 0xB6); |
newk8600 | 0:9c929de0a051 | 159 | B2 = read_short(bmp085_address, 0xB8); |
newk8600 | 0:9c929de0a051 | 160 | MB = read_short(bmp085_address, 0xBA); |
newk8600 | 0:9c929de0a051 | 161 | MC = read_short(bmp085_address, 0xBC); |
newk8600 | 0:9c929de0a051 | 162 | MD = read_short(bmp085_address, 0xBE); |
newk8600 | 0:9c929de0a051 | 163 | |
newk8600 | 0:9c929de0a051 | 164 | } |
newk8600 | 0:9c929de0a051 | 165 | |
newk8600 | 0:9c929de0a051 | 166 | void display_calibration(void) |
newk8600 | 0:9c929de0a051 | 167 | { |
newk8600 | 0:9c929de0a051 | 168 | pc.printf("Calibration Values:\n"); |
newk8600 | 0:9c929de0a051 | 169 | pc.printf("AC1 = %d\n",AC1); |
newk8600 | 0:9c929de0a051 | 170 | pc.printf("AC2 = %d\n",AC2); |
newk8600 | 0:9c929de0a051 | 171 | pc.printf("AC3 = %d\n",AC3); |
newk8600 | 0:9c929de0a051 | 172 | pc.printf("AC4 = %d\n",AC4); |
newk8600 | 0:9c929de0a051 | 173 | pc.printf("AC5 = %d\n",AC5); |
newk8600 | 0:9c929de0a051 | 174 | pc.printf("AC6 = %d\n",AC6); |
newk8600 | 0:9c929de0a051 | 175 | pc.printf("B1 = %d\n",B1); |
newk8600 | 0:9c929de0a051 | 176 | pc.printf("B2 = %d\n",B2); |
newk8600 | 0:9c929de0a051 | 177 | pc.printf("MB = %d\n",MB); |
newk8600 | 0:9c929de0a051 | 178 | pc.printf("MC = %d\n",MC); |
newk8600 | 0:9c929de0a051 | 179 | pc.printf("MD = %d\n",MD); |
newk8600 | 0:9c929de0a051 | 180 | } |
newk8600 | 0:9c929de0a051 | 181 | |
newk8600 | 0:9c929de0a051 | 182 | unsigned short read_short(int device_address,int address){ |
newk8600 | 0:9c929de0a051 | 183 | unsigned short value; |
newk8600 | 0:9c929de0a051 | 184 | |
newk8600 | 0:9c929de0a051 | 185 | i2c.start(); |
newk8600 | 0:9c929de0a051 | 186 | i2c.write(device_address); |
newk8600 | 0:9c929de0a051 | 187 | i2c.write(address); |
newk8600 | 0:9c929de0a051 | 188 | |
newk8600 | 0:9c929de0a051 | 189 | i2c.start(); |
newk8600 | 0:9c929de0a051 | 190 | i2c.write(device_address | 1); |
newk8600 | 0:9c929de0a051 | 191 | value = i2c.read(1) << 8; |
newk8600 | 0:9c929de0a051 | 192 | value |= i2c.read(0); |
newk8600 | 0:9c929de0a051 | 193 | i2c.stop(); |
newk8600 | 0:9c929de0a051 | 194 | |
newk8600 | 0:9c929de0a051 | 195 | return value; |
newk8600 | 0:9c929de0a051 | 196 | |
newk8600 | 0:9c929de0a051 | 197 | } |
newk8600 | 0:9c929de0a051 | 198 | |
newk8600 | 0:9c929de0a051 | 199 | void write_char(int device_address, int address, int data) |
newk8600 | 0:9c929de0a051 | 200 | { |
newk8600 | 0:9c929de0a051 | 201 | i2c.start(); |
newk8600 | 0:9c929de0a051 | 202 | i2c.write(device_address); |
newk8600 | 0:9c929de0a051 | 203 | i2c.write(address); |
newk8600 | 0:9c929de0a051 | 204 | i2c.write(data); |
newk8600 | 0:9c929de0a051 | 205 | i2c.stop(); |
newk8600 | 0:9c929de0a051 | 206 | } |
newk8600 | 0:9c929de0a051 | 207 | |
newk8600 | 0:9c929de0a051 | 208 |