Linux Git
[owSlave2.git] / common / I2C / BME680.c
index 3f425ba..9907867 100644 (file)
-// Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-//  * Redistributions of source code must retain the above copyright
-//    notice, this list of conditions and the following disclaimer.
-//  * Redistributions in binary form must reproduce the above copyright
-//    notice, this list of conditions and the following disclaimer in the
-//    documentation and/or other materials provided with the
-//    distribution.
-//  * All advertising materials mentioning features or use of this
-//    software must display the following acknowledgement: This product
-//    includes software developed by tm3d.de and its contributors.
-//  * Neither the name of tm3d.de nor the names of its contributors may
-//    be used to endorse or promote products derived from this software
-//    without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#ifdef  __4MHZ__
-#define F_CPU 4000000UL
-#else
-#define F_CPU 8000000UL
-#endif
-#include <avr/io.h>
-
-
-/** Array Index to Field data mapping for Calibration Data*/
-#define BME680_T2_LSB_REG      (1)
-#define BME680_T2_MSB_REG      (2)
-#define BME680_T3_REG          (3)
-#define BME680_P1_LSB_REG      (5)
-#define BME680_P1_MSB_REG      (6)
-#define BME680_P2_LSB_REG      (7)
-#define BME680_P2_MSB_REG      (8)
-#define BME680_P3_REG          (9)
-#define BME680_P4_LSB_REG      (11)
-#define BME680_P4_MSB_REG      (12)
-#define BME680_P5_LSB_REG      (13)
-#define BME680_P5_MSB_REG      (14)
-#define BME680_P7_REG          (15)
-#define BME680_P6_REG          (16)
-#define BME680_P8_LSB_REG      (19)
-#define BME680_P8_MSB_REG      (20)
-#define BME680_P9_LSB_REG      (21)
-#define BME680_P9_MSB_REG      (22)
-#define BME680_P10_REG         (23)
-#define BME680_H2_MSB_REG      (25)
-#define BME680_H2_LSB_REG      (26)
-#define BME680_H1_LSB_REG      (26)
-#define BME680_H1_MSB_REG      (27)
-#define BME680_H3_REG          (28)
-#define BME680_H4_REG          (29)
-#define BME680_H5_REG          (30)
-#define BME680_H6_REG          (31)
-#define BME680_H7_REG          (32)
-#define BME680_T1_LSB_REG      (33)
-#define BME680_T1_MSB_REG      (34)
-#define BME680_GH2_LSB_REG     (35)
-#define BME680_GH2_MSB_REG     (36)
-#define BME680_GH1_REG         (37)
-#define BME680_GH3_REG (38)
-
-
-#include <util/delay.h>
-#include <avr/pgmspace.h>
-
-#include "USI_TWI_Master.h"
-#include "BME680.h"
-
-#define WC 0b11101100
-#define RC 0b11101101
-typedef union {
-       volatile uint8_t d[41];
-       struct {
-               uint8_t fr1;
-               int16_t t2;
-               int8_t t3;
-               uint8_t fr2; //4
-               uint16_t p1;
-               int16_t p2; //7
-               int8_t p3;
-               uint8_t fr3; //10
-               int16_t p4;
-               int16_t p5;
-               int8_t p7;
-               int8_t p6;
-               uint8_t fr4[2]; //17-18
-               int16_t p8;
-               int16_t p9;
-               uint8_t p10;
-               uint8_t fr5; //24
-               uint16_t h2_; //25 +26
-               uint8_t h1_;  //halb und halb 26+27
-               int8_t h3;
-               int8_t h4;
-               int8_t h5;
-               uint8_t h6;
-               int8_t h7;//32
-               uint16_t t1;
-               int16_t gh2;
-               int8_t gh1;
-               int8_t gh3; //38
-               int8_t fr6;// 39;
-               int8_t fr7; //40;
-               uint16_t h2; //Berechnung
-               uint16_t h1; //Berechnung       
-               int32_t t_fine; //Berechnung bei Temperaturmessung      
+// Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de\r
+// All rights reserved.\r
+//\r
+// Redistribution and use in source and binary forms, with or without\r
+// modification, are permitted provided that the following conditions are\r
+// met:\r
+//\r
+//  * Redistributions of source code must retain the above copyright\r
+//    notice, this list of conditions and the following disclaimer.\r
+//  * Redistributions in binary form must reproduce the above copyright\r
+//    notice, this list of conditions and the following disclaimer in the\r
+//    documentation and/or other materials provided with the\r
+//    distribution.\r
+//  * All advertising materials mentioning features or use of this\r
+//    software must display the following acknowledgement: This product\r
+//    includes software developed by tm3d.de and its contributors.\r
+//  * Neither the name of tm3d.de nor the names of its contributors may\r
+//    be used to endorse or promote products derived from this software\r
+//    without specific prior written permission.\r
+//\r
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+#ifdef  __4MHZ__\r
+#define F_CPU 4000000UL\r
+#else\r
+#define F_CPU 8000000UL\r
+#endif\r
+#include <avr/io.h>\r
+\r
+\r
+/** Array Index to Field data mapping for Calibration Data*/\r
+#define BME680_T2_LSB_REG      (1)\r
+#define BME680_T2_MSB_REG      (2)\r
+#define BME680_T3_REG          (3)\r
+#define BME680_P1_LSB_REG      (5)\r
+#define BME680_P1_MSB_REG      (6)\r
+#define BME680_P2_LSB_REG      (7)\r
+#define BME680_P2_MSB_REG      (8)\r
+#define BME680_P3_REG          (9)\r
+#define BME680_P4_LSB_REG      (11)\r
+#define BME680_P4_MSB_REG      (12)\r
+#define BME680_P5_LSB_REG      (13)\r
+#define BME680_P5_MSB_REG      (14)\r
+#define BME680_P7_REG          (15)\r
+#define BME680_P6_REG          (16)\r
+#define BME680_P8_LSB_REG      (19)\r
+#define BME680_P8_MSB_REG      (20)\r
+#define BME680_P9_LSB_REG      (21)\r
+#define BME680_P9_MSB_REG      (22)\r
+#define BME680_P10_REG         (23)\r
+#define BME680_H2_MSB_REG      (25)\r
+#define BME680_H2_LSB_REG      (26)\r
+#define BME680_H1_LSB_REG      (26)\r
+#define BME680_H1_MSB_REG      (27)\r
+#define BME680_H3_REG          (28)\r
+#define BME680_H4_REG          (29)\r
+#define BME680_H5_REG          (30)\r
+#define BME680_H6_REG          (31)\r
+#define BME680_H7_REG          (32)\r
+#define BME680_T1_LSB_REG      (33)\r
+#define BME680_T1_MSB_REG      (34)\r
+#define BME680_GH2_LSB_REG     (35)\r
+#define BME680_GH2_MSB_REG     (36)\r
+#define BME680_GH1_REG         (37)\r
+#define BME680_GH3_REG (38)\r
+\r
+\r
+#include <util/delay.h>\r
+#include <avr/pgmspace.h>\r
+\r
+#include "USI_TWI_Master.h"\r
+#include "BME680.h"\r
+\r
+#define WC 0b11101100\r
+#define RC 0b11101101\r
+typedef union {\r
+       volatile uint8_t d[41];\r
+       struct {\r
+               uint8_t fr1;\r
+               int16_t t2;\r
+               int8_t t3;\r
+               uint8_t fr2; //4\r
+               uint16_t p1;\r
+               int16_t p2; //7\r
+               int8_t p3;\r
+               uint8_t fr3; //10\r
+               int16_t p4;\r
+               int16_t p5;\r
+               int8_t p7;\r
+               int8_t p6;\r
+               uint8_t fr4[2]; //17-18\r
+               int16_t p8;\r
+               int16_t p9;\r
+               uint8_t p10;\r
+               uint8_t fr5; //24\r
+               uint16_t h2_; //25 +26\r
+               uint8_t h1_;  //halb und halb 26+27\r
+               int8_t h3;\r
+               int8_t h4;\r
+               int8_t h5;\r
+               uint8_t h6;\r
+               int8_t h7;//32\r
+               uint16_t t1;\r
+               int16_t gh2;\r
+               int8_t gh1;\r
+               int8_t gh3; //38\r
+               int8_t fr6;// 39;\r
+               int8_t fr7; //40;\r
+               uint16_t h2; //Berechnung\r
+               uint16_t h1; //Berechnung       \r
+               int32_t t_fine; //Berechnung bei Temperaturmessung      \r
                uint8_t  res_heat_range;/**<resistance calculation*/
                int8_t  res_heat_val; /**<correction factor*/
-               int8_t  range_switching_error;/**<range switching error*/
-               int8_t ltemp; //letzte Temperatur
-       };
-} calib_t;
-
-volatile calib_t calib;
-
-
-#define BME680_CALIB_I2C_ADDR_1                                (0x89)
-#define BME680_CALIB_I2C_ADDR_2                                (0xE1)
-#define BME680_PAGE0_I2C_ID_REG                                (0xD0)
-#define BME680_CALIB_DATA_LENGTH_GAS                   (25)
-#define BME680_CALIB_DATA_LENGTH                       (16)
-
-
-#define BME680_MAX_HUMIDITY_VALUE              (102400)
-#define BME680_MIN_HUMIDITY_VALUE              (0)
-
-
-//ME680_CALIB_I2C_ADDR_1,
-//                                             a_data_u8,
-//                                             BME680_CALIB_DATA_LENGTH_GAS);
-                               /* read the humidity and gas
-                               calibration data*/
-/*                             com_status = (enum bme680_return_type)
-                                            bme680->bme680_bus_read(
-                                            bme680->dev_addr,
-                                            BME680_CALIB_I2C_ADDR_2,
-                                           (a_data_u8 +
-                                           BME680_CALIB_DATA_LENGTH_GAS),
-                                           BME680_CALIB_DATA_LENGTH);
-
-
-
-                                               */
-
-
-void setup_read(uint8_t addr) {
-       I2c_StartCondition();
-       I2c_WriteByte(WC);
-       I2c_WriteByte(addr);  //Ctrl hum
-       I2c_StartCondition();
-       I2c_WriteByte (RC);
-       
-}
-
-void setup_write(uint8_t addr) {
-       I2c_StartCondition();
-       I2c_WriteByte(WC);
-       I2c_WriteByte(addr);  //Ctrl hum
-}
-
-
-uint8_t readone(uint8_t addr) {
-       setup_read(addr);
-       uint8_t b=I2c_ReadByte(NO_ACK);
-       I2c_StopCondition();
-       return b;
-}
-
-void writeone(uint8_t addr,uint8_t b) {
-       setup_write(addr);
-       I2c_WriteByte(b);  //Ctrl hum
-       I2c_StopCondition();
-}
-
-int8_t initBME680() {
-       uint8_t b1=readone(0xD0);
-       setup_read(BME680_CALIB_I2C_ADDR_1);
-       for(uint8_t i=0;i<BME680_CALIB_DATA_LENGTH_GAS-1;i++) {
-               calib.d[i]=I2c_ReadByte(ACK);
-       }
-       calib.d[BME680_CALIB_DATA_LENGTH_GAS-1]=I2c_ReadByte(NO_ACK);
-       I2c_StopCondition();
-
-       setup_read(BME680_CALIB_I2C_ADDR_2);
-       for(uint8_t i=BME680_CALIB_DATA_LENGTH_GAS;i<BME680_CALIB_DATA_LENGTH_GAS+BME680_CALIB_DATA_LENGTH-1;i++) {
-               calib.d[i]=I2c_ReadByte(ACK);
-       }
-       calib.d[BME680_CALIB_DATA_LENGTH_GAS+BME680_CALIB_DATA_LENGTH-1]=I2c_ReadByte(NO_ACK);
-       I2c_StopCondition();
-       calib.res_heat_range=(readone(0x02)&0x30)>>4;
-       calib.res_heat_val=readone(0);
-       calib.range_switching_error=(readone(0x04)& 0xF0)>>4;
-
-
-    calib.h1 = (uint16_t)(((((uint16_t)calib.d[ BME680_H1_MSB_REG]))
-               << 4) | (calib.d[ BME680_H1_LSB_REG] &0x0F));
-       calib.h2 = (uint16_t)(((((uint16_t)calib.d[ BME680_H2_MSB_REG]))
-               << 4) | ((calib.d[ BME680_H2_LSB_REG]) >> 4));
-
-       
-       /*
-       
-       I2c_StartCondition();
-       I2c_WriteByte(WC);
-       I2c_WriteByte(0x72);  //Ctrl hum
-       I2c_WriteByte(0x01); //1x oversembling hum
-       I2c_WriteByte(0x74);  //Ctrl hum
-       I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont
-       I2c_StopCondition();    
-
-       */
-       calib.ltemp=25;
-
-       return b1==0x61;
-
-}
-
-
+               int8_t  range_switching_error;/**<range switching error*/\r
+               int8_t ltemp; //letzte Temperatur\r
+       };\r
+} calib_t;\r
+\r
+volatile calib_t calib;\r
+\r
+\r
+#define BME680_CALIB_I2C_ADDR_1                                (0x89)\r
+#define BME680_CALIB_I2C_ADDR_2                                (0xE1)\r
+#define BME680_PAGE0_I2C_ID_REG                                (0xD0)\r
+#define BME680_CALIB_DATA_LENGTH_GAS                   (25)\r
+#define BME680_CALIB_DATA_LENGTH                       (16)\r
+\r
+\r
+#define BME680_MAX_HUMIDITY_VALUE              (102400)\r
+#define BME680_MIN_HUMIDITY_VALUE              (0)\r
+\r
+\r
+//ME680_CALIB_I2C_ADDR_1,\r
+//                                             a_data_u8,\r
+//                                             BME680_CALIB_DATA_LENGTH_GAS);\r
+                               /* read the humidity and gas\r
+                               calibration data*/\r
+/*                             com_status = (enum bme680_return_type)\r
+                                            bme680->bme680_bus_read(\r
+                                            bme680->dev_addr,\r
+                                            BME680_CALIB_I2C_ADDR_2,\r
+                                           (a_data_u8 +\r
+                                           BME680_CALIB_DATA_LENGTH_GAS),\r
+                                           BME680_CALIB_DATA_LENGTH);\r
+\r
+\r
+\r
+                                               */\r
+\r
+\r
+void setup_read(uint8_t addr) {\r
+       I2c_StartCondition();\r
+       I2c_WriteByte(WC);\r
+       I2c_WriteByte(addr);  //Ctrl hum\r
+       I2c_StartCondition();\r
+       I2c_WriteByte (RC);\r
+       \r
+}\r
+\r
+void setup_write(uint8_t addr) {\r
+       I2c_StartCondition();\r
+       I2c_WriteByte(WC);\r
+       I2c_WriteByte(addr);  //Ctrl hum\r
+}\r
+\r
+\r
+uint8_t readone(uint8_t addr) {\r
+       setup_read(addr);\r
+       uint8_t b=I2c_ReadByte(NO_ACK);\r
+       I2c_StopCondition();\r
+       return b;\r
+}\r
+\r
+void writeone(uint8_t addr,uint8_t b) {\r
+       setup_write(addr);\r
+       I2c_WriteByte(b);  //Ctrl hum\r
+       I2c_StopCondition();\r
+}\r
+\r
+int8_t initBME680() {\r
+       uint8_t b1=readone(0xD0);\r
+       setup_read(BME680_CALIB_I2C_ADDR_1);\r
+       for(uint8_t i=0;i<BME680_CALIB_DATA_LENGTH_GAS-1;i++) {\r
+               calib.d[i]=I2c_ReadByte(ACK);\r
+       }\r
+       calib.d[BME680_CALIB_DATA_LENGTH_GAS-1]=I2c_ReadByte(NO_ACK);\r
+       I2c_StopCondition();\r
+\r
+       setup_read(BME680_CALIB_I2C_ADDR_2);\r
+       for(uint8_t i=BME680_CALIB_DATA_LENGTH_GAS;i<BME680_CALIB_DATA_LENGTH_GAS+BME680_CALIB_DATA_LENGTH-1;i++) {\r
+               calib.d[i]=I2c_ReadByte(ACK);\r
+       }\r
+       calib.d[BME680_CALIB_DATA_LENGTH_GAS+BME680_CALIB_DATA_LENGTH-1]=I2c_ReadByte(NO_ACK);\r
+       I2c_StopCondition();\r
+       calib.res_heat_range=(readone(0x02)&0x30)>>4;\r
+       calib.res_heat_val=readone(0);\r
+       calib.range_switching_error=(readone(0x04)& 0xF0)>>4;\r
+\r
+\r
+    calib.h1 = (uint16_t)(((((uint16_t)calib.d[ BME680_H1_MSB_REG]))\r
+               << 4) | (calib.d[ BME680_H1_LSB_REG] &0x0F));\r
+       calib.h2 = (uint16_t)(((((uint16_t)calib.d[ BME680_H2_MSB_REG]))\r
+               << 4) | ((calib.d[ BME680_H2_LSB_REG]) >> 4));\r
+\r
+       \r
+       /*\r
+       \r
+       I2c_StartCondition();\r
+       I2c_WriteByte(WC);\r
+       I2c_WriteByte(0x72);  //Ctrl hum\r
+       I2c_WriteByte(0x01); //1x oversembling hum\r
+       I2c_WriteByte(0x74);  //Ctrl hum\r
+       I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont\r
+       I2c_StopCondition();    \r
+\r
+       */\r
+       calib.ltemp=25;\r
+\r
+       return b1==0x61;\r
+\r
+}\r
+\r
+\r
 const float lookup_k1_range[16] PROGMEM = {
        1, 1, 1, 1, 1,0.99, 1, 0.992,
 1, 1, 0.998, 0.995, 1, 0.99, 1, 1};
 const float lookup_k2_range[16] PROGMEM = {
        8e6, 4e6, 2e6, 1e6,499500.4995, 248262.1648, 125000, 63004.03226,
-31281.28128, 15625, 7812.5, 3906.25,1953.125,976.5625, 488.28125, 244.140625};
-
+31281.28128, 15625, 7812.5, 3906.25,1953.125,976.5625, 488.28125, 244.140625};\r
+\r
 double bme680_compensate_gas_double(uint16_t gas_adc_u16, uint8_t gas_range_u8)
 {
        double gas_res_d = 0;
@@ -256,12 +256,12 @@ double bme680_compensate_gas_double(uint16_t gas_adc_u16, uint8_t gas_range_u8)
        gas_res_d = var1*a2/(gas_adc_u16-512.0+var1);
        return gas_res_d;
 }
-
-
-void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){
-
-
-       
+\r
+\r
+void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){\r
+\r
+\r
+       \r
        int32_t var1 ;
        int32_t var2 ;
        int32_t var3 ;
@@ -284,8 +284,8 @@ void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){
 
                res_heat_x100 = (int32_t)(((var4 / var5) - 250) * 34);
                res_heat = (uint8_t) ((res_heat_x100 + 50) / 100);
-       }
-       uint16_t duration=100;
+       }\r
+       uint16_t duration=100;\r
        uint8_t factor = 0;
 
        while ((duration) > 0x3F) {
@@ -293,144 +293,144 @@ void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){
                factor += 1;
        }
        (duration) = (duration) + (factor * 64);
-
-       //I2c_WriteByte(0x74);  //Ctrl hum
-       //I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont
-       // [71] <- 10;  [72] <- 04;  [73] <- 0C;  [74] <- 91;  [75] <- 00;
-       // [70] <- 00     [71] <- 10;  [72] <- 04;  [73] <- 0C;  [74] <- 91;  [75] <- 00;
-       setup_write(0x70);
-       I2c_WriteByte(0x00);
-       I2c_WriteByte(0x71);
-       I2c_WriteByte(0x10);
-       I2c_WriteByte(0x72);
-       I2c_WriteByte(0x04);
-       I2c_WriteByte(0x73);
-       I2c_WriteByte(0x0C);
-       I2c_WriteByte(0x74);
-       I2c_WriteByte(0x90);
-       I2c_WriteByte(0x75);
-       I2c_WriteByte(0x00);
-
-       I2c_WriteByte(0x5A);
-       I2c_WriteByte(res_heat);
-       I2c_WriteByte(0x64);
-       I2c_WriteByte(duration);
-       I2c_StopCondition();
-
-
-       writeone(0x74,0x91);
-       _delay_ms(1000);
-       
-       uint8_t bx=0x91;
-       while ((bx&0x01)==0x01) {
-               bx=readone(0x74);
-               _delay_ms(5);
-       }
-
-       //volatile uint8_t rs=readone(0x2B);
-       uint32_t Th,Hh,Ph;
-       setup_read(0x1F);
-       Ph=I2c_ReadByte(ACK);Ph=Ph<<8;
-       Ph|=I2c_ReadByte(ACK);Ph=Ph<<4;
-       Ph|=I2c_ReadByte(ACK)>>4;
-       Th=I2c_ReadByte(ACK);Th=Th<<8;
-       Th|=I2c_ReadByte(ACK);Th=Th<<4;
-       Th|=I2c_ReadByte(ACK)>>4;       
-       Hh=I2c_ReadByte(ACK);Hh=Hh<<8;
-       Hh|=I2c_ReadByte(NO_ACK);
-       I2c_StopCondition();
-       setup_read(0x2A);
-       volatile uint8_t g1=I2c_ReadByte(ACK);
-       volatile uint8_t g2=I2c_ReadByte(NO_ACK);
-       I2c_StopCondition();
-       *G=(((uint16_t)g1)<<2)|(g2>>6);
-       *P=*G;
-       *G= bme680_compensate_gas_double(*G,g2&0xF)/10.0;
-
-       int32_t temp_comp = 0;
-
-       var1 = ((int32_t)Th >> 3) -
-       ((int32_t)calib.t1 << 1);
-       var2 = (var1 * (int32_t)calib.t2) >> 11;
-       var3 = ((((var1 >> 1) * (var1 >> 1)) >> 12) *
-       ((int32_t)calib.t3 << 4)) >> 14;
-       calib.t_fine = var2 + var3;
-       temp_comp = ((calib.t_fine * 5) + 128) >> 8;
-
-       int32_t temp_scaled = 0;
-       int32_t var6    = 0;
-       int32_t humidity_comp = 0;
-
-       temp_scaled = (((int32_t)calib.t_fine * 5) + 128) >> 8;
-       var1 = (int32_t)Hh -
-               ((int32_t)((int32_t)calib.h1 << 4)) -
-               (((temp_scaled * (int32_t)calib.h3) /
-               ((int32_t)100)) >> 1);
-
-       var2 = ((int32_t)calib.h2 *
-               (((temp_scaled * (int32_t)calib.h4) /
-               ((int32_t)100)) + (((temp_scaled *
-               ((temp_scaled * (int32_t)calib.h5) /
-               ((int32_t)100))) >> 6) / ((int32_t)100)) + (int32_t)(1 << 14))) >> 10;
-
-       var3 = var1 * var2;
-
-       var4 = ((((int32_t)calib.h6) << 7) +
-               ((temp_scaled * (int32_t)calib.h7) /
-               ((int32_t)100))) >> 4;
-
-       var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;
-       var6 = (var4 * var5) >> 1;
-
-       humidity_comp = (var3 + var6) >> 12;
-       if (humidity_comp > BME680_MAX_HUMIDITY_VALUE)
-               humidity_comp = BME680_MAX_HUMIDITY_VALUE;
-               else if (humidity_comp < BME680_MIN_HUMIDITY_VALUE)
-               humidity_comp = BME680_MIN_HUMIDITY_VALUE;
-
-    int32_t pressure_comp = 0;//int -> 5684
-
-    var1 = (((int32_t)calib.t_fine) >> 1) - (int32_t)64000;
-    var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) *
-    (int32_t)calib.p6) >> 2;
-    var2 = var2 + ((var1 * (int32_t)calib.p5) << 1);
-    var2 = (var2 >> 2) + ((int32_t)calib.p4 << 16);
-    var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *
-    ((int32_t)calib.p3 << 5)) >> 3) +
-    (((int32_t)calib.p2 * var1) >> 1);
-    var1 = var1 >> 18;
-    var1 = (((int32_t)32768 + var1) * (int32_t)calib.p1) >> 15;
-    pressure_comp = (int32_t)1048576 - Ph;
-    pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125));
-    var4 = ((int32_t)1 << 31);
-    if (pressure_comp >= var4)
-    pressure_comp = ((pressure_comp / (uint32_t)var1) << 1);
-    else
-    pressure_comp = ((pressure_comp << 1) / (uint32_t)var1);
-    var1 = ((int32_t)calib.p9 * (int32_t)(((pressure_comp >> 3) *
-    (pressure_comp >> 3)) >> 13)) >> 12;
-    var2 = ((int32_t)(pressure_comp >> 2) *
-    (int32_t)calib.p8) >> 13;
-    var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) *
-    (int32_t)(pressure_comp >> 8) *
-    (int32_t)calib.p10) >> 17;
-
-    pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 +
-    ((int32_t)calib.p7 << 7)) >> 4);
-
-
-
-
-       
-       *T=(int16_t)temp_comp;
-       calib.ltemp=temp_comp/100;
-       //*P=pressure_comp;
-       *H=(uint16_t)(humidity_comp/10);
-       //*P=rs;
-       *T=g1;
-       *H=g2;
-       //*P=
-
-
-}
-
+\r
+       //I2c_WriteByte(0x74);  //Ctrl hum\r
+       //I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont\r
+       // [71] <- 10;  [72] <- 04;  [73] <- 0C;  [74] <- 91;  [75] <- 00;\r
+       // [70] <- 00     [71] <- 10;  [72] <- 04;  [73] <- 0C;  [74] <- 91;  [75] <- 00;\r
+       setup_write(0x70);\r
+       I2c_WriteByte(0x00);\r
+       I2c_WriteByte(0x71);\r
+       I2c_WriteByte(0x10);\r
+       I2c_WriteByte(0x72);\r
+       I2c_WriteByte(0x04);\r
+       I2c_WriteByte(0x73);\r
+       I2c_WriteByte(0x0C);\r
+       I2c_WriteByte(0x74);\r
+       I2c_WriteByte(0x90);\r
+       I2c_WriteByte(0x75);\r
+       I2c_WriteByte(0x00);\r
+\r
+       I2c_WriteByte(0x5A);\r
+       I2c_WriteByte(res_heat);\r
+       I2c_WriteByte(0x64);\r
+       I2c_WriteByte(duration);\r
+       I2c_StopCondition();\r
+\r
+\r
+       writeone(0x74,0x91);\r
+       _delay_ms(1000);\r
+       \r
+       uint8_t bx=0x91;\r
+       while ((bx&0x01)==0x01) {\r
+               bx=readone(0x74);\r
+               _delay_ms(5);\r
+       }\r
+\r
+       //volatile uint8_t rs=readone(0x2B);\r
+       uint32_t Th,Hh,Ph;\r
+       setup_read(0x1F);\r
+       Ph=I2c_ReadByte(ACK);Ph=Ph<<8;\r
+       Ph|=I2c_ReadByte(ACK);Ph=Ph<<4;\r
+       Ph|=I2c_ReadByte(ACK)>>4;\r
+       Th=I2c_ReadByte(ACK);Th=Th<<8;\r
+       Th|=I2c_ReadByte(ACK);Th=Th<<4;\r
+       Th|=I2c_ReadByte(ACK)>>4;       \r
+       Hh=I2c_ReadByte(ACK);Hh=Hh<<8;\r
+       Hh|=I2c_ReadByte(NO_ACK);\r
+       I2c_StopCondition();\r
+       setup_read(0x2A);\r
+       volatile uint8_t g1=I2c_ReadByte(ACK);\r
+       volatile uint8_t g2=I2c_ReadByte(NO_ACK);\r
+       I2c_StopCondition();\r
+       *G=(((uint16_t)g1)<<2)|(g2>>6);\r
+       *P=*G;\r
+       *G= bme680_compensate_gas_double(*G,g2&0xF)/10.0;\r
+\r
+       int32_t temp_comp = 0;\r
+\r
+       var1 = ((int32_t)Th >> 3) -\r
+       ((int32_t)calib.t1 << 1);\r
+       var2 = (var1 * (int32_t)calib.t2) >> 11;\r
+       var3 = ((((var1 >> 1) * (var1 >> 1)) >> 12) *\r
+       ((int32_t)calib.t3 << 4)) >> 14;\r
+       calib.t_fine = var2 + var3;\r
+       temp_comp = ((calib.t_fine * 5) + 128) >> 8;\r
+\r
+       int32_t temp_scaled = 0;\r
+       int32_t var6    = 0;\r
+       int32_t humidity_comp = 0;\r
+\r
+       temp_scaled = (((int32_t)calib.t_fine * 5) + 128) >> 8;\r
+       var1 = (int32_t)Hh -\r
+               ((int32_t)((int32_t)calib.h1 << 4)) -\r
+               (((temp_scaled * (int32_t)calib.h3) /\r
+               ((int32_t)100)) >> 1);\r
+\r
+       var2 = ((int32_t)calib.h2 *\r
+               (((temp_scaled * (int32_t)calib.h4) /\r
+               ((int32_t)100)) + (((temp_scaled *\r
+               ((temp_scaled * (int32_t)calib.h5) /\r
+               ((int32_t)100))) >> 6) / ((int32_t)100)) + (int32_t)(1 << 14))) >> 10;\r
+\r
+       var3 = var1 * var2;\r
+\r
+       var4 = ((((int32_t)calib.h6) << 7) +\r
+               ((temp_scaled * (int32_t)calib.h7) /\r
+               ((int32_t)100))) >> 4;\r
+\r
+       var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;\r
+       var6 = (var4 * var5) >> 1;\r
+\r
+       humidity_comp = (var3 + var6) >> 12;\r
+       if (humidity_comp > BME680_MAX_HUMIDITY_VALUE)\r
+               humidity_comp = BME680_MAX_HUMIDITY_VALUE;\r
+               else if (humidity_comp < BME680_MIN_HUMIDITY_VALUE)\r
+               humidity_comp = BME680_MIN_HUMIDITY_VALUE;\r
+\r
+    int32_t pressure_comp = 0;//int -> 5684\r
+\r
+    var1 = (((int32_t)calib.t_fine) >> 1) - (int32_t)64000;\r
+    var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) *\r
+    (int32_t)calib.p6) >> 2;\r
+    var2 = var2 + ((var1 * (int32_t)calib.p5) << 1);\r
+    var2 = (var2 >> 2) + ((int32_t)calib.p4 << 16);\r
+    var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *\r
+    ((int32_t)calib.p3 << 5)) >> 3) +\r
+    (((int32_t)calib.p2 * var1) >> 1);\r
+    var1 = var1 >> 18;\r
+    var1 = (((int32_t)32768 + var1) * (int32_t)calib.p1) >> 15;\r
+    pressure_comp = (int32_t)1048576 - Ph;\r
+    pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125));\r
+    var4 = ((int32_t)1 << 31);\r
+    if (pressure_comp >= var4)\r
+    pressure_comp = ((pressure_comp / (uint32_t)var1) << 1);\r
+    else\r
+    pressure_comp = ((pressure_comp << 1) / (uint32_t)var1);\r
+    var1 = ((int32_t)calib.p9 * (int32_t)(((pressure_comp >> 3) *\r
+    (pressure_comp >> 3)) >> 13)) >> 12;\r
+    var2 = ((int32_t)(pressure_comp >> 2) *\r
+    (int32_t)calib.p8) >> 13;\r
+    var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) *\r
+    (int32_t)(pressure_comp >> 8) *\r
+    (int32_t)calib.p10) >> 17;\r
+\r
+    pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 +\r
+    ((int32_t)calib.p7 << 7)) >> 4);\r
+\r
+\r
+\r
+\r
+       \r
+       *T=(int16_t)temp_comp;\r
+       calib.ltemp=temp_comp/100;\r
+       //*P=pressure_comp;\r
+       *H=(uint16_t)(humidity_comp/10);\r
+       //*P=rs;\r
+       *T=g1;\r
+       *H=g2;\r
+       //*P=\r
+\r
+\r
+}\r
+\r