From 560b3f6852f569cb5e42eb430152937440a46cd7 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Sun, 12 Jun 2016 18:24:37 +0200 Subject: BMP280 library and example --- lib/bmp280.h | 132 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 58 deletions(-) (limited to 'lib/bmp280.h') diff --git a/lib/bmp280.h b/lib/bmp280.h index 22472e3..fe8435e 100644 --- a/lib/bmp280.h +++ b/lib/bmp280.h @@ -29,78 +29,94 @@ extern "C" { #endif #define BMP280_AVERAGE_SEA_LEVEL_PRESSURE 1013.25 -#define BMP280_LUFA_SPIO_OPTIONS SPI_SPEED_FCPU_DIV_16 | SPI_MODE_MASTER | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING +#define BMP280_LUFA_SPIO_OPTIONS SPI_SPEED_FCPU_DIV_32 | SPI_MODE_MASTER | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING /*========================================================================= - REGISTERS - -----------------------------------------------------------------------*/ - enum - { - BMP280_REGISTER_DIG_T1 = 0x88, - BMP280_REGISTER_DIG_T2 = 0x8A, - BMP280_REGISTER_DIG_T3 = 0x8C, - - BMP280_REGISTER_DIG_P1 = 0x8E, - BMP280_REGISTER_DIG_P2 = 0x90, - BMP280_REGISTER_DIG_P3 = 0x92, - BMP280_REGISTER_DIG_P4 = 0x94, - BMP280_REGISTER_DIG_P5 = 0x96, - BMP280_REGISTER_DIG_P6 = 0x98, - BMP280_REGISTER_DIG_P7 = 0x9A, - BMP280_REGISTER_DIG_P8 = 0x9C, - BMP280_REGISTER_DIG_P9 = 0x9E, - - BMP280_REGISTER_CHIPID = 0xD0, - BMP280_REGISTER_VERSION = 0xD1, - BMP280_REGISTER_SOFTRESET = 0xE0, - - BMP280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0 - - BMP280_REGISTER_CONTROL = 0xF4, - BMP280_REGISTER_CONFIG = 0xF5, - BMP280_REGISTER_PRESSUREDATA = 0xF7, - BMP280_REGISTER_TEMPDATA = 0xFA, - }; +REGISTERS +-----------------------------------------------------------------------*/ +enum +{ + BMP280_REGISTER_DIG_T1 = 0x88, + BMP280_REGISTER_DIG_T2 = 0x8A, + BMP280_REGISTER_DIG_T3 = 0x8C, + + BMP280_REGISTER_DIG_P1 = 0x8E, + BMP280_REGISTER_DIG_P2 = 0x90, + BMP280_REGISTER_DIG_P3 = 0x92, + BMP280_REGISTER_DIG_P4 = 0x94, + BMP280_REGISTER_DIG_P5 = 0x96, + BMP280_REGISTER_DIG_P6 = 0x98, + BMP280_REGISTER_DIG_P7 = 0x9A, + BMP280_REGISTER_DIG_P8 = 0x9C, + BMP280_REGISTER_DIG_P9 = 0x9E, + + BMP280_REGISTER_CHIPID = 0xD0, + BMP280_REGISTER_VERSION = 0xD1, + BMP280_REGISTER_SOFTRESET = 0xE0, + + BMP280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0 + + BMP280_REGISTER_CONTROL = 0xF4, + BMP280_REGISTER_CONFIG = 0xF5, + BMP280_REGISTER_PRESSUREDATA = 0xF7, + BMP280_REGISTER_TEMPDATA = 0xFA, +}; /*=========================================================================*/ /*========================================================================= - CALIBRATION DATA - -----------------------------------------------------------------------*/ - typedef struct - { - volatile uint8_t *cs_port; - uint8_t cs_pin; - uint16_t dig_T1; - int16_t dig_T2; - int16_t dig_T3; - - uint16_t dig_P1; - int16_t dig_P2; - int16_t dig_P3; - int16_t dig_P4; - int16_t dig_P5; - int16_t dig_P6; - int16_t dig_P7; - int16_t dig_P8; - int16_t dig_P9; - - uint8_t dig_H1; - int16_t dig_H2; - uint8_t dig_H3; - int16_t dig_H4; - int16_t dig_H5; - int8_t dig_H6; - } bmp280_sensor; +CALIBRATION DATA +-----------------------------------------------------------------------*/ +typedef struct +{ + volatile uint8_t *cs_port; + uint8_t cs_pin; + + uint16_t dig_T1; + int16_t dig_T2; + int16_t dig_T3; + + uint16_t dig_P1; + int16_t dig_P2; + int16_t dig_P3; + int16_t dig_P4; + int16_t dig_P5; + int16_t dig_P6; + int16_t dig_P7; + int16_t dig_P8; + int16_t dig_P9; + + uint8_t dig_H1; + int16_t dig_H2; + uint8_t dig_H3; + int16_t dig_H4; + int16_t dig_H5; + int8_t dig_H6; +} bmp280_sensor; /*=========================================================================*/ +typedef struct +{ + float temperature; + float pressure; +} bmp280_result; + //make sure to configure the cs_pin as OUTPUT beforehand //make sure to configure SPI beforehand -void bmp280_init(bmp280_sensor *sensor, volatile uint8_t *cs_port, uint8_t cs_pin); +uint8_t bmp280_init(bmp280_sensor *sensor, volatile uint8_t *cs_port, uint8_t cs_pin); +//check if we deal with am bmp280 chip +uint8_t bmp280_check_chipid(bmp280_sensor *sensor); +//returns temperature in degress celsius float bmp280_readTemp(bmp280_sensor *sensor); +//returns ambient pressure in pascal float bmp280_readPressure(bmp280_sensor *sensor); +//read Temperature and Pressure in one fell swoop +bmp280_result bmp280_readTempAndPressure(bmp280_sensor *sensor); +//calculate altitude from measured pressure and given sealevel-pressure +float bmp280_calcAltitude(float pressure, float sealevelp); +//readPressure and calculate altitude float bmp280_readAltitude(bmp280_sensor *sensor, float sealevelp); -- cgit v1.2.3