- OWINIT save Register used in C
[owSlave2.git] / DS18B20_MAX44009_DS2438_SHT / DS18B20_DS2438.c
index 3cdc7ec..affc509 100644 (file)
@@ -60,7 +60,7 @@ 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=5;
 
 
 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
@@ -82,6 +82,26 @@ ISR(WDT_vect) {
 
 }
 
+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);
+       
+       
+}
+
 
 typedef union {
        volatile uint8_t bytes[8];
@@ -159,8 +179,12 @@ int main(void){
 
        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();
        if (checkMAX44009(0)) max_adr=0; else max_adr=1 ;
@@ -173,29 +197,48 @@ int main(void){
        // --- measure temperature with "Polling Mode" (no hold master) ---
        SHT2x_MeasurePoll(TEMP, &sT);
        //-- calculate humidity and temperature --
-       temperatureC = SHT2x_CalcTemperatureC(sT);
-       humidityRH = SHT2x_CalcRH(sRH);
+       //temperatureC = SHT2x_CalcTemperatureC(sT);
+       //humidityRH = SHT2x_CalcRH(sRH);
        
        gcontrol=1;
        sei();
        //DDRB|=(1<<PINB1);
     while(1)   {
-               if (wdcounter>0) {
+               if ((gcontrol==2)||(gcontrol==3)) {
+                       wdcounter=3;
+                       gcontrol=0;
+                       
+               }
+               
+               if (wdcounter>2) {
                        //PORTB|=(1<<PINB1); //Dauer 440ms
                        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;
-                       //PORTB&=~(1<<PINB1);
+                       temperatureC = SHT2x_CalcTemperatureC(sT)*10.0-4;
+                       humidityRH = SHT2x_CalcRH(sRH)*10.0+5;
+                       if (humidityRH>1000) humidityRH=1000;
+                       if (testSW()) {
+                               am2302_hum= humidityRH*10.0;
+                               am2302_temp=temperatureC*25.6;
+                               //
+                               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-45;
+                               config_info2[5]=7;
+                       }
                        wdcounter=0;
                }
        
                if (gcontrol==1) {
+                       gcontrol=0;
                        l=MAX44009getlux(max_adr);              
                        if (l<0.030) l=0.030; //Darf nicht 0 sein. minimum -35°C Sensor minimum 0.045
                        //double l=1000;
@@ -212,10 +255,7 @@ int main(void){
                        sei();                  
                        EXTERN_SLEEP();         
                }
-               if ((gcontrol==2)||(gcontrol==3)) {
-                       gcontrol=0;
-                       
-               }
+               
 
                
 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)