d455f89528666bad8d861f9d60cc30a476690118
[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
194 int main(void){
195     PRR|=(1<<PRADC);  // adc for save Power
196         pack1.temp=0x0550;
197         pack1.config=0x7F;
198         pack1.TH=75;
199         pack1.TL=70;
200         pack1.rrFF=0xFF;
201         pack1.rr00=0;
202         pack1.rr10=0x10;
203         PORTA=0xFF;
204         PORTB=0xFF;
205         OWINIT();
206         PORTB|=(1<<PINB1);
207         DDRB|=(1<<PINB1);
208         PORTA|=(1<<PINA0);
209         DDRA|=(1<<PINA0);
210         
211         MCUCR &=~(1<<PUD); //All Pins Pullup...
212         MCUCR |=(1<<BODS);
213         
214         while(EECR & (1<<EEPE));
215         EEARL=1;
216         EECR |= (1<<EERE);
217         hr=EEDR;
218         if (hr!=0xFF) {
219                 hr=hr<<8;
220                 while(EECR & (1<<EEPE));
221                 EEARL=0;
222                 EECR |= (1<<EERE);
223                 hr|=EEDR;
224                 R0=hr/100.0;
225                 pack2.page3[0]=hr&0xFF;
226                 pack2.page3[1]=(hr>>8)&0xFF;
227         }
228
229         
230         /*volatile double test=interp(0,800);
231         test=interp(100,900);
232         test=interp(110,400);
233         test=interp(300,200);
234         test=interp(300,210);
235         test=interp(280,600);
236         test=interp(100,900);
237         */
238
239         WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)
240         WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt
241         (1<<WDP3) | (1<<WDP0);    // Set Timeout to ~8 seconds
242
243         MCUSR=0;
244         USI_TWI_Master_Initialise();
245         
246         SHT2x_SoftReset();
247         SHT2x_ReadUserRegister(userRegister);
248         //(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;
249         SHT2x_WriteUserRegister(userRegister); //write changed user reg
250         // --- measure humidity with "Hold Master Mode (HM)" ---
251         SHT2x_MeasurePoll(HUMIDITY, &sRH);
252         // --- measure temperature with "Polling Mode" (no hold master) ---
253         SHT2x_MeasurePoll(TEMP, &sT);
254         //-- calculate humidity and temperature --
255         temperatureC = SHT2x_CalcTemperatureC(sT);
256         humidityRH = SHT2x_CalcRH(sRH);
257         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);//#define MAX1164x_C_CS0
258         _delay_ms(30); //Internal Referenz start
259         //2970 -> 1,5V  
260         gcontrol=1;
261         sei();
262         //DDRB|=(1<<PINB1);
263     while(1)   {
264                 if (wdcounter>0) {
265                         if (startup!=0) startup--;
266                         //PORTB|=(1<<PINB1); //Dauer 440ms
267                         SHT2x_MeasurePoll(HUMIDITY, &sRH);
268                         // --- measure temperature with "Polling Mode" (no hold master) ---
269                         SHT2x_MeasurePoll(TEMP, &sT);
270                         //-- calculate humidity and temperature --
271                         temperatureC = SHT2x_CalcTemperatureC(sT)*10.0;
272                         humidityRH = SHT2x_CalcRH(sRH)*10.0;
273                         double hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
274                         am2302_hum=0.318*hhum +76.0;
275                         am2302_temp=temperatureC*25.6;
276                         //PORTB&=~(1<<PINB1);
277                         PORTB&=~(1<<PINB1);
278                         _delay_us(500);
279                         l=MAX1164x_read();
280                         PORTB|=(1<<PINB1);
281                         if (cmode) {
282                                 l=l*2.048/4096+1.5; //Spannung real
283                         } else {
284                                 l=l*2.048/4096; //Spannung real
285                         }
286                         if (l>1.8) {
287                                 if (cmode==0) {
288                                         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
289                                         cmode=1;
290                                         pack2.page3[4]=cmode;
291                                 }
292                         }
293                         if (l<1.6) {
294                                 if (cmode==1) {
295                                         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
296                                         cmode=0;
297                                         pack2.page3[4]=cmode;
298                                 }
299                                 
300                                 
301                         }
302                         hr=l*10000.0;
303                         pack2.page3[2]=hr&0xFF;
304                         pack2.page3[3]=(hr>>8)&0xFF;
305                         l=( 3/l- 1) *27;
306                         pack2.current=l*100;
307                         float ip=interp(temperatureC,humidityRH);
308                         hr=ip*1000;
309                         pack2.page3[5]=hr&0xFF;
310                         pack2.page3[6]=(hr>>8)&0xFF;
311                         
312                         l=l/ip;
313                         if (startup!=0){
314                         if (l>R0) {
315                                 R0=l;
316                                 hr=R0*100;
317                                 pack2.page3[0]=hr&0xFF;
318                                 pack2.page3[1]=(hr>>8)&0xFF;
319                                 
320                                 while(EECR & (1<<EEPE));
321                                 EECR = (0<<EEPM1)|(0<<EEPM0);
322                                 EEARL = 0;
323                                 EEDR = hr&0xFF;
324                                 EECR |= (1<<EEMPE);
325                                 EECR |= (1<<EEPE);
326                                 while(EECR & (1<<EEPE));
327                                 EECR = (0<<EEPM1)|(0<<EEPM0);
328                                 EEARL = 1;
329                                 EEDR = hr>>8;
330                                 EECR |= (1<<EEMPE);
331                                 EECR |= (1<<EEPE);
332
333                         }
334                         }
335                          l=l/R0;
336                         l=log(1/l);
337                         l=l*160; //fuer DS18B20
338                         wdcounter=0;
339                         
340                 }
341         
342                 if (gcontrol==1) {
343                         uint16_t w=l;
344                         uint8_t t8=w>>4;
345                         uint8_t af=0;
346                         if (t8>pack1.TH) af=1;
347                         if (t8<=pack1.TL) af=1; 
348                         cli();
349                         pack1.temp=w;
350                         //pack.temp++;
351                         alarmflag=af;
352                         sei();                  
353                         EXTERN_SLEEP();         
354                 }
355                 if ((gcontrol==2)||(gcontrol==3)) {
356                         gcontrol=0;
357                         
358                 }
359
360                 
361 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
362                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
363 #endif                  
364 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
365                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
366 #endif
367                           {
368
369                         MCUCR|=(1<<SE)|(1<<SM1);
370                         MCUCR&=~(1<<ISC01);
371                 } else {
372                         MCUCR|=(1<<SE);
373                         MCUCR&=~(1<<SM1);
374                 }
375                 asm("SLEEP");
376    }
377
378
379 }