[175] | 1 | /***************************************************************************
|
---|
| 2 | This is a library for the BME280 humidity, temperature & pressure sensor
|
---|
| 3 |
|
---|
| 4 | Designed specifically to work with the Adafruit BME280 Breakout
|
---|
| 5 | ----> http://www.adafruit.com/products/2650
|
---|
| 6 |
|
---|
| 7 | These sensors use I2C or SPI to communicate, 2 or 4 pins are required
|
---|
| 8 | to interface.
|
---|
| 9 |
|
---|
| 10 | Adafruit invests time and resources providing this open source code,
|
---|
| 11 | please support Adafruit andopen-source hardware by purchasing products
|
---|
| 12 | from Adafruit!
|
---|
| 13 |
|
---|
| 14 | Written by Limor Fried & Kevin Townsend for Adafruit Industries.
|
---|
| 15 | BSD license, all text above must be included in any redistribution
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | ***************************************************************************/
|
---|
| 19 | #ifndef __BME280_H__
|
---|
| 20 | #define __BME280_H__
|
---|
| 21 |
|
---|
| 22 | #if (ARDUINO >= 100)
|
---|
| 23 | #include "Arduino.h"
|
---|
| 24 | #else
|
---|
| 25 | #include "WProgram.h"
|
---|
| 26 | #endif
|
---|
| 27 |
|
---|
| 28 | #include <Wire.h>
|
---|
| 29 |
|
---|
| 30 | /*=========================================================================
|
---|
| 31 | I2C ADDRESS/BITS
|
---|
| 32 | -----------------------------------------------------------------------*/
|
---|
| 33 | #define BME280_ADDRESS (0x77)
|
---|
| 34 | /*=========================================================================*/
|
---|
| 35 |
|
---|
| 36 | /*=========================================================================
|
---|
| 37 | REGISTERS
|
---|
| 38 | -----------------------------------------------------------------------*/
|
---|
| 39 | enum
|
---|
| 40 | {
|
---|
| 41 | BME280_REGISTER_DIG_T1 = 0x88,
|
---|
| 42 | BME280_REGISTER_DIG_T2 = 0x8A,
|
---|
| 43 | BME280_REGISTER_DIG_T3 = 0x8C,
|
---|
| 44 |
|
---|
| 45 | BME280_REGISTER_DIG_P1 = 0x8E,
|
---|
| 46 | BME280_REGISTER_DIG_P2 = 0x90,
|
---|
| 47 | BME280_REGISTER_DIG_P3 = 0x92,
|
---|
| 48 | BME280_REGISTER_DIG_P4 = 0x94,
|
---|
| 49 | BME280_REGISTER_DIG_P5 = 0x96,
|
---|
| 50 | BME280_REGISTER_DIG_P6 = 0x98,
|
---|
| 51 | BME280_REGISTER_DIG_P7 = 0x9A,
|
---|
| 52 | BME280_REGISTER_DIG_P8 = 0x9C,
|
---|
| 53 | BME280_REGISTER_DIG_P9 = 0x9E,
|
---|
| 54 |
|
---|
| 55 | BME280_REGISTER_DIG_H1 = 0xA1,
|
---|
| 56 | BME280_REGISTER_DIG_H2 = 0xE1,
|
---|
| 57 | BME280_REGISTER_DIG_H3 = 0xE3,
|
---|
| 58 | BME280_REGISTER_DIG_H4 = 0xE4,
|
---|
| 59 | BME280_REGISTER_DIG_H5 = 0xE5,
|
---|
| 60 | BME280_REGISTER_DIG_H6 = 0xE7,
|
---|
| 61 |
|
---|
| 62 | BME280_REGISTER_CHIPID = 0xD0,
|
---|
| 63 | BME280_REGISTER_VERSION = 0xD1,
|
---|
| 64 | BME280_REGISTER_SOFTRESET = 0xE0,
|
---|
| 65 |
|
---|
| 66 | BME280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0
|
---|
| 67 |
|
---|
| 68 | BME280_REGISTER_CONTROLHUMID = 0xF2,
|
---|
| 69 | BME280_REGISTER_CONTROL = 0xF4,
|
---|
| 70 | BME280_REGISTER_CONFIG = 0xF5,
|
---|
| 71 | BME280_REGISTER_PRESSUREDATA = 0xF7,
|
---|
| 72 | BME280_REGISTER_TEMPDATA = 0xFA,
|
---|
| 73 | BME280_REGISTER_HUMIDDATA = 0xFD,
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | /*=========================================================================*/
|
---|
| 77 |
|
---|
| 78 | /*=========================================================================
|
---|
| 79 | CALIBRATION DATA
|
---|
| 80 | -----------------------------------------------------------------------*/
|
---|
| 81 | typedef struct
|
---|
| 82 | {
|
---|
| 83 | uint16_t dig_T1;
|
---|
| 84 | int16_t dig_T2;
|
---|
| 85 | int16_t dig_T3;
|
---|
| 86 |
|
---|
| 87 | uint16_t dig_P1;
|
---|
| 88 | int16_t dig_P2;
|
---|
| 89 | int16_t dig_P3;
|
---|
| 90 | int16_t dig_P4;
|
---|
| 91 | int16_t dig_P5;
|
---|
| 92 | int16_t dig_P6;
|
---|
| 93 | int16_t dig_P7;
|
---|
| 94 | int16_t dig_P8;
|
---|
| 95 | int16_t dig_P9;
|
---|
| 96 |
|
---|
| 97 | uint8_t dig_H1;
|
---|
| 98 | int16_t dig_H2;
|
---|
| 99 | uint8_t dig_H3;
|
---|
| 100 | int16_t dig_H4;
|
---|
| 101 | int16_t dig_H5;
|
---|
| 102 | int8_t dig_H6;
|
---|
| 103 | } bme280_calib_data;
|
---|
| 104 | /*=========================================================================*/
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | class BME280
|
---|
| 109 | {
|
---|
| 110 | public:
|
---|
| 111 |
|
---|
| 112 | bool begin(uint8_t addr = BME280_ADDRESS);
|
---|
| 113 | float temperature(void);
|
---|
| 114 | float pressure(void);
|
---|
| 115 | float humidity(void);
|
---|
| 116 | float altitude(float seaLevel);
|
---|
| 117 |
|
---|
| 118 | private:
|
---|
| 119 |
|
---|
| 120 | void readCoefficients(void);
|
---|
| 121 |
|
---|
| 122 | void write8(byte reg, byte value);
|
---|
| 123 | uint8_t read8(byte reg);
|
---|
| 124 | uint16_t read16(byte reg);
|
---|
| 125 | uint32_t read24(byte reg);
|
---|
| 126 | int16_t readS16(byte reg);
|
---|
| 127 | uint16_t read16_LE(byte reg); // little endian
|
---|
| 128 | int16_t readS16_LE(byte reg); // little endian
|
---|
| 129 |
|
---|
| 130 | uint8_t _i2caddr;
|
---|
| 131 | int32_t _sensorID;
|
---|
| 132 | int32_t t_fine;
|
---|
| 133 |
|
---|
| 134 | bme280_calib_data _bme280_calib;
|
---|
| 135 |
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 | extern BME280 bme280;
|
---|
| 139 |
|
---|
| 140 | #endif
|
---|