2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
34 #define F_CPU 8000000UL
36 #include <avr/interrupt.h>
37 #include <util/delay.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"
49 extern void EXTERN_SLEEP();
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};
55 #error "Variable not correct"
59 extern uint8_t gcontrol;
60 extern uint8_t reset_indicator;
61 extern uint8_t alarmflag;
62 volatile uint8_t wdcounter=1;
65 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
71 if (reset_indicator==1) reset_indicator++;
72 else if (reset_indicator==2) mode=0;
77 volatile uint16_t _temp;
78 volatile uint16_t _hum;
81 uint8_t userRegister[1];
83 double temperatureC,humidityRH,hhum;
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
100 uint16_t weekmaxarr[8];
104 volatile uint8_t bytes[0x20];
144 volatile pack_t pack;
159 volatile pack2_t pack2;
162 //Kompensieren der Abhänigkeit von RS/RO von Temperatur und Luftfeuchte
163 inline double interp(double t, double h) {
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;
177 uint16_t readEEPROM(uint8_t addr,uint16_t def) {
179 while(EECR & (1<<EEPE));
185 while(EECR & (1<<EEPE));
194 void writeEEPROM(uint8_t addr,uint16_t val) {
195 while(EECR & (1<<EEPE));
196 EECR = (0<<EEPM1)|(0<<EEPM0);
201 while(EECR & (1<<EEPE));
202 EECR = (0<<EEPM1)|(0<<EEPM0);
214 PRR|=(1<<PRADC); // adc for save Power
216 PORTB=0xFF-(1<<PORTB0); //Schalter kann gegen Masse sein und zieht dann immer Strom
217 DDRB|=(1<<PORTB0); //Als Ausgang und 0
219 DDRB|=(1<<PINB1);//Ausgang und 1
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
251 pack2.R0=readEEPROM(EEPROM_R0,1);
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;
262 USI_TWI_Master_Initialise();
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
276 /*if (pack1.config==0x1F) {
280 writeEEPROM(EEPROM_R0,0);
282 if (pack1.config==0x05) {
287 pack2.tol_s8=0; //Tag faengt mit Einschalten an
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++) {
299 if (pack2.tol_s8>(10000)) {//10800 ist theortisch der Tag aber meistens zu lang
301 pack2.tol_d++; //rund 180 Jahre :-)
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];
309 pack2.R0=pack2.r_week_max;
311 pack2.R0=pack2.r_day_max;
313 //R0=pack2.R0/100.0-0.5*(pack2.R0/100.0-R0);
314 R0=R0-(R0-pack2.R0/100.0)*0.5 ;
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
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);
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);
337 //Kritische Sektion !___Ein Breakpoint in dieser Section kann den TGS8100 zerstoeren!___________________________
338 PORTB&=~(1<<PINB1); //Auf 0 Ziehen
344 //ENDE Kritische Sektion !______________________________
345 //l=mr/2.0*2.048/4096;
346 // l maximal 2 mr max 4096 //mr 2V=8000
348 if (pack2.cmode) { //cmode=0 V 0..2 V cmode=1 V 1.5..3.5V
349 //l+=1.5; //Spannung real
354 if (pack2.cmode==0) {
355 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
361 if (pack2.cmode==1) {
362 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
379 writeEEPROM(EEPROM_R0,pack2.R0);
382 if (l*100>pack2.r_day_max) {
383 pack2.r_day_max=l*100;
385 } else if (l<R0) l=R0; //negative Werte am Anfang verhintern
388 l=l*160*4; //fuer DS18B20
389 bmp280ConvertIntP1(&bmp_P);
390 bmp_P=bmp_P/3; // /100.0*32.0;
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;}
403 if (pack.convc2&(bb)) {pack.bytes[i*2]=0xFF;pack.bytes[i*2+1]=0xFF;}
410 cli();pack.A=_temp;sei();
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;}
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;}
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;}
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;}
442 //PORTB&=~(1<<PINB1);
446 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
447 if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
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))
454 MCUCR|=(1<<SE)|(1<<SM1);