1 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the
14 // * All advertising materials mentioning features or use of this
15 // software must display the following acknowledgement: This product
16 // includes software developed by tm3d.de and its contributors.
17 // * Neither the name of tm3d.de nor the names of its contributors may
18 // be used to endorse or promote products derived from this software
19 // without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #define F_CPU 4000000UL
35 #define F_CPU 8000000UL
40 /** Array Index to Field data mapping for Calibration Data*/
41 #define BME680_T2_LSB_REG (1)
42 #define BME680_T2_MSB_REG (2)
43 #define BME680_T3_REG (3)
44 #define BME680_P1_LSB_REG (5)
45 #define BME680_P1_MSB_REG (6)
46 #define BME680_P2_LSB_REG (7)
47 #define BME680_P2_MSB_REG (8)
48 #define BME680_P3_REG (9)
49 #define BME680_P4_LSB_REG (11)
50 #define BME680_P4_MSB_REG (12)
51 #define BME680_P5_LSB_REG (13)
52 #define BME680_P5_MSB_REG (14)
53 #define BME680_P7_REG (15)
54 #define BME680_P6_REG (16)
55 #define BME680_P8_LSB_REG (19)
56 #define BME680_P8_MSB_REG (20)
57 #define BME680_P9_LSB_REG (21)
58 #define BME680_P9_MSB_REG (22)
59 #define BME680_P10_REG (23)
60 #define BME680_H2_MSB_REG (25)
61 #define BME680_H2_LSB_REG (26)
62 #define BME680_H1_LSB_REG (26)
63 #define BME680_H1_MSB_REG (27)
64 #define BME680_H3_REG (28)
65 #define BME680_H4_REG (29)
66 #define BME680_H5_REG (30)
67 #define BME680_H6_REG (31)
68 #define BME680_H7_REG (32)
69 #define BME680_T1_LSB_REG (33)
70 #define BME680_T1_MSB_REG (34)
71 #define BME680_GH2_LSB_REG (35)
72 #define BME680_GH2_MSB_REG (36)
73 #define BME680_GH1_REG (37)
74 #define BME680_GH3_REG (38)
77 #include <util/delay.h>
78 #include <avr/pgmspace.h>
80 #include "USI_TWI_Master.h"
86 volatile uint8_t d[41];
100 uint8_t fr4[2]; //17-18
105 uint16_t h2_; //25 +26
106 uint8_t h1_; //halb und halb 26+27
118 uint16_t h2; //Berechnung
119 uint16_t h1; //Berechnung
120 int32_t t_fine; //Berechnung bei Temperaturmessung
121 uint8_t res_heat_range;/**<resistance calculation*/
122 int8_t res_heat_val; /**<correction factor*/
123 int8_t range_switching_error;/**<range switching error*/
124 int8_t ltemp; //letzte Temperatur
128 volatile calib_t calib;
131 #define BME680_CALIB_I2C_ADDR_1 (0x89)
132 #define BME680_CALIB_I2C_ADDR_2 (0xE1)
133 #define BME680_PAGE0_I2C_ID_REG (0xD0)
134 #define BME680_CALIB_DATA_LENGTH_GAS (25)
135 #define BME680_CALIB_DATA_LENGTH (16)
138 #define BME680_MAX_HUMIDITY_VALUE (102400)
139 #define BME680_MIN_HUMIDITY_VALUE (0)
142 //ME680_CALIB_I2C_ADDR_1,
144 // BME680_CALIB_DATA_LENGTH_GAS);
145 /* read the humidity and gas
147 /* com_status = (enum bme680_return_type)
148 bme680->bme680_bus_read(
150 BME680_CALIB_I2C_ADDR_2,
152 BME680_CALIB_DATA_LENGTH_GAS),
153 BME680_CALIB_DATA_LENGTH);
160 void setup_read(uint8_t addr) {
161 I2c_StartCondition();
163 I2c_WriteByte(addr); //Ctrl hum
164 I2c_StartCondition();
169 void setup_write(uint8_t addr) {
170 I2c_StartCondition();
172 I2c_WriteByte(addr); //Ctrl hum
176 uint8_t readone(uint8_t addr) {
178 uint8_t b=I2c_ReadByte(NO_ACK);
183 void writeone(uint8_t addr,uint8_t b) {
185 I2c_WriteByte(b); //Ctrl hum
189 int8_t initBME680() {
190 uint8_t b1=readone(0xD0);
191 setup_read(BME680_CALIB_I2C_ADDR_1);
192 for(uint8_t i=0;i<BME680_CALIB_DATA_LENGTH_GAS-1;i++) {
193 calib.d[i]=I2c_ReadByte(ACK);
195 calib.d[BME680_CALIB_DATA_LENGTH_GAS-1]=I2c_ReadByte(NO_ACK);
198 setup_read(BME680_CALIB_I2C_ADDR_2);
199 for(uint8_t i=BME680_CALIB_DATA_LENGTH_GAS;i<BME680_CALIB_DATA_LENGTH_GAS+BME680_CALIB_DATA_LENGTH-1;i++) {
200 calib.d[i]=I2c_ReadByte(ACK);
202 calib.d[BME680_CALIB_DATA_LENGTH_GAS+BME680_CALIB_DATA_LENGTH-1]=I2c_ReadByte(NO_ACK);
204 calib.res_heat_range=(readone(0x02)&0x30)>>4;
205 calib.res_heat_val=readone(0);
206 calib.range_switching_error=(readone(0x04)& 0xF0)>>4;
209 calib.h1 = (uint16_t)(((((uint16_t)calib.d[ BME680_H1_MSB_REG]))
210 << 4) | (calib.d[ BME680_H1_LSB_REG] &0x0F));
211 calib.h2 = (uint16_t)(((((uint16_t)calib.d[ BME680_H2_MSB_REG]))
212 << 4) | ((calib.d[ BME680_H2_LSB_REG]) >> 4));
217 I2c_StartCondition();
219 I2c_WriteByte(0x72); //Ctrl hum
220 I2c_WriteByte(0x01); //1x oversembling hum
221 I2c_WriteByte(0x74); //Ctrl hum
222 I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont
233 const float lookup_k1_range[16] PROGMEM = {
234 1, 1, 1, 1, 1,0.99, 1, 0.992,
235 1, 1, 0.998, 0.995, 1, 0.99, 1, 1};
236 const float lookup_k2_range[16] PROGMEM = {
237 8e6, 4e6, 2e6, 1e6,499500.4995, 248262.1648, 125000, 63004.03226,
238 31281.28128, 15625, 7812.5, 3906.25,1953.125,976.5625, 488.28125, 244.140625};
240 double bme680_compensate_gas_double(uint16_t gas_adc_u16, uint8_t gas_range_u8)
242 double gas_res_d = 0;
246 int8_t range_switching_error_val = 0;
249 float a1= pgm_read_float(&(lookup_k1_range[gas_range_u8]));
250 float a2= pgm_read_float(&(lookup_k2_range[gas_range_u8]));
252 range_switching_error_val = calib.range_switching_error;
255 var1 = (1340.0 + (5.0 * range_switching_error_val))*a1;
256 gas_res_d = var1*a2/(gas_adc_u16-512.0+var1);
261 void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){
270 int32_t res_heat_x100 = 0;
271 uint8_t res_heat = 0;
272 uint16_t heater_temp_u16=350;
273 int16_t ambient_temp_s16=calib.ltemp;
274 if ((heater_temp_u16 >= 200) && (heater_temp_u16 <= 400)) {
275 var1 = (((int32_t)ambient_temp_s16 *
276 calib.gh3) / 10) << 8;
277 var2 = (calib.gh1 + 784) *
278 (((((calib.gh2 + 154009) *
279 heater_temp_u16 * 5) / 100) + 3276800) / 10);
280 var3 = var1 + (var2 >> 1);
281 var4 = (var3 / (calib.res_heat_range + 4));
283 var5 = (131 * calib.res_heat_val) + 65536;
285 res_heat_x100 = (int32_t)(((var4 / var5) - 250) * 34);
286 res_heat = (uint8_t) ((res_heat_x100 + 50) / 100);
288 uint16_t duration=100;
291 while ((duration) > 0x3F) {
292 (duration) = (duration) >> 2;
295 (duration) = (duration) + (factor * 64);
297 //I2c_WriteByte(0x74); //Ctrl hum
298 //I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont
299 // [71] <- 10; [72] <- 04; [73] <- 0C; [74] <- 91; [75] <- 00;
300 // [70] <- 00 [71] <- 10; [72] <- 04; [73] <- 0C; [74] <- 91; [75] <- 00;
315 I2c_WriteByte(res_heat);
317 I2c_WriteByte(duration);
325 while ((bx&0x01)==0x01) {
330 //volatile uint8_t rs=readone(0x2B);
333 Ph=I2c_ReadByte(ACK);Ph=Ph<<8;
334 Ph|=I2c_ReadByte(ACK);Ph=Ph<<4;
335 Ph|=I2c_ReadByte(ACK)>>4;
336 Th=I2c_ReadByte(ACK);Th=Th<<8;
337 Th|=I2c_ReadByte(ACK);Th=Th<<4;
338 Th|=I2c_ReadByte(ACK)>>4;
339 Hh=I2c_ReadByte(ACK);Hh=Hh<<8;
340 Hh|=I2c_ReadByte(NO_ACK);
343 volatile uint8_t g1=I2c_ReadByte(ACK);
344 volatile uint8_t g2=I2c_ReadByte(NO_ACK);
346 *G=(((uint16_t)g1)<<2)|(g2>>6);
348 *G= bme680_compensate_gas_double(*G,g2&0xF)/10.0;
350 int32_t temp_comp = 0;
352 var1 = ((int32_t)Th >> 3) -
353 ((int32_t)calib.t1 << 1);
354 var2 = (var1 * (int32_t)calib.t2) >> 11;
355 var3 = ((((var1 >> 1) * (var1 >> 1)) >> 12) *
356 ((int32_t)calib.t3 << 4)) >> 14;
357 calib.t_fine = var2 + var3;
358 temp_comp = ((calib.t_fine * 5) + 128) >> 8;
360 int32_t temp_scaled = 0;
362 int32_t humidity_comp = 0;
364 temp_scaled = (((int32_t)calib.t_fine * 5) + 128) >> 8;
366 ((int32_t)((int32_t)calib.h1 << 4)) -
367 (((temp_scaled * (int32_t)calib.h3) /
368 ((int32_t)100)) >> 1);
370 var2 = ((int32_t)calib.h2 *
371 (((temp_scaled * (int32_t)calib.h4) /
372 ((int32_t)100)) + (((temp_scaled *
373 ((temp_scaled * (int32_t)calib.h5) /
374 ((int32_t)100))) >> 6) / ((int32_t)100)) + (int32_t)(1 << 14))) >> 10;
378 var4 = ((((int32_t)calib.h6) << 7) +
379 ((temp_scaled * (int32_t)calib.h7) /
380 ((int32_t)100))) >> 4;
382 var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;
383 var6 = (var4 * var5) >> 1;
385 humidity_comp = (var3 + var6) >> 12;
386 if (humidity_comp > BME680_MAX_HUMIDITY_VALUE)
387 humidity_comp = BME680_MAX_HUMIDITY_VALUE;
388 else if (humidity_comp < BME680_MIN_HUMIDITY_VALUE)
389 humidity_comp = BME680_MIN_HUMIDITY_VALUE;
391 int32_t pressure_comp = 0;//int -> 5684
393 var1 = (((int32_t)calib.t_fine) >> 1) - (int32_t)64000;
394 var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) *
395 (int32_t)calib.p6) >> 2;
396 var2 = var2 + ((var1 * (int32_t)calib.p5) << 1);
397 var2 = (var2 >> 2) + ((int32_t)calib.p4 << 16);
398 var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *
399 ((int32_t)calib.p3 << 5)) >> 3) +
400 (((int32_t)calib.p2 * var1) >> 1);
402 var1 = (((int32_t)32768 + var1) * (int32_t)calib.p1) >> 15;
403 pressure_comp = (int32_t)1048576 - Ph;
404 pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125));
405 var4 = ((int32_t)1 << 31);
406 if (pressure_comp >= var4)
407 pressure_comp = ((pressure_comp / (uint32_t)var1) << 1);
409 pressure_comp = ((pressure_comp << 1) / (uint32_t)var1);
410 var1 = ((int32_t)calib.p9 * (int32_t)(((pressure_comp >> 3) *
411 (pressure_comp >> 3)) >> 13)) >> 12;
412 var2 = ((int32_t)(pressure_comp >> 2) *
413 (int32_t)calib.p8) >> 13;
414 var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) *
415 (int32_t)(pressure_comp >> 8) *
416 (int32_t)calib.p10) >> 17;
418 pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 +
419 ((int32_t)calib.p7 << 7)) >> 4);
425 *T=(int16_t)temp_comp;
426 calib.ltemp=temp_comp/100;
428 *H=(uint16_t)(humidity_comp/10);