- OWINIT save Register used in C
[owSlave2.git] / DS18B20_BMP280_DS2438_SHT / DS18B20_DS2438.c
index b4b62d0..d8b04bd 100644 (file)
@@ -40,7 +40,7 @@
 #include "../common/I2C/USI_TWI_Master.h"
 #include "../common/I2C/SHT2x.h"
 #include "../common/I2C/BMP280.h"
-
+#include "../common/calibr.h"
 
 extern void OWINIT();
 extern void EXTERN_SLEEP();
@@ -64,8 +64,9 @@ volatile uint8_t wdcounter=3;
 
 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
 ISR(WATCHDOG_vect) {
-#else
-ISR(WDT_vect) {
+       #else
+       ISR(WDT_vect) {
+       
 #endif 
        //sleep_disable();          // Disable Sleep on Wakeup
        wdcounter++;
@@ -180,7 +181,7 @@ int main(void){
        ACSR|=(1<<ACD);  //Disable Comparator
        ADCSRB|=(1<<ACME); //Disable Analog multiplexer
        MCUCR &=~(1<<PUD); //All Pins Pullup...
-       MCUCR |=(1<<BODS);
+       //MCUCR &=~(1<<BODS);
 
        
        WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)
@@ -195,36 +196,51 @@ int main(void){
 
        MCUSR=0;
        USI_TWI_Master_Initialise();
-       bmp280Init();
+       
        SHT2x_SoftReset();
        SHT2x_ReadUserRegister(userRegister);
        //(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;
        SHT2x_WriteUserRegister(userRegister); //write changed user reg
        // --- measure humidity with "Hold Master Mode (HM)" ---
-       SHT2x_MeasurePoll(HUMIDITY, &sRH);
+       //SHT2x_MeasurePoll(HUMIDITY, &sRH);
        // --- measure temperature with "Polling Mode" (no hold master) ---
-       SHT2x_MeasurePoll(TEMP, &sT);
+       //SHT2x_MeasurePoll(TEMP, &sT);
        //-- calculate humidity and temperature --
-       temperatureC = SHT2x_CalcTemperatureC(sT);
-       humidityRH = SHT2x_CalcRH(sRH);
-       
+       //temperatureC = SHT2x_CalcTemperatureC(sT);
+       //humidityRH = SHT2x_CalcRH(sRH);
+       bmp280Init();
        gcontrol=1;
        sei();
     while(1)   {
-               if ((gcontrol==2)||(gcontrol==3)) {
+               if (gcontrol) {
                        wdcounter=3;
-                       gcontrol=0;
-               }               
-               if (wdcounter>3) {
+                       
+               }
+
+               if (wdcounter>2) {
                        SHT2x_MeasurePoll(HUMIDITY, &sRH);
                        // --- measure temperature with "Polling Mode" (no hold master) ---
                        SHT2x_MeasurePoll(TEMP, &sT);
                        //-- calculate humidity and temperature --
-                       temperatureC = SHT2x_CalcTemperatureC(sT)*10.0;
-                       humidityRH = SHT2x_CalcRH(sRH)*10.0;
-                       double hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
-                       am2302_hum=0.318*hhum +76.0;
-                       am2302_temp=temperatureC*25.6;
+                       temperatureC = SHT2x_CalcTemperatureC(sT);
+                       humidityRH = SHT2x_CalcRH(sRH);
+                       humidityRH=calibr_hum(temperatureC,-0.45,humidityRH)*10.0;
+                       temperatureC=temperatureC*10.0-4.5;
+                        if (testSW()) {
+                                am2302_hum= humidityRH*10.0;
+                                am2302_temp=temperatureC*25.6;
+                                am2302_temp=am2302_temp-20;
+                                config_info2[5]=8;
+                                
+                                }else{
+                                
+                                double hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
+                                //am2302_hum=0.318*hhum +76.0;
+                                am2302_hum=0.31*hhum +80;
+                                am2302_temp=temperatureC*25.6;
+                                am2302_temp=am2302_temp-20;
+                                config_info2[5]=7;
+                        }
                        wdcounter=0;
                }
        
@@ -243,9 +259,13 @@ int main(void){
                                //pack.temp++;
                                alarmflag=af;
                                sei();
+                               gcontrol=0;
                                EXTERN_SLEEP(); 
                }
-               
+               if (gcontrol) {
+                       gcontrol=0;
+                       
+               }
 
                
 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)