New I2C devices
[owSlave2.git] / DS2438_VOC_DS2438_SHT_Wiregate / DS2438_DS2438.c
index 00a58ef..05b8a28 100644 (file)
@@ -63,6 +63,7 @@ extern uint8_t gcontrol;
 extern uint8_t reset_indicator;\r
 extern uint8_t alarmflag;\r
 volatile uint8_t wdcounter=1;\r
+extern uint8_t cpsp2;\r
 \r
 \r
 typedef union {\r
@@ -125,23 +126,50 @@ typedef union {
                uint8_t threshold; //8\r
                \r
                uint8_t page1[8]; //9\r
-               #if  defined(__AVR_ATtiny25__)\r
-               #else\r
+               \r
                uint8_t page2[8]; //17\r
                uint8_t page3[8]; //25\r
                \r
-               uint8_t page4[8];  //33\r
-               uint8_t page5[8];  //41\r
-               uint8_t page6[8];  //49\r
+               union{\r
+                       uint8_t page4[8];  //33\r
+                       struct {\r
+                               uint16_t tol_s8;\r
+                               uint16_t tol_d;\r
+                               uint16_t r_day_max;\r
+                               uint16_t r_week_max;\r
+                       };\r
+               };\r
+               union{\r
+                       uint8_t page5[8];  //41\r
+                       struct {\r
+                               uint8_t codeVOC; // immer 0x37 nach Neustart\r
+                               uint8_t days_of_r0; //Anzahl der Tage fuer die r0 ermittelt wird\r
+                               int8_t corr_VOC_mult; //r0 corr\r
+                               int8_t corr_VOC_div;\r
+                               uint16_t vv3;\r
+                               uint8_t time_corr; //Wiregate;\r
+                               uint8_t reset_code;\r
+                       };\r
+                       uint16_t page5d[4];\r
+               };\r
+               union{\r
+                       uint8_t page6[8]; //25\r
+                       struct {\r
+                               uint16_t R0;\r
+                               uint16_t VS;\r
+                               uint8_t cmode;\r
+                               int16_t ip;\r
+                               uint8_t free;\r
+                       };\r
+               };\r
                uint8_t page7[8];  //57\r
                \r
-               #endif\r
        };\r
 } pack2_t;\r
 volatile pack2_t pack2;\r
 \r
-#define TIME_CORR 5\r
-volatile int8_t time_corr_count=TIME_CORR;\r
+//#define TIME_CORR 3\r
+volatile int8_t time_corr_count;//=TIME_CORR;\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
@@ -156,7 +184,7 @@ ISR(WATCHDOG_vect) {
                        if (time_corr_count<=0) {\r
                                //wdcounter++;\r
                                pack1.etm+=1;\r
-                               time_corr_count=TIME_CORR;\r
+                               time_corr_count=pack2.time_corr;\r
                        }\r
        /*      if ((pack1.page1[0]&0x0F)==0) {\r
                         pack1.etm+=2;\r
@@ -174,15 +202,6 @@ ISR(WATCHDOG_vect) {
 \r
 \r
 \r
-uint16_t pR0;\r
-uint16_t pVS;\r
-uint8_t pcmode;\r
-int16_t pip;\r
-\r
-uint16_t ptol_s8;\r
-uint16_t ptol_d;\r
-uint16_t pr_day_max;\r
-uint16_t pr_week_max;\r
 \r
 \r
 volatile int16_t DS2438_1_TEMP;\r
@@ -202,7 +221,7 @@ uint8_t max_adr=0;
 #define CH0_CH1 MAX1164x_C_SCAN0\r
 //|MAX1164x_C_CS0\r
 \r
-uint16_t weekmaxarr[8];\r
+uint16_t weekmaxarr[33];\r
 \r
 //Kompensieren der Abhänigkeit von RS/RO von Temperatur und Luftfeuchte\r
 inline double interp(double t, double h) {\r
@@ -243,6 +262,10 @@ int testSW(void) {
 #define EEPROM_R0d 2\r
 #define EEPROM_R0w 4\r
 #define EEPROM_dol 6\r
+#define EEPROM_CODE_DAYOFR0 8\r
+#define EEPROM_CORR 10\r
+#define EEPROM_FREE 12\r
+#define EEPROM_TCORR_RESET 14\r
 \r
 \r
 uint16_t readEEPROM(uint8_t addr,uint16_t def) {\r
@@ -295,18 +318,25 @@ int main(void){
 \r
        pack1.temp=0x0550;\r
        pack2.page3[0]=0xF1;\r
-       pcmode=0;\r
-       pR0=readEEPROM(EEPROM_R0,1);\r
-       R0=pR0/100.0;\r
+       pack2.cmode=0;\r
+       pack2.R0=readEEPROM(EEPROM_R0,1);\r
+       R0=pack2.R0/100.0;\r
                \r
-       pr_day_max=readEEPROM(EEPROM_R0d,1);\r
-       pr_week_max=readEEPROM(EEPROM_R0w,1);\r
-       ptol_d=readEEPROM(EEPROM_dol,0); \r
-       ptol_s8=0;  //Tag faengt mit Einschalten an\r
-       for(uint8_t i=0;i<7;i++) {\r
-               weekmaxarr[i]=pr_week_max;\r
-       }\r
+       pack2.r_day_max=readEEPROM(EEPROM_R0d,1);\r
+       pack2.r_week_max=readEEPROM(EEPROM_R0w,1);\r
+       pack2.tol_d=readEEPROM(EEPROM_dol,0); \r
+       pack2.tol_s8=0;  //Tag faengt mit Einschalten an\r
+       \r
 \r
+       pack2.page5d[0]=readEEPROM(EEPROM_CODE_DAYOFR0,0x0437);\r
+       pack2.page5d[1]=readEEPROM(EEPROM_CORR,0x0101);\r
+       pack2.page5d[2]=readEEPROM(EEPROM_FREE,0x0);\r
+       pack2.page5d[3]=readEEPROM( EEPROM_TCORR_RESET,0x0005);\r
+       time_corr_count=pack2.time_corr;\r
+\r
+       for(uint8_t i=0;i<pack2.days_of_r0;i++) {\r
+               weekmaxarr[i]=pack2.r_week_max;\r
+       }\r
        if (testSW()) {\r
                config_info2[5]=12;\r
        }else{\r
@@ -323,60 +353,69 @@ int main(void){
        sei();\r
        //DDRB|=(1<<PINB1);\r
     while(1)   {\r
-/*             if (pack1.config==0x1F) {  //Reset R0\r
-                       pack1.config=0x7F;\r
-                       R0=1;\r
-                       pR0=0;\r
-                       writeEEPROM(EEPROM_R0,0);\r
-               } \r
-               if (pack1.config==0x05) {  //Reset all Data\r
-                       pack1.config=0x7F;                              \r
-                       pr_day_max=1;\r
-                       pr_week_max=1;\r
-                       ptol_d=0;\r
-                       ptol_s8=0;  //Tag faengt mit Einschalten an     \r
-                       R0=1;   \r
-                       writeEEPROM(EEPROM_R0,0xFF);\r
-                       writeEEPROM(EEPROM_R0d,0xFF);  //Maximum des Tages\r
-                       writeEEPROM(EEPROM_R0w,0xFF); //Maximum der Letzten 7 Tage\r
-                       writeEEPROM(EEPROM_dol,0xFF); //Anzahl der Betriebstage         \r
-                       for(uint8_t i=0;i<7;i++) {\r
-                               weekmaxarr[i]=1;\r
-                       }                               \r
-               } */\r
+                       if ((cpsp2&0x80)!=0) {\r
+                               if ((cpsp2&0x0F)==5) {\r
+                                       if (pack2.reset_code==0x01) {\r
+                                               R0=1;\r
+                                               pack2.R0=0;\r
+                                               writeEEPROM(EEPROM_R0,0);\r
+                                       } else if (pack2.reset_code==0x05) {\r
+                                               pack2.r_day_max=1;\r
+                                               pack2.r_week_max=1;\r
+                                               pack2.tol_d=0;\r
+                                               pack2.tol_s8=0;  //Tag faengt mit Einschalten an\r
+                                               R0=1;\r
+                                               writeEEPROM(EEPROM_R0,0xFF);\r
+                                               writeEEPROM(EEPROM_R0d,0xFF);  //Maximum des Tages\r
+                                               writeEEPROM(EEPROM_R0w,0xFF); //Maximum der Letzten 7 Tage\r
+                                               writeEEPROM(EEPROM_dol,0xFF); //Anzahl der Betriebstage\r
+                                               for(uint8_t i=0;i<7;i++) {\r
+                                                       weekmaxarr[i]=1;\r
+                                               }\r
+\r
+                                       } else {\r
+                                               writeEEPROM(EEPROM_CODE_DAYOFR0,0x0037|(pack2.days_of_r0<<8));\r
+                                               writeEEPROM(EEPROM_CORR,pack2.page5d[1]);\r
+                                               writeEEPROM(EEPROM_FREE,pack2.page5d[2]);\r
+                                               writeEEPROM(EEPROM_TCORR_RESET,pack2.time_corr);\r
+                                       }\r
+                               }\r
+                               cpsp2=0;\r
+               }\r
                if (wdcounter>0) {  //8s\r
-                       ptol_s8++;\r
+                       pack2.tol_s8++;\r
                        \r
                        //pack1.dis+=8;\r
                        //pack1.eoc+=8;\r
-                       if (ptol_s8>(10000))  {//10800 ist theortisch der Tag aber meistens zu lang\r
-                               ptol_s8=0;\r
-                               ptol_d++;  //rund 180 Jahre :-)\r
-                               pr_week_max=0;\r
-                               weekmaxarr[7]=pr_day_max;\r
-                               for(uint8_t i=0;i<7;i++) {\r
+                       if (pack2.tol_s8>(10000))  {//10800 ist theortisch der Tag aber meistens zu lang\r
+                               pack2.tol_s8=0;\r
+                               pack2.tol_d++;  //rund 180 Jahre :-)\r
+                               pack2.r_week_max=0;\r
+                               weekmaxarr[pack2.days_of_r0]=pack2.r_day_max;\r
+                               for(uint8_t i=0;i<pack2.days_of_r0;i++) {\r
                                        weekmaxarr[i]=weekmaxarr[i+1];\r
                                        //maximum of week\r
-                                       if (weekmaxarr[i]>pr_week_max) pr_week_max=weekmaxarr[i];\r
+                                       if (weekmaxarr[i]>pack2.r_week_max) pack2.r_week_max=weekmaxarr[i];\r
                                }\r
-                               if (ptol_d>7) {\r
-                                       pR0=pr_week_max;\r
+                               if (pack2.tol_d>pack2.days_of_r0) {\r
+                                       pack2.R0=pack2.r_week_max;\r
                                } else {\r
-                                       pR0=pr_day_max;\r
+                                       pack2.R0=pack2.r_day_max;\r
                                }\r
                                //R0=//R0-0.5*(pack2.R0/100-R0);\r
-                               R0=R0-(R0-pR0/100.0)*0.5        ;\r
-                               pR0=R0*100;\r
-                               writeEEPROM(EEPROM_R0,pR0);\r
-                               writeEEPROM(EEPROM_R0d,pr_day_max);  //Maximum des Tages\r
-                               writeEEPROM(EEPROM_R0w,pr_week_max); //Maximum der Letzten 7 Tage\r
-                               writeEEPROM(EEPROM_dol,ptol_d); //Anzahl der Betriebstage\r
-                               pr_day_max=0;\r
+                               //R0=R0-(R0-pack2.R0/100.0)*0.5 ;\r
+                               R0=pack2.R0/100.0;\r
+                               pack2.R0=R0*100;\r
+                               writeEEPROM(EEPROM_R0,pack2.R0);\r
+                               writeEEPROM(EEPROM_R0d,pack2.r_day_max);  //Maximum des Tages\r
+                               writeEEPROM(EEPROM_R0w,pack2.r_week_max); //Maximum der Letzten 7 Tage\r
+                               writeEEPROM(EEPROM_dol,pack2.tol_d); //Anzahl der Betriebstage\r
+                               pack2.r_day_max=0;\r
                        }\r
                        if (startup!=0) startup--;\r
                        getSHT2xHumTemp(&temperatureC,&humidityRH);\r
                        ip=interp(temperatureC,humidityRH);\r
-                       pip=ip*1000;\r
+                       pack2.ip=ip*1000;\r
                        double RH=calibr_hum(temperatureC,-0.2,humidityRH)*10.0;\r
                        double TC =temperatureC *10.0-2;\r
 \r
@@ -406,27 +445,27 @@ int main(void){
                        //l=mr/2.0*2.048/4096; \r
                        // l maximal 2  mr max 4096  //mr 2V=8000 \r
                        \r
-                       if (pcmode) { //cmode=0 V 0..2 V cmode=1 V 1.5..3.5V\r
+                       if (pack2.cmode) { //cmode=0 V 0..2 V cmode=1 V 1.5..3.5V\r
                                //l+=1.5; //Spannung real\r
                                mr+=6000;\r
                        } \r
                        //if (l>1.8) {\r
                        if (mr>7200) {                          \r
-                               if (pcmode==0) {\r
+                               if (pack2.cmode==0) {\r
                                        MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);\r
-                                       pcmode=1;\r
+                                       pack2.cmode=1;\r
                                }\r
                        }\r
                        //if (l<1.6) {\r
                        if (mr<6400) {  \r
-                               if (pcmode==1) {\r
+                               if (pack2.cmode==1) {\r
                                        MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);\r
-                                       pcmode=0;\r
+                                       pack2.cmode=0;\r
                                }\r
                                \r
                                \r
                        }\r
-                       pVS=mr*5/2;\r
+                       pack2.VS=mr*5/2;\r
                        l=mr/4000.0;\r
                        l=( 3/l- 1) *30;\r
                        pack2.current=l*100;\r
@@ -436,17 +475,18 @@ int main(void){
                        if (startup==0){\r
                                if (l>R0) {\r
                                        R0=l;\r
-                                       pR0=R0*100;\r
-                                       writeEEPROM(EEPROM_R0,pR0);\r
+                                       pack2.R0=R0*100;\r
+                                       writeEEPROM(EEPROM_R0,pack2.R0);\r
 \r
                                }\r
-                               if (l*100>pr_day_max) {\r
-                                       pr_day_max=l*100;\r
+                               if (l*100>pack2.r_day_max) {\r
+                                       pack2.r_day_max=l*100;\r
                                }\r
                        } else if (l<R0) l=R0; //negative Werte am Anfang verhintern\r
                        l=exp((1-(l/R0))*6.05)-1;// exp((1-($val)/55)*5.75);  (5.75 geht über 125 6.05 geht bis 240... mittlere Linie im Datenblatt)\r
                        //l=(l/R0*100.0);\r
                        l=l*0.5*35; //fuer DS2438\r
+                       l=l*(double)pack2.corr_VOC_mult/(double)pack2.corr_VOC_div;\r
                        if (l==0) l=1;\r
                        gcontrol=1;\r
                        wdcounter=0;\r