First commit

This commit is contained in:
KTOWN
2013-02-14 15:37:29 +01:00
parent 05b7b4ecb6
commit ed98fdd5c1
4 changed files with 606 additions and 3 deletions

375
Adafruit_BMP085.cpp Normal file
View File

@ -0,0 +1,375 @@
/***************************************************************************
This is a library for the BMP085 pressure sensor
Designed specifically to work with the Adafruit BMP085 Breakout
These displays use I2C to communicate, 2 pins are required to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <limits.h>
#include "Adafruit_BMP085.h"
static bmp085_calib_data _bmp085_coeffs; // Last read accelerometer data will be available here
static uint8_t _bmp085Mode;
#define BMP085_USE_DATASHEET_VALS (0) /* Set to 1 for sanity check */
/***************************************************************************
PRIVATE FUNCTIONS
***************************************************************************/
/**************************************************************************/
/*!
@brief Writes an 8 bit value over I2C
*/
/**************************************************************************/
static void writeCommand(byte reg, byte value)
{
Wire.beginTransmission((uint8_t)BMP085_ADDRESS);
#if ARDUINO >= 100
Wire.write((uint8_t)reg);
Wire.write((uint8_t)value);
#else
Wire.send(reg);
Wire.send(value);
#endif
Wire.endTransmission();
}
/**************************************************************************/
/*!
@brief Reads an 8 bit value over I2C
*/
/**************************************************************************/
static void read8(byte reg, uint8_t *value)
{
Wire.beginTransmission((uint8_t)BMP085_ADDRESS);
#if ARDUINO >= 100
Wire.write((uint8_t)reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.requestFrom((uint8_t)BMP085_ADDRESS, (byte)1);
#if ARDUINO >= 100
*value = Wire.read();
#else
*value = Wire.receive();
#endif
Wire.endTransmission();
}
/**************************************************************************/
/*!
@brief Reads a 16 bit value over I2C
*/
/**************************************************************************/
static void read16(byte reg, uint16_t *value)
{
Wire.beginTransmission((uint8_t)BMP085_ADDRESS);
#if ARDUINO >= 100
Wire.write((uint8_t)reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.requestFrom((uint8_t)BMP085_ADDRESS, (byte)2);
#if ARDUINO >= 100
*value = (Wire.read() << 8) | Wire.read();
#else
*value = (Wire.receive() << 8) | Wire.receive();
#endif
Wire.endTransmission();
}
/**************************************************************************/
/*!
@brief Reads a signed 16 bit value over I2C
*/
/**************************************************************************/
static void readS16(byte reg, int16_t *value)
{
uint16_t i;
read16(reg, &i);
*value = (int16_t)i;
}
/**************************************************************************/
/*!
@brief Reads the factory-set coefficients
*/
/**************************************************************************/
static void readCoefficients(void)
{
#if BMP085_USE_DATASHEET_VALS
_bmp085_coeffs.ac1 = 408;
_bmp085_coeffs.ac2 = -72;
_bmp085_coeffs.ac3 = -14383;
_bmp085_coeffs.ac4 = 32741;
_bmp085_coeffs.ac5 = 32757;
_bmp085_coeffs.ac6 = 23153;
_bmp085_coeffs.b1 = 6190;
_bmp085_coeffs.b2 = 4;
_bmp085_coeffs.mb = -32768;
_bmp085_coeffs.mc = -8711;
_bmp085_coeffs.md = 2868;
_bmp085Mode = 0;
#else
readS16(BMP085_REGISTER_CAL_AC1, &_bmp085_coeffs.ac1);
readS16(BMP085_REGISTER_CAL_AC2, &_bmp085_coeffs.ac2);
readS16(BMP085_REGISTER_CAL_AC3, &_bmp085_coeffs.ac3);
read16(BMP085_REGISTER_CAL_AC4, &_bmp085_coeffs.ac4);
read16(BMP085_REGISTER_CAL_AC5, &_bmp085_coeffs.ac5);
read16(BMP085_REGISTER_CAL_AC6, &_bmp085_coeffs.ac6);
readS16(BMP085_REGISTER_CAL_B1, &_bmp085_coeffs.b1);
readS16(BMP085_REGISTER_CAL_B2, &_bmp085_coeffs.b2);
readS16(BMP085_REGISTER_CAL_MB, &_bmp085_coeffs.mb);
readS16(BMP085_REGISTER_CAL_MC, &_bmp085_coeffs.mc);
readS16(BMP085_REGISTER_CAL_MD, &_bmp085_coeffs.md);
#endif
}
/**************************************************************************/
/*!
*/
/**************************************************************************/
static void readRawTemperature(int32_t *temperature)
{
#if BMP085_USE_DATASHEET_VALS
*temperature = 27898;
#else
uint16_t t;
writeCommand(BMP085_REGISTER_CONTROL, BMP085_REGISTER_READTEMPCMD);
delay(5);
read16(BMP085_REGISTER_TEMPDATA, &t);
*temperature = t;
#endif
}
/**************************************************************************/
/*!
*/
/**************************************************************************/
static void readRawPressure(int32_t *pressure)
{
#if BMP085_USE_DATASHEET_VALS
*pressure = 23843;
#else
uint8_t p8;
uint16_t p16;
int32_t p32;
writeCommand(BMP085_REGISTER_CONTROL, BMP085_REGISTER_READPRESSURECMD + (_bmp085Mode << 6));
switch(_bmp085Mode)
{
case BMP085_MODE_ULTRALOWPOWER:
delay(5);
break;
case BMP085_MODE_STANDARD:
delay(8);
break;
case BMP085_MODE_HIGHRES:
delay(14);
break;
case BMP085_MODE_ULTRAHIGHRES:
default:
delay(26);
break;
}
read16(BMP085_REGISTER_PRESSUREDATA, &p16);
p32 = (uint32_t)p16 << 8;
read8(BMP085_REGISTER_PRESSUREDATA+2, &p8);
p32 += p8;
p32 >>= (8 - _bmp085Mode);
*pressure = p32;
#endif
}
/***************************************************************************
CONSTRUCTOR
***************************************************************************/
/**************************************************************************/
/*!
@brief Instantiates a new Adafruit_BMP085 class
*/
/**************************************************************************/
Adafruit_BMP085::Adafruit_BMP085(int32_t sensorID) {
_sensorID = sensorID;
}
/***************************************************************************
PUBLIC FUNCTIONS
***************************************************************************/
/**************************************************************************/
/*!
@brief Setups the HW
*/
/**************************************************************************/
bool Adafruit_BMP085::begin(bmp085_mode_t mode)
{
// Enable I2C
Wire.begin();
/* Mode boundary check */
if ((mode > BMP085_MODE_ULTRAHIGHRES) || (mode < 0))
{
mode = BMP085_MODE_ULTRAHIGHRES;
}
/* Make sure we have the right device */
uint8_t id;
read8(BMP085_REGISTER_CHIPID, &id);
if(id != 0x55)
{
return false;
}
/* Set the mode indicator */
_bmp085Mode = mode;
/* Coefficients need to be read once */
readCoefficients();
return true;
}
/**************************************************************************/
/*!
@brief Gets the compensated pressure level in kPa
*/
/**************************************************************************/
void Adafruit_BMP085::getPressure(float *pressure)
{
int32_t ut = 0, up = 0, compp = 0;
int32_t x1, x2, b5, b6, x3, b3, p;
uint32_t b4, b7;
/* Get the raw pressure and temperature values */
readRawTemperature(&ut);
readRawPressure(&up);
/* Temperature compensation */
x1 = (ut - (int32_t)(_bmp085_coeffs.ac6))*((int32_t)(_bmp085_coeffs.ac5))/pow(2,15);
x2 = ((int32_t)(_bmp085_coeffs.mc*pow(2,11)))/(x1+(int32_t)(_bmp085_coeffs.md));
b5 = x1 + x2;
/* Pressure compensation */
b6 = b5 - 4000;
x1 = (_bmp085_coeffs.b2 * ((b6 * b6) >> 12)) >> 11;
x2 = (_bmp085_coeffs.ac2 * b6) >> 11;
x3 = x1 + x2;
b3 = (((((int32_t) _bmp085_coeffs.ac1) * 4 + x3) << _bmp085Mode) + 2) >> 2;
x1 = (_bmp085_coeffs.ac3 * b6) >> 13;
x2 = (_bmp085_coeffs.b1 * ((b6 * b6) >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (_bmp085_coeffs.ac4 * (uint32_t) (x3 + 32768)) >> 15;
b7 = ((uint32_t) (up - b3) * (50000 >> _bmp085Mode));
if (b7 < 0x80000000)
{
p = (b7 << 1) / b4;
}
else
{
p = (b7 / b4) << 1;
}
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
compp = p + ((x1 + x2 + 3791) >> 4);
/* Assign compensated pressure value */
*pressure = compp;
}
/**************************************************************************/
/*!
@brief Reads the temperatures in degrees Celsius
*/
/**************************************************************************/
void Adafruit_BMP085::getTemperature(float *temp)
{
int32_t ut, x1, x2;
readRawTemperature(&ut);
x1 = (ut - _bmp085_coeffs.ac6) * _bmp085_coeffs.ac5 >> 15;
x2 = (_bmp085_coeffs.mc << 11) / (x1 + _bmp085_coeffs.md);
*temp = (x1+x2+8) >> 4;
*temp /= 10;
}
/**************************************************************************/
/*!
Converts pressure in hPa to altitude in meters
*/
/**************************************************************************/
float Adafruit_BMP085::pressureToAltitude(float pressure_hPa)
{
return (pressure_hPa
/ (float) pow(1.0F - (SENSORS_PRESSURE_SEALEVELHPA / 44330.0f), 5.255f))
/ 10;
}
/**************************************************************************/
/*!
@brief Provides the sensor_t data for this sensor
*/
/**************************************************************************/
void Adafruit_BMP085::getSensor(sensor_t *sensor)
{
/* Clear the sensor_t object */
memset(sensor, 0, sizeof(sensor_t));
/* Insert the sensor name in the fixed length char array */
strncpy (sensor->name, "BMP085", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name)- 1] = 0;
sensor->version = 1;
sensor->sensor_id = _sensorID;
sensor->type = SENSOR_TYPE_PRESSURE;
sensor->min_delay = 0;
sensor->max_value = 300.0F; // 300..1100 hPa
sensor->min_value = 1100.0F;
sensor->resolution = 0.01F; // Datasheet states 0.01 hPa resolution
}
/**************************************************************************/
/*!
@brief Reads the sensor and returns the data as a sensors_event_t
*/
/**************************************************************************/
void Adafruit_BMP085::getEvent(sensors_event_t *event)
{
float pressure_kPa;
/* Clear the event */
memset(event, 0, sizeof(sensors_event_t));
event->version = sizeof(sensors_event_t);
event->sensor_id = _sensorID;
event->type = SENSOR_TYPE_PRESSURE;
event->timestamp = 0;
getPressure(&pressure_kPa);
event->pressure = pressure_kPa / 100.0F; /* kPa to hPa */
}

107
Adafruit_BMP085.h Normal file
View File

@ -0,0 +1,107 @@
/***************************************************************************
This is a library for the BMP085 pressure sensor
Designed specifically to work with the Adafruit BMP085 Breakout
These displays use I2C to communicate, 2 pins are required to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#ifndef __BMP085_H__
#define __BMP085_H__
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Adafruit_Sensor.h>
#include <Wire.h>
/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
#define BMP085_ADDRESS (0x77)
/*=========================================================================*/
/*=========================================================================
REGISTERS
-----------------------------------------------------------------------*/
enum
{
BMP085_REGISTER_CAL_AC1 = 0xAA, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_AC2 = 0xAC, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_AC3 = 0xAE, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_AC4 = 0xB0, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_AC5 = 0xB2, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_AC6 = 0xB4, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_B1 = 0xB6, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_B2 = 0xB8, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_MB = 0xBA, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_MC = 0xBC, // R Calibration data (16 bits)
BMP085_REGISTER_CAL_MD = 0xBE, // R Calibration data (16 bits)
BMP085_REGISTER_CHIPID = 0xD0,
BMP085_REGISTER_VERSION = 0xD1,
BMP085_REGISTER_SOFTRESET = 0xE0,
BMP085_REGISTER_CONTROL = 0xF4,
BMP085_REGISTER_TEMPDATA = 0xF6,
BMP085_REGISTER_PRESSUREDATA = 0xF6,
BMP085_REGISTER_READTEMPCMD = 0x2E,
BMP085_REGISTER_READPRESSURECMD = 0x34
};
/*=========================================================================*/
/*=========================================================================
MODE SETTINGS
-----------------------------------------------------------------------*/
typedef enum
{
BMP085_MODE_ULTRALOWPOWER = 0,
BMP085_MODE_STANDARD = 1,
BMP085_MODE_HIGHRES = 2,
BMP085_MODE_ULTRAHIGHRES = 3
} bmp085_mode_t;
/*=========================================================================*/
/*=========================================================================
CALIBRATION DATA
-----------------------------------------------------------------------*/
typedef struct
{
int16_t ac1;
int16_t ac2;
int16_t ac3;
uint16_t ac4;
uint16_t ac5;
uint16_t ac6;
int16_t b1;
int16_t b2;
int16_t mb;
int16_t mc;
int16_t md;
} bmp085_calib_data;
/*=========================================================================*/
class Adafruit_BMP085 : public Adafruit_Sensor
{
public:
Adafruit_BMP085(int32_t sensorID = -1);
bool begin(bmp085_mode_t mode = BMP085_MODE_ULTRAHIGHRES);
void getTemperature(float *temp);
void getPressure(float *pressure);
float pressureToAltitude(float pressure_hPa);
void getEvent(sensors_event_t*);
void getSensor(sensor_t*);
private:
int32_t _sensorID;
};
#endif

View File

@ -1,4 +1,27 @@
Adafruit_BMP085_Unified
=======================
#Adafruit Unified BMP085 Driver (Barometric Pressure Sensor) #
Unified sensor driver for Adafruit's BMP085 breakouts
This driver is for the Adafruit BMP085 Breakout (http://www.adafruit.com/products/391), and is based on Adafruit's Unified Sensor Library (Adafruit_Sensor).
## About the BMP085 ##
This precision sensor from Bosch is the best low-cost sensing solution for measuring barometric pressure and temperature. Because pressure changes with altitude you can also use it as an altimeter!
## What is the Adafruit Unified Sensor Library? ##
The Adafruit Unified Sensor Library (**Adafruit_Sensor**) provides a common interface and data type for any supported sensor. It defines some basic information about the sensor (sensor limits, etc.), and returns standard SI units of a specific type and scale for each supported sensor type.
It provides a simple abstraction layer between your application and the actual sensor HW, allowing you to drop in any comparable sensor with only one or two lines of code to change in your project (essentially the constructor since the functions to read sensor data and get information about the sensor are defined in the base Adafruit_Sensor class).
This is imporant useful for two reasons:
1.) You can use the data right away because it's already converted to SI units that you understand and can compare, rather than meaningless values like 0..1023.
2.) Because SI units are standardised in the sensor library, you can also do quick sanity checks when working with new sensors, or drop in any comparable sensor if you need better sensitivity or if a lower cost unit becomes available, etc.
Light sensors will always report units in lux, gyroscopes will always report units in rad/s, etc. ... freeing you up to focus on the data, rather than digging through the datasheet to understand what the sensor's raw numbers really mean.
## About this Driver ##
Adafruit invests time and resources providing this open source code. Please support Adafruit and open-source hardware by purchasing products from Adafruit!
Written by Kevin (KTOWN) Townsend for Adafruit Industries.

View File

@ -0,0 +1,98 @@
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
which provides a common 'type' for sensor data and some helper functions.
To use this driver you will also need to download the Adafruit_Sensor
library and include it in your libraries folder.
You should also assign a unique ID to this sensor for use with
the Adafruit Sensor API so that you can identify this particular
sensor in any data logs, etc. To assign a unique ID, simply
provide an appropriate value in the constructor below (12345
is used by default in this example).
Connections
===========
Connect SCL to analog 5
Connect SDA to analog 4
Connect VDD to 3.3V DC
Connect GROUND to common ground
History
=======
2013/FEB/13 - First version (KTOWN)
*/
Adafruit_BMP085 bmp = Adafruit_BMP085(10085);
/**************************************************************************/
/*
Displays some basic information on this sensor from the unified
sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
sensor_t sensor;
bmp.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" hPa");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" hPa");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" hPa");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
/**************************************************************************/
/*
Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
Serial.println("Pressure Sensor Test"); Serial.println("");
/* Initialise the sensor */
if(!bmp.begin())
{
/* There was a problem detecting the BMP085 ... check your connections */
Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
while(1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
}
/**************************************************************************/
/*
Arduino loop function, called once 'setup' is complete (your own code
should go here)
*/
/**************************************************************************/
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
bmp.getEvent(&event);
/* Display the results (barometric pressure is measure in hPa) */
if (event.pressure)
{
Serial.print(event.pressure); Serial.print(" hPa");
Serial.print(" ("); Serial.print(bmp.pressureToAltitude(event.pressure)); Serial.println(" m)");
}
else
{
Serial.println("Sensor error");
}
delay(250);
}