- OWINIT save Register used in C
[owSlave2.git] / DS18B20_VOC_DS2438_SHT / DS18B20_DS2438.c
index 5a0094e..0e9ae0a 100644 (file)
 #include "../common/I2C/USI_TWI_Master.h"
 #include "../common/I2C/MAX1164x.h"
 #include "../common/I2C/SHT2x.h"
-#include <avr/pgmspace.h>
+#include "../common/calibr.h"
 
 extern void OWINIT();
 extern void EXTERN_SLEEP();
 
 
-const int16_t hl[5] PROGMEM = {0,400,650,850,1000};  //H*10
-
-//log values *1000
-const int16_t il[5][5] PROGMEM= {
-{470,  247,    140,    77,     49},
-{336,  104,    0,      -62,    -83},
-{207,  -20,    -139,   -198,   -223},
-{86,   -139,   -248,   -315,   -342},
-{-30,  -248,   -371,   -431,   -462}};
-
-
-
-
-extern void OWINIT();
-extern void EXTERN_SLEEP();
-
 
-uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/
-uint8_t owid2[8]={0x26, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x67};/**/
-uint8_t config_info1[16]={0x08,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};        
-uint8_t config_info2[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x07,0x08, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};        
+volatile uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/
+volatile uint8_t owid2[8]={0x26, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x67};/**/
+volatile uint8_t config_info1[16]={0x08,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};       
+volatile uint8_t config_info2[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x07,0x08, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};       
        
 #if (owid>128) 
 #error "Variable not correct"
@@ -146,8 +130,8 @@ volatile uint16_t am2302_hum;
 
 uint8_t userRegister[1];
 int16_t sRH,sT;
-volatile double temperatureC,humidityRH;
-volatile double l;
+double temperatureC,humidityRH,hhum;
+double l;
 
 uint8_t max_adr=0;
 #define CH0_M MAX1164x_C_SCAN0|MAX1164x_C_SGL
@@ -155,44 +139,25 @@ uint8_t max_adr=0;
 #define CH0_CH1 MAX1164x_C_SCAN0
 //|MAX1164x_C_CS0
 
-//T und H mit 10 multipliziert
-
-double interp(int16_t t, int16_t h) {
-double ya,yb;
-uint8_t h1,h2,t1,t2;
-int16_t x1,x2,y1_,y2;
-       h1=0;
-       h2=0;
-       for(uint8_t i=0;i<5;i++) {
-               int16_t hlw=pgm_read_word(&(hl[i]));
-               if (h>hlw) h1=i;
-       }
-       h2=h1+1;
-       if (h2>4) {h2=4;h1=3;}
-       t2=((uint8_t)t/100); //t mit 10 multipliziert
-       if (t2<1) t2=1;
-       t1=t2-1;
-       x1=pgm_read_word(&(hl[h1]));
-       x2=pgm_read_word(&(hl[h2]));
-       y1_=pgm_read_word(&(il[t1][h1]));
-       y2=pgm_read_word(&(il[t1][h2]));
-       double d=(double)(h-x1)/(x2-x1);
-       ya=y1_+(y2-y1_)*d;
-       y1_=pgm_read_word(&(il[t2][h1]));
-       y2=pgm_read_word(&(il[t2][h2]));
-       yb=y1_+(y2-y1_)*d;
-       x1=(t1+1)*100;
-       x2=x1+100;
-       return exp((ya+(yb-ya)/(double)(x2-x1)*(t-x1))/1000.0);
-       
+
+inline double interp(double t, double h) {
+       double h2;
+        double t2;
+       h2=h*h;
+       t2=t*t;
+       return 4.76111e-9*h2*t2-3.96956e-7*h2*t+0.0000408889*h2-1.07132e-6*h*t2+0.000115968*h*t-0.0101333*h+0.000163806*t2-0.0241179*t+1.80591;
 }
+
 uint8_t cmode=0;
 double R0=0;
 int16_t hr;
 uint8_t startup=5;
 uint8_t reset_R0=0;
+int16_t mr;
+double ip;
 
 int main(void){
+       //volatile double test=calibr_hum(20,1,20);
     PRR|=(1<<PRADC);  // adc for save Power
        pack1.temp=0x0550;
        pack1.config=0x7F;
@@ -228,18 +193,11 @@ int main(void){
        }
 
        
-       /*volatile double test=interp(0,800);
-       test=interp(100,900);
-       test=interp(110,400);
-       test=interp(300,200);
-       test=interp(300,210);
-       test=interp(280,600);
-       test=interp(100,900);
-       */
 
        WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)
        WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt
-       (1<<WDP3) | (1<<WDP0);    // Set Timeout to ~8 seconds
+       (1<<WDP0) |(1<<WDP2) | (1<<WDP1);    // Set Timeout to ~2 seconds
+       //(1<<WDP3) | (1<<WDP0);    // Set Timeout to ~8 seconds
 
        MCUSR=0;
        USI_TWI_Master_Initialise();
@@ -249,12 +207,12 @@ int main(void){
        //(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);
        MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);//#define MAX1164x_C_CS0
        _delay_ms(30); //Internal Referenz start
        //2970 -> 1,5V  
@@ -292,29 +250,43 @@ int main(void){
                        // --- 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);
+                       temperatureC = SHT2x_CalcTemperatureC(sT);
+                       humidityRH = SHT2x_CalcRH(sRH);
+                       ip=interp(temperatureC,humidityRH);
+                       humidityRH=calibr_hum(temperatureC,-1,humidityRH)*10.0;
+                       //humidityRH=humidityRH*10.0;
+                       temperatureC =temperatureC *10.0-10;
+                       hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
                        am2302_hum=0.318*hhum +76.0;
                        am2302_temp=temperatureC*25.6;
                        //PORTB&=~(1<<PINB1);
+                       mr=0;
+//Kritische Sektion !______________________________
                        PORTB&=~(1<<PINB1);
-                       _delay_us(500);
-                       l=MAX1164x_read();
+                       _delay_us(150);
+                       mr+=MAX1164x_read();
+                       _delay_us(150);
+                       mr+=MAX1164x_read();
+                       
                        PORTB|=(1<<PINB1);
+//ENDE Kritische Sektion !______________________________
+                       //l=mr/2.0*2.048/4096; 
+                       // l maximal 2  mr max 4096  //mr 2V=8000 
+                       
                        if (cmode) {
-                               l=l*2.048/4096+1.5; //Spannung real
-                       } else {
-                               l=l*2.048/4096; //Spannung real
-                       }
-                       if (l>1.8) {
+                               //l+=1.5; //Spannung real
+                               mr+=6000;
+                       } 
+                       //if (l>1.8) {
+                       if (mr>7200) {                          
                                if (cmode==0) {
                                        MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
                                        cmode=1;
                                        pack2.page3[4]=cmode;
                                }
                        }
-                       if (l<1.6) {
+                       //if (l<1.6) {
+                       if (mr<6400) {  
                                if (cmode==1) {
                                        MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
                                        cmode=0;
@@ -323,12 +295,14 @@ int main(void){
                                
                                
                        }
-                       hr=l*10000.0;
+                       //hr=l*10000.0;
+                       hr=mr*5/2;
                        pack2.page3[2]=hr&0xFF;
                        pack2.page3[3]=(hr>>8)&0xFF;
-                       l=( 3/l- 1) *27;
+                       l=mr/4000.0;
+                       l=( 3/l- 1) *30;
                        pack2.current=l*100;
-                       float ip=interp(temperatureC,humidityRH);
+                       //ip=interp(temperatureC,humidityRH);
                        hr=ip*1000;
                        pack2.page3[5]=hr&0xFF;
                        pack2.page3[6]=(hr>>8)&0xFF;
@@ -360,6 +334,9 @@ int main(void){
                         l=l/R0;
                        l=log(1/l);
                        l=l*160*4; //fuer DS18B20
+                       if (l>500) {
+                               l=100;
+                       }
                        wdcounter=0;
                        
                }