- OWINIT save Register used in C
[owSlave2.git] / DS18B20_BMP280_DS2438_SHT / DS18B20_DS2438.c
index 5b98b1c..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();
@@ -59,13 +59,14 @@ extern uint8_t mode;
 extern uint8_t gcontrol;
 extern uint8_t reset_indicator;
 extern uint8_t alarmflag;
-volatile uint8_t wdcounter;
+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++;
@@ -133,6 +134,26 @@ volatile int16_t am2302_temp;
 volatile uint16_t am2302_hum;
 
 
+int testSW(void) {
+       uint8_t r;
+       DDRB&=~(1<<PORTB0);  //Eingang
+       __asm__ __volatile__ ("nop");
+       PORTB|=(1<<PORTB0); //Pullup
+       __asm__ __volatile__ ("nop");
+       __asm__ __volatile__ ("nop");
+       __asm__ __volatile__ ("nop");
+       __asm__ __volatile__ ("nop");
+       __asm__ __volatile__ ("nop");
+       r=PINB&(1<<PORTB0);
+       __asm__ __volatile__ ("nop");
+       PORTB&=~(1<<PORTB0);
+       __asm__ __volatile__ ("nop");
+       DDRB|=(1<<PORTB0);  //Eingang
+       return (r==0);
+       
+       
+}
+
 uint8_t userRegister[1];
 int16_t sRH,sT;
 volatile double temperatureC,humidityRH;
@@ -142,6 +163,10 @@ volatile double l;
 
 int main(void){
     PRR|=(1<<PRADC);  // adc for save Power
+       PORTA=0xFF;
+       PORTB=0xFF-(1<<PORTB0); //Schalter kann gegen Masse sein und zieht dann immer Strom
+       DDRB|=(1<<PORTB0); //Als Ausgang und 0
+       
        pack1.temp=0x0550;
        pack1.config=0x7F;
        pack1.TH=75;
@@ -153,41 +178,69 @@ int main(void){
        PORTB=0xFF;
        OWINIT();
 
+       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)
        WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt
-       (1<<WDP2) | (1<<WDP1);   // Set Timeout to ~1 seconds
+       (1<<WDP3) | (1<<WDP0);   // Set Timeout to ~8 seconds
+       if (testSW()) {
+               config_info2[5]=8;
+               }else{
+               config_info2[5]=7;
+       }
+       
 
        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 (wdcounter>0) {
+               if (gcontrol) {
+                       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;
                }
        
@@ -206,9 +259,10 @@ int main(void){
                                //pack.temp++;
                                alarmflag=af;
                                sei();
+                               gcontrol=0;
                                EXTERN_SLEEP(); 
                }
-               if ((gcontrol==2)||(gcontrol==3)) {
+               if (gcontrol) {
                        gcontrol=0;
                        
                }