Many changes from 2018
[owSlave2.git] / DS18B20_MAX44009_DS2438_SHT / DS18B20_DS2438.c
index 4e8d28b..efd0454 100644 (file)
 #include <avr/pgmspace.h>\r
 #include "../common/I2C/USI_TWI_Master.h"\r
 #include "../common/I2C/MAX44009.h"\r
-#include "../common/I2C/SHT2x.h"\r
+#include "../common/I2C/SHT2xV2.h"\r
+#include "../common/calibr.h"\r
+#include "../common/owSlave_tools.h"\r
 \r
 \r
-extern void OWINIT();\r
-extern void EXTERN_SLEEP();\r
-\r
+OWST_EXTERN_VARS\r
 \r
 uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/\r
 uint8_t owid2[8]={0x26, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x67};/**/\r
 uint8_t config_info1[26]={0x03,0x04, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x02,15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};\r
 uint8_t config_info2[26]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x00,0x00, 0x02,7,0x00,7,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};\r
 \r
-\r
-       \r
-#if (owid>128) \r
-#error "Variable not correct"\r
-#endif\r
-\r
-extern uint8_t mode;\r
-extern uint8_t gcontrol;\r
-extern uint8_t reset_indicator;\r
-extern uint8_t alarmflag;\r
-volatile uint8_t wdcounter=5;\r
-\r
-\r
-#if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)\r
-ISR(WATCHDOG_vect) {\r
-#else\r
-ISR(WDT_vect) {\r
-#endif \r
-       //sleep_disable();          // Disable Sleep on Wakeup\r
-       wdcounter++;\r
-       if (reset_indicator==1) reset_indicator++;\r
-       else if (reset_indicator==2) mode=0;\r
-/*     if (timeout==2) {\r
-               DIS_TIMER;\r
-               EN_OWINT;\r
-               mode=OWM_SLEEP;\r
-       }\r
-       timeout++;*/\r
-       //sleep_enable();           // Enable Sleep Mode\r
-\r
-}\r
-\r
-int testSW(void) {\r
-       uint8_t r;\r
-       DDRB&=~(1<<PORTB0);  //Eingang\r
-       __asm__ __volatile__ ("nop");\r
-       PORTB|=(1<<PORTB0); //Pullup\r
-       __asm__ __volatile__ ("nop");\r
-       __asm__ __volatile__ ("nop");\r
-       __asm__ __volatile__ ("nop");\r
-       __asm__ __volatile__ ("nop");\r
-       __asm__ __volatile__ ("nop");\r
-       r=PINB&(1<<PORTB0);\r
-       __asm__ __volatile__ ("nop");\r
-       PORTB&=~(1<<PORTB0);\r
-       __asm__ __volatile__ ("nop");\r
-       DDRB|=(1<<PORTB0);  //Eingang\r
-       return (r==0);\r
-       \r
+OWST_WDT_ISR\r
        \r
-}\r
+OWST_TESTSW\r
 \r
 \r
 typedef union {\r
@@ -150,21 +102,17 @@ typedef union {
 volatile pack2_t pack2;\r
 \r
 \r
+volatile int16_t DS2438_2_TEMP;\r
+volatile uint16_t DS2438_2_VAD;\r
+volatile uint16_t DS2438_2_VDD=0x01F4;\r
 \r
-\r
-volatile int16_t am2302_temp;\r
-volatile uint16_t am2302_hum;\r
-\r
-\r
-uint8_t userRegister[1];\r
-int16_t sRH,sT;\r
-volatile double temperatureC,humidityRH;\r
-volatile double l;\r
+double temperatureC,humidityRH;\r
+double l;\r
 \r
 uint8_t max_adr=0;\r
 \r
 int main(void){\r
-    PRR|=(1<<PRADC);  // adc for save Power\r
+    OWST_INIT_USI_ON\r
        pack1.temp=0x0550;\r
        pack1.config=0x7F;\r
        pack1.TH=75;\r
@@ -172,40 +120,22 @@ int main(void){
        pack1.rrFF=0xFF;\r
        pack1.rr00=0;\r
        pack1.rr10=0x10;\r
-       PORTA=0xFF;\r
-       PORTB=0xFF;\r
        OWINIT();\r
-\r
-       MCUCR &=~(1<<PUD); //All Pins Pullup...\r
-       MCUCR |=(1<<BODS);\r
-\r
-       WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)\r
-       WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt\r
-       (1<<WDP3) | (1<<WDP0);   // Set Timeout to ~8 seconds\r
+       OWST_EN_PULLUP\r
+       OWST_WDR_CONFIG8\r
        if (testSW()) {\r
-               config_info2[5]=8;\r
+               config_info2[5]=12;\r
                }else{\r
                config_info2[5]=7;\r
        }\r
-       MCUSR=0;\r
        sei();\r
        USI_TWI_Master_Initialise();\r
        if (checkMAX44009(0)) max_adr=0; else max_adr=1 ;\r
-       SHT2x_SoftReset();\r
-       SHT2x_ReadUserRegister(userRegister);\r
-       //(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;\r
-       SHT2x_WriteUserRegister(userRegister); //write changed user reg\r
-       // --- measure humidity with "Hold Master Mode (HM)" ---\r
-       SHT2x_MeasurePoll(HUMIDITY, &sRH);\r
-       // --- measure temperature with "Polling Mode" (no hold master) ---\r
-       SHT2x_MeasurePoll(TEMP, &sT);\r
-       //-- calculate humidity and temperature --\r
-       //temperatureC = SHT2x_CalcTemperatureC(sT);\r
-       //humidityRH = SHT2x_CalcRH(sRH);\r
+       _delay_ms(10);\r
        \r
+       initSHT2x();\r
        gcontrol=1;\r
        sei();\r
-       //DDRB|=(1<<PINB1);\r
     while(1)   {\r
                if ((gcontrol&2)==2){\r
                        wdcounter=3;\r
@@ -214,28 +144,26 @@ int main(void){
                }\r
                \r
                if (wdcounter>2) {\r
-                       //PORTB|=(1<<PINB1); //Dauer 440ms\r
-                       SHT2x_MeasurePoll(HUMIDITY, &sRH);\r
-                       // --- measure temperature with "Polling Mode" (no hold master) ---\r
-                       SHT2x_MeasurePoll(TEMP, &sT);\r
-                       //-- calculate humidity and temperature --\r
-                       temperatureC = SHT2x_CalcTemperatureC(sT)*10.0-2;\r
-                       humidityRH = SHT2x_CalcRH(sRH)*10.0+5;\r
-                       if (humidityRH>1000) humidityRH=1000;\r
-                       if (testSW()) {\r
-                               am2302_hum= humidityRH*10.0;\r
-                               am2302_temp=temperatureC*25.6;\r
-                               //\r
-                               config_info2[5]=8;\r
-                               \r
-                       }else{\r
-                               \r
-                               double hhum=(1.0546-0.000216*temperatureC)*(humidityRH);\r
-                               //am2302_hum=0.318*hhum +76.0;\r
-                               am2302_hum=0.31*hhum +80;\r
-                               am2302_temp=temperatureC*25.6;\r
-                               //am2302_temp=am2302_temp-45;\r
-                               config_info2[5]=7;\r
+                               getSHT2xHumTemp(&temperatureC,&humidityRH);\r
+                               double RH=calibr_hum(temperatureC,-0.2,humidityRH)*10.0;\r
+                               double TC =temperatureC *10.0-2;\r
+\r
+                               if (testSW()) {\r
+                                       DS2438_2_VAD=RH;\r
+                                       DS2438_2_TEMP=TC*25.6;\r
+                                       //am2302_temp=am2302_temp-20;\r
+                                       config_info2[5]=12;\r
+                                       DS2438_2_VDD=0x01F4;\r
+                                       \r
+                               }else{\r
+                                       \r
+                                        double hhum=(1.0546-0.000216*TC)*(RH);\r
+                                       //am2302_hum=0.318*hhum +76.0;\r
+                                       DS2438_2_VAD=0.31*hhum +80;\r
+                                       DS2438_2_TEMP=TC*25.6;\r
+                                       //am2302_temp=am2302_temp-20;\r
+                                       config_info2[5]=7;\r
+                                       DS2438_2_VDD=0x01F4;\r
                        }\r
                        wdcounter=0;\r
                }\r
@@ -258,24 +186,7 @@ int main(void){
                        sei();                  \r
                        EXTERN_SLEEP();         \r
                }\r
-               \r
-\r
-               \r
-#if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)\r
-                       if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))\r
-#endif                 \r
-#if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)\r
-                       if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))\r
-#endif\r
-                         {\r
-\r
-                       MCUCR|=(1<<SE)|(1<<SM1);\r
-                       MCUCR&=~(1<<ISC01);\r
-               } else {\r
-                       MCUCR|=(1<<SE);\r
-                       MCUCR&=~(1<<SM1);\r
-               }\r
-               asm("SLEEP");\r
+               OWST_MAIN_END\r
    }\r
 \r
 \r