- New Device compinations
[owSlave2.git] / DS18B20_VOC_DS2438_SHT / DS18B20_DS2438.c
1
2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //  * Redistributions of source code must retain the above copyright
10 //    notice, this list of conditions and the following disclaimer.
11 //  * Redistributions in binary form must reproduce the above copyright
12 //    notice, this list of conditions and the following disclaimer in the
13 //    documentation and/or other materials provided with the
14 //    distribution.
15 //  * All advertising materials mentioning features or use of this
16 //    software must display the following acknowledgement: This product
17 //    includes software developed by tm3d.de and its contributors.
18 //  * Neither the name of tm3d.de nor the names of its contributors may
19 //    be used to endorse or promote products derived from this software
20 //    without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 #define F_CPU 8000000UL
35 #include <avr/io.h>
36 #include <avr/interrupt.h>
37 #include <util/delay.h>
38 #include <avr/wdt.h>
39 #include <avr/sleep.h>
40 #include <avr/pgmspace.h>
41 #include "../common/I2C/USI_TWI_Master.h"
42 #include "../common/I2C/MAX1164x.h"
43 #include "../common/I2C/SHT2x.h"
44 #include <avr/pgmspace.h>
45
46 extern void OWINIT();
47 extern void EXTERN_SLEEP();
48
49
50 const int16_t hl[5] PROGMEM = {0,400,650,850,1000};  //H*10
51
52 //log values *1000
53 const int16_t il[5][5] PROGMEM= {
54 {470,   247,    140,    77,     49},
55 {336,   104,    0,      -62,    -83},
56 {207,   -20,    -139,   -198,   -223},
57 {86,    -139,   -248,   -315,   -342},
58 {-30,   -248,   -371,   -431,   -462}};
59
60
61
62
63 extern void OWINIT();
64 extern void EXTERN_SLEEP();
65
66
67 uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/
68 uint8_t owid2[8]={0x26, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x67};/**/
69 uint8_t config_info1[16]={0x08,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 
70 uint8_t config_info2[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x07,0x08, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 
71         
72 #if (owid>128) 
73 #error "Variable not correct"
74 #endif
75
76 extern uint8_t mode;
77 extern uint8_t gcontrol;
78 extern uint8_t reset_indicator;
79 extern uint8_t alarmflag;
80 volatile uint8_t wdcounter=1;
81
82
83 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
84 ISR(WATCHDOG_vect) {
85 #else
86 ISR(WDT_vect) {
87 #endif 
88         wdcounter++;
89         if (reset_indicator==1) reset_indicator++;
90         else if (reset_indicator==2) mode=0;
91
92
93 }
94
95
96 typedef union {
97         volatile uint8_t bytes[8];
98         struct {
99                 uint16_t temp;  //0
100                 uint8_t TH;  //2
101                 uint8_t TL;  //3
102                 uint8_t config;  //4
103                 uint8_t rrFF; //5
104                 uint8_t rr00; //6
105                 uint8_t rr10; //7
106         };
107 } pack1_t;
108 volatile pack1_t pack1;
109
110
111
112 typedef union {
113         #if  defined(__AVR_ATtiny25__)
114         volatile uint8_t bytes[16];
115         #else
116         volatile uint8_t bytes[64];
117         #endif
118         struct {
119                 uint8_t status;  //1
120                 int16_t temp;  //2
121                 uint16_t voltage;  //4
122                 int16_t current;  //6
123                 uint8_t threshold; //8
124                 
125                 uint8_t page1[8]; //9
126                 #if  defined(__AVR_ATtiny25__)
127                 #else
128                 uint8_t page2[8]; //17
129                 uint8_t page3[8]; //25
130                 uint8_t page4[8];  //33
131                 uint8_t page5[8];  //41
132                 uint8_t page6[8];  //49
133                 uint8_t page7[8];  //57
134                 //uint8_t crc;  //65
135                 #endif
136         };
137 } pack2_t;
138 volatile pack2_t pack2;
139
140
141
142
143 volatile int16_t am2302_temp;
144 volatile uint16_t am2302_hum;
145
146
147 uint8_t userRegister[1];
148 int16_t sRH,sT;
149 volatile double temperatureC,humidityRH;
150 volatile double l;
151
152 uint8_t max_adr=0;
153 #define CH0_M MAX1164x_C_SCAN0|MAX1164x_C_SGL
154 #define CH1_M MAX1164x_C_SCAN0|MAX1164x_C_SGL|MAX1164x_C_CS0
155 #define CH0_CH1 MAX1164x_C_SCAN0
156 //|MAX1164x_C_CS0
157
158 //T und H mit 10 multipliziert
159
160 double interp(int16_t t, int16_t h) {
161 double ya,yb;
162 uint8_t h1,h2,t1,t2;
163 int16_t x1,x2,y1_,y2;
164         h1=0;
165         h2=0;
166         for(uint8_t i=0;i<5;i++) {
167                 int16_t hlw=pgm_read_word(&(hl[i]));
168                 if (h>hlw) h1=i;
169         }
170         h2=h1+1;
171         if (h2>4) {h2=4;h1=3;}
172         t2=((uint8_t)t/100); //t mit 10 multipliziert
173         if (t2<1) t2=1;
174         t1=t2-1;
175         x1=pgm_read_word(&(hl[h1]));
176         x2=pgm_read_word(&(hl[h2]));
177         y1_=pgm_read_word(&(il[t1][h1]));
178         y2=pgm_read_word(&(il[t1][h2]));
179         double d=(double)(h-x1)/(x2-x1);
180         ya=y1_+(y2-y1_)*d;
181         y1_=pgm_read_word(&(il[t2][h1]));
182         y2=pgm_read_word(&(il[t2][h2]));
183         yb=y1_+(y2-y1_)*d;
184         x1=(t1+1)*100;
185         x2=x1+100;
186         return exp((ya+(yb-ya)/(double)(x2-x1)*(t-x1))/1000.0);
187         
188 }
189 uint8_t cmode=0;
190 double R0=0;
191 int16_t hr;
192 uint8_t startup=5;
193 uint8_t reset_R0=0;
194
195 int main(void){
196     PRR|=(1<<PRADC);  // adc for save Power
197         pack1.temp=0x0550;
198         pack1.config=0x7F;
199         pack1.TH=75;
200         pack1.TL=70;
201         pack1.rrFF=0xFF;
202         pack1.rr00=0;
203         pack1.rr10=0x10;
204         PORTA=0xFF;
205         PORTB=0xFF;
206         OWINIT();
207         PORTB|=(1<<PINB1);
208         DDRB|=(1<<PINB1);
209         PORTA|=(1<<PINA0);
210         DDRA|=(1<<PINA0);
211         
212         MCUCR &=~(1<<PUD); //All Pins Pullup...
213         MCUCR |=(1<<BODS);
214         
215         while(EECR & (1<<EEPE));
216         EEARL=1;
217         EECR |= (1<<EERE);
218         hr=EEDR;
219         if (hr!=0xFF) {
220                 hr=hr<<8;
221                 while(EECR & (1<<EEPE));
222                 EEARL=0;
223                 EECR |= (1<<EERE);
224                 hr|=EEDR;
225                 R0=hr/100.0;
226                 pack2.page3[0]=hr&0xFF;
227                 pack2.page3[1]=(hr>>8)&0xFF;
228         }
229
230         
231         /*volatile double test=interp(0,800);
232         test=interp(100,900);
233         test=interp(110,400);
234         test=interp(300,200);
235         test=interp(300,210);
236         test=interp(280,600);
237         test=interp(100,900);
238         */
239
240         WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)
241         WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt
242         (1<<WDP3) | (1<<WDP0);    // Set Timeout to ~8 seconds
243
244         MCUSR=0;
245         USI_TWI_Master_Initialise();
246         
247         SHT2x_SoftReset();
248         SHT2x_ReadUserRegister(userRegister);
249         //(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;
250         SHT2x_WriteUserRegister(userRegister); //write changed user reg
251         // --- measure humidity with "Hold Master Mode (HM)" ---
252         SHT2x_MeasurePoll(HUMIDITY, &sRH);
253         // --- measure temperature with "Polling Mode" (no hold master) ---
254         SHT2x_MeasurePoll(TEMP, &sT);
255         //-- calculate humidity and temperature --
256         temperatureC = SHT2x_CalcTemperatureC(sT);
257         humidityRH = SHT2x_CalcRH(sRH);
258         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);//#define MAX1164x_C_CS0
259         _delay_ms(30); //Internal Referenz start
260         //2970 -> 1,5V  
261         gcontrol=1;
262         sei();
263         //DDRB|=(1<<PINB1);
264     while(1)   {
265                 if (pack1.config==0x1F) {
266                         if (reset_R0==0) {
267                                 reset_R0=1;
268                                 pack1.config=0x7F;
269                                 R0=0;
270                                 hr=R0*100;
271                                 pack2.page3[0]=hr&0xFF;
272                                 pack2.page3[1]=(hr>>8)&0xFF;
273                                 
274                                 while(EECR & (1<<EEPE));
275                                 EECR = (0<<EEPM1)|(0<<EEPM0);
276                                 EEARL = 0;
277                                 EEDR = hr&0xFF;
278                                 EECR |= (1<<EEMPE);
279                                 EECR |= (1<<EEPE);
280                                 while(EECR & (1<<EEPE));
281                                 EECR = (0<<EEPM1)|(0<<EEPM0);
282                                 EEARL = 1;
283                                 EEDR = hr>>8;
284                                 EECR |= (1<<EEMPE);
285                                 EECR |= (1<<EEPE);
286
287                         }
288                 } else reset_R0=0;                      //PORTB|=(1<<PINB1); //Dauer 440ms
289                 if (wdcounter>0) {
290                         if (startup!=0) startup--;
291                         SHT2x_MeasurePoll(HUMIDITY, &sRH);
292                         // --- measure temperature with "Polling Mode" (no hold master) ---
293                         SHT2x_MeasurePoll(TEMP, &sT);
294                         //-- calculate humidity and temperature --
295                         temperatureC = SHT2x_CalcTemperatureC(sT)*10.0;
296                         humidityRH = SHT2x_CalcRH(sRH)*10.0;
297                         double hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
298                         am2302_hum=0.318*hhum +76.0;
299                         am2302_temp=temperatureC*25.6;
300                         //PORTB&=~(1<<PINB1);
301                         PORTB&=~(1<<PINB1);
302                         _delay_us(500);
303                         l=MAX1164x_read();
304                         PORTB|=(1<<PINB1);
305                         if (cmode) {
306                                 l=l*2.048/4096+1.5; //Spannung real
307                         } else {
308                                 l=l*2.048/4096; //Spannung real
309                         }
310                         if (l>1.8) {
311                                 if (cmode==0) {
312                                         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
313                                         cmode=1;
314                                         pack2.page3[4]=cmode;
315                                 }
316                         }
317                         if (l<1.6) {
318                                 if (cmode==1) {
319                                         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
320                                         cmode=0;
321                                         pack2.page3[4]=cmode;
322                                 }
323                                 
324                                 
325                         }
326                         hr=l*10000.0;
327                         pack2.page3[2]=hr&0xFF;
328                         pack2.page3[3]=(hr>>8)&0xFF;
329                         l=( 3/l- 1) *27;
330                         pack2.current=l*100;
331                         float ip=interp(temperatureC,humidityRH);
332                         hr=ip*1000;
333                         pack2.page3[5]=hr&0xFF;
334                         pack2.page3[6]=(hr>>8)&0xFF;
335                         
336                         l=l/ip;
337                         
338                         if (startup==0){
339                         if (l>R0) {
340                                 R0=l;
341                                 hr=R0*100;
342                                 pack2.page3[0]=hr&0xFF;
343                                 pack2.page3[1]=(hr>>8)&0xFF;
344                                 
345                                 while(EECR & (1<<EEPE));
346                                 EECR = (0<<EEPM1)|(0<<EEPM0);
347                                 EEARL = 0;
348                                 EEDR = hr&0xFF;
349                                 EECR |= (1<<EEMPE);
350                                 EECR |= (1<<EEPE);
351                                 while(EECR & (1<<EEPE));
352                                 EECR = (0<<EEPM1)|(0<<EEPM0);
353                                 EEARL = 1;
354                                 EEDR = hr>>8;
355                                 EECR |= (1<<EEMPE);
356                                 EECR |= (1<<EEPE);
357
358                         }
359                         }
360                          l=l/R0;
361                         l=log(1/l);
362                         l=l*160*4; //fuer DS18B20
363                         wdcounter=0;
364                         
365                 }
366         
367                 if (gcontrol==1) {
368                         uint16_t w=l;
369                         uint8_t t8=w>>4;
370                         uint8_t af=0;
371                         if (t8>pack1.TH) af=1;
372                         if (t8<=pack1.TL) af=1; 
373                         cli();
374                         pack1.temp=w;
375                         //pack.temp++;
376                         alarmflag=af;
377                         sei();                  
378                         EXTERN_SLEEP();         
379                 }
380                 if ((gcontrol==2)||(gcontrol==3)) {
381                         gcontrol=0;
382                         
383                 }
384
385                 
386 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
387                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
388 #endif                  
389 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
390                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
391 #endif
392                           {
393
394                         MCUCR|=(1<<SE)|(1<<SM1);
395                         MCUCR&=~(1<<ISC01);
396                 } else {
397                         MCUCR|=(1<<SE);
398                         MCUCR&=~(1<<SM1);
399                 }
400                 asm("SLEEP");
401    }
402
403
404 }