New Devices
[owSlave2.git] / DS2450_VOC_SHT_BMP / DS2450_VOC_SHT_BMP.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 "../common/I2C/USI_TWI_Master.h"
41 #include "../common/I2C/MAX1164x.h"
42 #include "../common/I2C/SHT2x.h"
43 #include "../common/calibr.h"
44 #include "../common/I2C/BMP280.h"
45
46
47
48 extern void OWINIT();
49 extern void EXTERN_SLEEP();
50
51 uint8_t owid[8]={0x20, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x02, 0x5D};/**/
52 uint8_t config_info[16]={0x01,14, 0x04,0x08, 0x08,1, 0x02,16,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
53
54 #if (owid>128) 
55 #error "Variable not correct"
56 #endif
57
58 extern uint8_t mode;
59 extern uint8_t gcontrol;
60 extern uint8_t reset_indicator;
61 extern uint8_t alarmflag;
62 volatile uint8_t wdcounter=1;
63
64
65 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
66 ISR(WATCHDOG_vect) {
67 #else
68 ISR(WDT_vect) {
69 #endif 
70         wdcounter++;
71         if (reset_indicator==1) reset_indicator++;
72         else if (reset_indicator==2) mode=0;
73
74
75 }
76
77 volatile uint16_t _temp;
78 volatile uint16_t _hum;
79
80
81 uint8_t userRegister[1];
82 int16_t sRH,sT;
83 double temperatureC,humidityRH,hhum;
84 double l;
85
86 double R0;
87 uint16_t mr;
88 uint8_t startup=10;
89 double ip;
90
91 uint32_t bmp_P;
92 int32_t bmp_t;
93
94 uint8_t max_adr=0;
95 #define CH0_M MAX1164x_C_SCAN0|MAX1164x_C_SGL
96 #define CH1_M MAX1164x_C_SCAN0|MAX1164x_C_SGL|MAX1164x_C_CS0
97 #define CH0_CH1 MAX1164x_C_SCAN0
98 //|MAX1164x_C_CS0
99
100 uint16_t weekmaxarr[8];
101
102
103 typedef union {
104         volatile uint8_t bytes[0x20];
105         struct {
106                 //Page0
107                 uint16_t A;  //0
108                 uint16_t B;  //2
109                 uint16_t C;  //4
110                 uint16_t D;  //6
111                 //Page1
112                 uint8_t CSA1;
113                 uint8_t CSA2;
114                 uint8_t CSB1;
115                 uint8_t CSB2;
116                 uint8_t CSC1;
117                 uint8_t CSC2;
118                 uint8_t CSD1;
119                 uint8_t CSD2;
120                 //Page2
121                 uint8_t LA;
122                 uint8_t HA;
123                 uint8_t LB;
124                 uint8_t HB;
125                 uint8_t LC;
126                 uint8_t HC;
127                 uint8_t LD;
128                 uint8_t HD;
129                 //Page3
130                 uint8_t FC1;
131                 uint8_t FC2;
132                 uint8_t FC3;
133                 uint8_t FC4;
134                 uint8_t VCCP;
135                 uint8_t FC5;
136                 uint8_t FC6;
137                 uint8_t FC7;
138                 uint8_t convc1;
139                 uint8_t convc2;
140                 
141                 
142         };
143 } pack_t;
144 volatile pack_t pack;
145
146
147 typedef struct {
148         int16_t current;  //6
149         uint16_t R0;
150         //uint16_t VS;
151         uint8_t cmode;
152         int16_t ip;
153         //uint8_t free;
154         uint16_t tol_s8;
155         uint16_t tol_d;
156         uint16_t r_day_max;
157         uint16_t r_week_max;
158 } pack2_t;
159 volatile pack2_t pack2;
160
161
162 //Kompensieren der Abhänigkeit von RS/RO von Temperatur und Luftfeuchte
163 inline double interp(double t, double h) {
164         double h2;
165          double t2;
166         h2=h*h;
167         t2=t*t;
168         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;
169 }
170
171 #define EEPROM_R0 0
172 #define EEPROM_R0d 2
173 #define EEPROM_R0w 4
174 #define EEPROM_dol 6
175
176
177 uint16_t readEEPROM(uint8_t addr,uint16_t def) {
178         uint16_t hr;
179         while(EECR & (1<<EEPE));
180         EEARL=addr+1;
181         EECR |= (1<<EERE);
182         hr=EEDR;
183         if (hr!=0xFF) {
184                 hr=hr<<8;
185                 while(EECR & (1<<EEPE));
186                 EEARL=addr;
187                 EECR |= (1<<EERE);
188                 hr|=EEDR;
189                 return hr;
190         }
191         return def;
192 }
193
194 void writeEEPROM(uint8_t addr,uint16_t val) {
195         while(EECR & (1<<EEPE));
196         EECR = (0<<EEPM1)|(0<<EEPM0);
197         EEARL = addr;
198         EEDR = val&0xFF;
199         EECR |= (1<<EEMPE);
200         EECR |= (1<<EEPE);
201         while(EECR & (1<<EEPE));
202         EECR = (0<<EEPM1)|(0<<EEPM0);
203         EEARL = addr+1;
204         EEDR = val>>8;
205         EECR |= (1<<EEMPE);
206         EECR |= (1<<EEPE);
207 }
208
209
210
211
212
213 int main(void){
214         PRR|=(1<<PRADC);  // adc for save Power
215         PORTA=0xFF;
216         PORTB=0xFF-(1<<PORTB0); //Schalter kann gegen Masse sein und zieht dann immer Strom
217         DDRB|=(1<<PORTB0); //Als Ausgang und 0
218         OWINIT();
219         DDRB|=(1<<PINB1);//Ausgang und 1
220         DDRA|=(1<<PINA0);
221         
222         WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)
223         WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt
224         //(1<<WDP0) |(1<<WDP2) | (1<<WDP1);    // Set Timeout to ~2 seconds
225         (1<<WDP3) | (1<<WDP0);    // Set Timeout to ~8 seconds
226
227         MCUSR=0;
228         pack.A=0;
229         pack.B=0;
230         pack.C=0;
231         pack.D=0;
232         pack.CSA1=0x08;
233         pack.CSA2=0x8C;
234         pack.CSB1=0x08;
235         pack.CSB2=0x8C;
236         pack.CSC1=0x08;
237         pack.CSC2=0x8C;
238         pack.CSD1=0x08;
239         pack.CSD2=0x8C;
240         pack.HA=0xFF;
241         pack.LA=0x00;
242         pack.HB=0xFF;
243         pack.LB=0x00;
244         pack.HC=0xFF;
245         pack.LC=0x00;
246         pack.HD=0xFF;
247         pack.LD=0x00;
248         pack.VCCP=0;
249
250         pack2.cmode=0;
251         pack2.R0=readEEPROM(EEPROM_R0,1);
252         R0=pack2.R0/100.0;
253                 
254         pack2.r_day_max=readEEPROM(EEPROM_R0d,1);
255         pack2.r_week_max=readEEPROM(EEPROM_R0w,1);
256         pack2.tol_d=readEEPROM(EEPROM_dol,0); 
257         pack2.tol_s8=0;  //Tag faengt mit Einschalten an
258         for(uint8_t i=0;i<7;i++) {
259                 weekmaxarr[i]=pack2.r_week_max;
260         }
261
262         USI_TWI_Master_Initialise();
263         bmp280Init();
264         SHT2x_SoftReset();
265         SHT2x_ReadUserRegister(userRegister);
266         SHT2x_WriteUserRegister(userRegister); //write changed user reg
267         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);//#define MAX1164x_C_CS0
268         _delay_ms(30); //Internal Referenz start
269         //2970 -> 1,5V  
270         gcontrol=1;
271         sei();
272         
273
274     while(1)   {
275
276                 /*if (pack1.config==0x1F) {
277                         pack1.config=0x7F;
278                         R0=1;
279                         pack2.R0=0;
280                         writeEEPROM(EEPROM_R0,0);
281                 } 
282                 if (pack1.config==0x05) {
283                         pack1.config=0x7F;                              
284                         pack2.r_day_max=1;
285                         pack2.r_week_max=1;
286                         pack2.tol_d=0;
287                         pack2.tol_s8=0;  //Tag faengt mit Einschalten an        
288                         R0=1;   
289                         writeEEPROM(EEPROM_R0,0xFF);
290                         writeEEPROM(EEPROM_R0d,0xFF);  //Maximum des Tages
291                         writeEEPROM(EEPROM_R0w,0xFF); //Maximum der Letzten 7 Tage
292                         writeEEPROM(EEPROM_dol,0xFF); //Anzahl der Betriebstage         
293                         for(uint8_t i=0;i<7;i++) {
294                                 weekmaxarr[i]=1;
295                         }                               
296                 } */
297                 if (wdcounter>0) {
298                         pack2.tol_s8++;
299                         if (pack2.tol_s8>(10000))  {//10800 ist theortisch der Tag aber meistens zu lang
300                                 pack2.tol_s8=0;
301                                 pack2.tol_d++;  //rund 180 Jahre :-)
302                                 pack2.r_week_max=0;
303                                 weekmaxarr[7]=pack2.r_day_max;
304                                 for(uint8_t i=0;i<7;i++) {
305                                         weekmaxarr[i]=weekmaxarr[i+1];
306                                         if (weekmaxarr[i]>pack2.r_week_max) pack2.r_week_max=weekmaxarr[i];
307                                 }
308                                 if (pack2.tol_d>7) {
309                                         pack2.R0=pack2.r_week_max;
310                                 } else {
311                                         pack2.R0=pack2.r_day_max;
312                                 }
313                                 //R0=pack2.R0/100.0-0.5*(pack2.R0/100.0-R0);
314                                 R0=R0-(R0-pack2.R0/100.0)*0.5   ;
315                                 pack2.R0=R0*100;
316                                 writeEEPROM(EEPROM_R0,pack2.R0);
317                                 writeEEPROM(EEPROM_R0d,pack2.r_day_max);  //Maximum des Tages
318                                 writeEEPROM(EEPROM_R0w,pack2.r_week_max); //Maximum der Letzten 7 Tage
319                                 writeEEPROM(EEPROM_dol,pack2.tol_d); //Anzahl der Betriebstage
320                                 pack2.r_day_max=0;
321                         }
322                         if (startup!=0) startup--;
323                         SHT2x_MeasurePoll(HUMIDITY, &sRH);
324                         // --- measure temperature with "Polling Mode" (no hold master) ---
325                         SHT2x_MeasurePoll(TEMP, &sT);
326                         //-- calculate humidity and temperature --
327                         temperatureC = SHT2x_CalcTemperatureC(sT);
328                         humidityRH = SHT2x_CalcRH(sRH);
329                         ip=interp(temperatureC,humidityRH);
330                         pack2.ip=ip*1000;
331                         //humidityRH=calibr_hum(temperatureC,-0.45,humidityRH);
332                         //temperatureC =temperatureC -0.45;
333                         _hum= humidityRH*100.0;
334                         _temp=(temperatureC*100.0)+32767;
335                         //PORTB&=~(1<<PINB1);
336                         mr=0;
337 //Kritische Sektion !___Ein Breakpoint in dieser Section kann den TGS8100 zerstoeren!___________________________
338                         PORTB&=~(1<<PINB1); //Auf 0 Ziehen
339                         _delay_us(150);
340                         mr+=MAX1164x_read();
341                         _delay_us(150);
342                         mr+=MAX1164x_read();
343                         PORTB|=(1<<PINB1);
344 //ENDE Kritische Sektion !______________________________
345                         //l=mr/2.0*2.048/4096; 
346                         // l maximal 2  mr max 4096  //mr 2V=8000 
347                         
348                         if (pack2.cmode) { //cmode=0 V 0..2 V cmode=1 V 1.5..3.5V
349                                 //l+=1.5; //Spannung real
350                                 mr+=6000;
351                         } 
352                         //if (l>1.8) {
353                         if (mr>7200) {                          
354                                 if (pack2.cmode==0) {
355                                         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
356                                         pack2.cmode=1;
357                                 }
358                         }
359                         //if (l<1.6) {
360                         if (mr<6400) {  
361                                 if (pack2.cmode==1) {
362                                         MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
363                                         pack2.cmode=0;
364                                 }
365                                 
366                                 
367                         }
368                         //pack2.VS=mr*5/2;
369                         l=mr/4000.0;
370                         l=( 3/l- 1) *30;
371                         pack2.current=l*100;
372                         
373                         l=l/ip;
374                         
375                         if (startup==0){
376                                 if (l>R0) {
377                                         R0=l;
378                                         pack2.R0=R0*100;
379                                         writeEEPROM(EEPROM_R0,pack2.R0);
380
381                                 }
382                                 if (l*100>pack2.r_day_max) {
383                                         pack2.r_day_max=l*100;
384                                 }
385                         } else if (l<R0) l=R0; //negative Werte am Anfang verhintern
386                         l=R0/l;
387                         l=log(l);
388                         l=l*160*4; //fuer DS18B20
389                         bmp280ConvertIntP1(&bmp_P);
390                         bmp_P=bmp_P/3;    // /100.0*32.0;
391                         wdcounter=0;
392                         
393                 }
394
395                 if (gcontrol) {
396                         //PORTB|=(1<<PINB1);
397                         uint8_t bb=1;
398                         uint8_t bb1=1;
399                         for(uint8_t i=0;i<4;i++){
400                                 if (pack.convc1&bb1) {
401                                         if (pack.convc2&(bb)) {pack.bytes[i*2]=0;pack.bytes[i*2+1]=0;}
402                                         bb=bb<<1;
403                                         if (pack.convc2&(bb)) {pack.bytes[i*2]=0xFF;pack.bytes[i*2+1]=0xFF;}
404                                         bb=bb<<1;
405                                 } else bb=bb<<2;
406                                 bb1=bb1<<1;                             
407                         }
408                         //CHanel A
409                         if (pack.convc1&1) {
410                                 cli();pack.A=_temp;sei();
411                                 alarmflag=0;
412                                 if (pack.CSA2&0x08)  //AEH
413                                         if (pack.bytes[1]>pack.HA) {alarmflag=1;pack.CSA2|=0x20;}
414                                 if (pack.CSA2&0x04)  //AEL
415                                         if (pack.bytes[1]<pack.LA) {alarmflag=1;pack.CSA2|=0x10;}
416                         }
417
418                         if (pack.convc1&2) {
419                                 cli();pack.B=_hum;sei();
420                                 if (pack.CSB2&0x08)  //AEH
421                                         if (pack.bytes[1]>pack.HB) {alarmflag=1;pack.CSB2|=0x20;}
422                                 if (pack.CSB2&0x04)  //AEL
423                                         if (pack.bytes[1]<pack.LB) {alarmflag=1;pack.CSB2|=0x10;}
424                         }
425
426                         if (pack.convc1&4) {
427                                 cli();pack.C=l;sei();
428                                 if (pack.CSC2&0x08)  //AEH
429                                         if (pack.bytes[1]>pack.HC) {alarmflag=1;pack.CSC2|=0x20;}
430                                 if (pack.CSC2&0x04)  //AEL
431                                         if (pack.bytes[1]<pack.LC) {alarmflag=1;pack.CSC2|=0x10;}
432                         } 
433                         if (pack.convc1&8) {
434                                 cli();pack.D=bmp_P;sei();
435                                 if (pack.CSD2&0x08)  //AEH
436                                         if (pack.bytes[1]>pack.HD) {alarmflag=1;pack.CSD2|=0x20;}
437                                 if (pack.CSD2&0x04)  //AEL
438                                         if (pack.bytes[1]<pack.LD) {alarmflag=1;pack.CSD2|=0x10;}
439                         }
440                         
441                         EXTERN_SLEEP();
442                         //PORTB&=~(1<<PINB1);
443                 }
444
445                 
446 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
447                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
448 #endif                  
449 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
450                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
451 #endif
452                           {
453
454                         MCUCR|=(1<<SE)|(1<<SM1);
455                         MCUCR&=~(1<<ISC01);
456                 } else {
457                         MCUCR|=(1<<SE);
458                         MCUCR&=~(1<<SM1);
459                 }
460         //      asm("SLEEP");
461    }
462
463
464 }