YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
bme280_drv.h
1/*
2 * bme280_drv.h
3 *
4 * Created on: Mar 15, 2017
5 * Author: Dennis Hoffmann
6 */
7
8#ifndef SRC_DRIVER_BME280_DRV_H_
9#define SRC_DRIVER_BME280_DRV_H_
10
11#include "i2c_interface.h"
12
13namespace BME280 {
14 // SDO connected to GND
15 static const uint8_t ADDRESS = 0x76;
16
17 // value softreset
18 static const uint8_t RESET = 0xB6;
19
20 // V_DDIO connected to GND
21 //static const uint8_t ADDRESS = 0x77;
22 static const uint8_t REG_HUM_LSB = 0xFE;
23 static const uint8_t REG_HUM_MSB = 0xFD;
24 static const uint8_t REG_TEMP_XLSB = 0xFC;
25 static const uint8_t REG_TEMP_LSB = 0xFB;
26 static const uint8_t REG_TEMP_MSB = 0xFA;
27 static const uint8_t REG_PRESS_XLSB = 0xF9;
28 static const uint8_t REG_PRESS_LSB = 0xF8;
29 static const uint8_t REG_PRESS_MSB = 0xF7;
30 static const uint8_t REG_CONFIG = 0xF5;
31 static const uint8_t REG_CTRL_MEAS = 0xF4;
32 static const uint8_t REG_STATUS = 0xF3;
33 static const uint8_t REG_CTRL_HUM = 0xF2;
34 static const uint8_t REG_RESET = 0xE0;
35 static const uint8_t REG_ID = 0xD0;// ID = 0x60
36
37 //Calibration Data Register
38 //temperature
39 static const uint8_t REG_DIG_T1 = 0x88;
40 static const uint8_t REG_DIG_T2 = 0x8A;
41 static const uint8_t REG_DIG_T3 = 0x8C;
42
43 //pressure
44 static const uint8_t REG_DIG_P1 = 0x8E;
45 static const uint8_t REG_DIG_P2 = 0x90;
46 static const uint8_t REG_DIG_P3 = 0x92;
47 static const uint8_t REG_DIG_P4 = 0x94;
48 static const uint8_t REG_DIG_P5 = 0x96;
49 static const uint8_t REG_DIG_P6 = 0x98;
50 static const uint8_t REG_DIG_P7 = 0x9A;
51 static const uint8_t REG_DIG_P8 = 0x9C;
52 static const uint8_t REG_DIG_P9 = 0x9E;
53
54 //humidity
55 static const uint8_t REG_DIG_H1 = 0xA1;
56 static const uint8_t REG_DIG_H2 = 0xE1;
57 static const uint8_t REG_DIG_H3 = 0xE3;
58 static const uint8_t REG_DIG_H4 = 0xE4;
59 static const uint8_t REG_DIG_H5 = 0xE5;
60 static const uint8_t REG_DIG_H6 = 0xE7;
61
62 enum class SAMPLING : uint8_t {
63 OFF = 0b000,
64 X1 = 0b001,
65 X2 = 0b010,
66 X4 = 0b011,
67 X8 = 0b100,
68 X16 = 0b101
69 };
70
71 enum class MODE : uint8_t {
72 SLEEP = 0b000,
73 FORCED = 0b001,
74 NORMAL = 0b011
75 };
76
77 enum class FILTER : uint8_t {
78 OFF = 0b000,
79 X2 = 0b001,
80 X4 = 0b010,
81 X8 = 0b011,
82 X16 = 0b100
83 };
84
85 enum class STANDBY_TIME : uint8_t {
86 MS_0 = 0b000, // 0,5ms
87 MS_62 = 0b001, // 62,5ms
88 MS_125 = 0b010,
89 MS_250 = 0b011,
90 MS_500 = 0b100,
91 MS_1000 = 0b101,
92 MS_10 = 0b110,
93 MS_20 = 0b111
94 };
95
97 //temperature
98 uint16_t dig_T1;
99 int16_t dig_T2;
100 int16_t dig_T3;
101
102 //pressure
103 uint16_t dig_P1;
104 int16_t dig_P2;
105 int16_t dig_P3;
106 int16_t dig_P4;
107 int16_t dig_P5;
108 int16_t dig_P6;
109 int16_t dig_P7;
110 int16_t dig_P8;
111 int16_t dig_P9;
112
113 //humidity
114 uint8_t dig_H1;
115 int16_t dig_H2;
116 uint8_t dig_H3;
117 int16_t dig_H4;
118 int16_t dig_H5;
119 int8_t dig_H6;
120 };
121
122}
123
125
126public:
127 bme280_drv(i2c_interface & i2c, uint8_t _i2c_addr);
128 float get_temperature();
129 float get_pressure();
130 float get_humidity();
131 void set_sampling(BME280::MODE m,
132 BME280::FILTER f,
133 BME280::STANDBY_TIME t,
134 BME280::SAMPLING s_temp,
135 BME280::SAMPLING s_press,
136 BME280::SAMPLING s_hum);
137
138 void get_calibration_data();
139 bool detect_sensor();
140
141private:
142 i2c_interface & _i2c;
143 uint8_t _i2c_addr;
144 int32_t t_fine;
145 BME280::CALIBRATION_DATA _calibration_data;
146 void soft_reset();
147
148 void writeRegister(uint8_t reg, uint8_t val);
149 uint8_t readRegister(uint8_t reg);
150 uint32_t read_u24(uint8_t reg);
151 uint16_t read_u16(uint8_t reg);
152 uint16_t read_u16_le(uint8_t reg);
153 uint16_t read_s16_le(uint8_t reg);
154};
155
156#endif /* SRC_DRIVER_BME280_DRV_H_ */