- OWINIT save Register used in C
[owSlave2.git] / DS18B20_MAX44009_DS2438_SHT / DS18B20_DS2438.c
index d737c59..affc509 100644 (file)
@@ -44,7 +44,7 @@
 
 
 extern void OWINIT();
-
+extern void EXTERN_SLEEP();
 
 
 uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/
@@ -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];
@@ -139,6 +159,7 @@ int16_t sRH,sT;
 volatile double temperatureC,humidityRH;
 volatile double l;
 
+uint8_t max_adr=0;
 
 int main(void){
     PRR|=(1<<PRADC);  // adc for save Power
@@ -158,10 +179,15 @@ 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 ;
        SHT2x_SoftReset();
        SHT2x_ReadUserRegister(userRegister);
        //(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;
@@ -171,27 +197,49 @@ 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;
+                       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) {
-                       l=MAX44009getlux(0);            
+                       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;
                        l=log(l)*10*16;
@@ -205,12 +253,9 @@ int main(void){
                        //pack.temp++;
                        alarmflag=af;
                        sei();                  
-                       gcontrol=0;
-               }
-               if ((gcontrol==2)||(gcontrol==3)) {
-                       gcontrol=0;
-                       
+                       EXTERN_SLEEP();         
                }
+               
 
                
 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
@@ -227,7 +272,6 @@ int main(void){
                        MCUCR|=(1<<SE);
                        MCUCR&=~(1<<SM1);
                }
-               //MCUCR&=~(1<<ISC01);
                asm("SLEEP");
    }