Kory Hill
/
BMP085_lib
demo program of BMP085 pressure sensor
Diff: main.cpp
- Revision:
- 1:2935199329b2
- Parent:
- 0:9c929de0a051
- Child:
- 2:8c00953d4755
- Child:
- 4:227f0a670091
--- a/main.cpp Fri Oct 05 08:03:52 2012 +0000 +++ b/main.cpp Mon Nov 05 19:31:55 2012 +0000 @@ -22,95 +22,125 @@ * * */ -#include "mbed.h" + +class BMP085 +{ + +public: + BMP085(); + //BMP085(); + + float get_temperature(); + long get_pressure(); + void calculations(long); + +protected: + void init(BMP085_OSS); + unsigned short read_short (int, int); + void write_char (int, int, int); + + I2C i2c; + float temperature; + long pressure; + +private: + short AC1, AC2, AC3, B1, B2, MB, MC, MD, OSS; + unsigned short AC4, AC5, AC6; +}; + +#include "mbed.h" enum bmp085_oss {ULTRALOW, //samples 1 time STANDARD, //samples twice HIGH, //samples 4 times ULTRAHIGH};//samples 8 times - -I2C i2c(p9, p10); // sda, scl + +I2C i2c(p9, p10); // sda, scl Serial pc(USBTX, USBRX); // tx, rx const int bmp085_address = 0xEE; //address of bmp085 -const int P0 = 101325; //pressure at sea level +const int P0 = 101325; //pressure at sea level void bmp085_calibration(void); -void display_calibration(void); +void display_calibration(void); unsigned short read_short(int,int); void write_char(int,int,int); long get_raw_temp(void); void calculations(long); + +float get_temperature(void); +long get_pressure(void); float get_altitude(long); - -short AC1, AC2, AC3, B1, B2, MB, MC, MD, OSS; -unsigned short AC4, AC5, AC6; - + +//short AC1, AC2, AC3, B1, B2, MB, MC, MD, OSS; +//unsigned short AC4, AC5, AC6; + -int main() { - +int main() +{ + //Initialize OSS = STANDARD; //change between enums under bmp085_oss for desired Sampling resolution - - //calibrate + + //calibrate bmp085_calibration(); - display_calibration(); - + display_calibration(); + long raw_temp = get_raw_temp(); - + calculations(raw_temp); - + //float altitude = get_altitude(pressure); - + //pc.printf("Temperature: %f\n",temperature); //pc.printf("Pressure: %l\n", pressure); //pc.printf("Altitude: %f\n", altitude); - - + + } long get_raw_temp(void) -{ - long raw_temp; - - //Read raw temperature value +{ + long raw_temp; + + //Read raw temperature value write_char(bmp085_address, 0xF4, 0x2E); wait_ms(4.5); raw_temp = read_short(bmp085_address, 0xF6); - + return raw_temp; } void calculations(long raw_temp) -{ - long X1, X2, B5, temperature; - +{ + long X1, X2, B5; //temperature; + //Start temperature calculation X1 = (((long)raw_temp - (long)AC6) * (long)AC5) >> 15; X2 = ((long)MC << 11) / (X1 + MD); B5 = X1 + X2; temperature = ((B5 + 8) >> 4); - + temperature = ((float)temperature / 10.0); - + pc.printf("Temperature: %f\n",temperature); - - - long raw_pressure, B6, B3, pressure, X3; + + + long raw_pressure, B6, B3, X3; unsigned long B4, B7; - + //Read raw pressure value write_char(bmp085_address, 0xf4, 0x34 | (OSS << 6)); wait_ms(5); raw_pressure = read_short(bmp085_address, 0xF6) >> (8 - OSS); - + //Start Pressure calculation B6 = B5 - 4000; X1 = (B2 * (B6 * B6) >> 12) >> 11; @@ -126,45 +156,55 @@ pressure = (B7 *2) / B4; else pressure = (B7 / B4) * 2; - + X1 = (pressure >> 8) * (pressure >>8); X1 = (X1 * 3038) >>16; X2 = (-7357 * pressure) >>16; pressure += (X1 + X2 + 3791)>>4; - + pc.printf("Pressure: %f\n", pressure); - + +} + +float get_temperature(void) +{ + return temperature; } - + +long get_pressure(void) +{ + return pressure; +} + float get_altitude(long pressure) { float altitude; - - altitude = (float)44330 * (1 - pow(( pressure/P0), 0.190295)); - + + altitude = (float)44330 * (1 - pow(( pressure/P0), 0.190295)); + pc.printf("Altitude: %f\n", altitude); - return altitude; - + return altitude; + } void bmp085_calibration(void) -{ +{ AC1 = read_short(bmp085_address, 0xAA); AC2 = read_short(bmp085_address, 0xAC); AC3 = read_short(bmp085_address, 0xAE); - AC4 = read_short(bmp085_address, 0xB0); + AC4 = read_short(bmp085_address, 0xB0); AC5 = read_short(bmp085_address, 0xB2); AC6 = read_short(bmp085_address, 0xB4); B1 = read_short(bmp085_address, 0xB6); B2 = read_short(bmp085_address, 0xB8); MB = read_short(bmp085_address, 0xBA); MC = read_short(bmp085_address, 0xBC); - MD = read_short(bmp085_address, 0xBE); - + MD = read_short(bmp085_address, 0xBE); + } void display_calibration(void) -{ +{ pc.printf("Calibration Values:\n"); pc.printf("AC1 = %d\n",AC1); pc.printf("AC2 = %d\n",AC2); @@ -176,24 +216,25 @@ pc.printf("B2 = %d\n",B2); pc.printf("MB = %d\n",MB); pc.printf("MC = %d\n",MC); - pc.printf("MD = %d\n",MD); + pc.printf("MD = %d\n",MD); } - -unsigned short read_short(int device_address,int address){ + +unsigned short read_short(int device_address,int address) +{ unsigned short value; - + i2c.start(); i2c.write(device_address); i2c.write(address); - + i2c.start(); i2c.write(device_address | 1); value = i2c.read(1) << 8; value |= i2c.read(0); i2c.stop(); - + return value; - + } void write_char(int device_address, int address, int data) @@ -203,6 +244,5 @@ i2c.write(address); i2c.write(data); i2c.stop(); -} - - \ No newline at end of file +} +