2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
\r
3 // All rights reserved.
\r
5 // Redistribution and use in source and binary forms, with or without
\r
6 // modification, are permitted provided that the following conditions are
\r
9 // * Redistributions of source code must retain the above copyright
\r
10 // notice, this list of conditions and the following disclaimer.
\r
11 // * Redistributions in binary form must reproduce the above copyright
\r
12 // notice, this list of conditions and the following disclaimer in the
\r
13 // documentation and/or other materials provided with the
\r
15 // * All advertising materials mentioning features or use of this
\r
16 // software must display the following acknowledgement: This product
\r
17 // includes software developed by tm3d.de and its contributors.
\r
18 // * Neither the name of tm3d.de nor the names of its contributors may
\r
19 // be used to endorse or promote products derived from this software
\r
20 // without specific prior written permission.
\r
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
\r
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
\r
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
34 //!!!!!Max Program size 7551 Byte
\r
36 #define F_CPU 8000000UL
\r
38 #include <avr/interrupt.h>
\r
39 #include <util/delay.h>
\r
40 #include <avr/wdt.h>
\r
41 #include <avr/sleep.h>
\r
42 #include <avr/pgmspace.h>
\r
43 #include "../common/I2C/USI_TWI_Master.h"
\r
44 #include "../common/I2C/MAX1164x.h"
\r
45 #include "../common/I2C/SHT2x.h"
\r
46 #include "../common/calibr.h"
\r
48 extern void OWINIT(void);
\r
49 extern void EXTERN_SLEEP(void);
\r
53 volatile uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/
\r
54 volatile uint8_t owid2[8]={0x26, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x67};/**/
\r
55 volatile uint8_t config_info1[16]={0x08,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
\r
56 volatile uint8_t config_info2[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x07,0x08, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
\r
59 #error "Variable not correct"
\r
62 extern uint8_t mode;
\r
63 extern uint8_t gcontrol;
\r
64 extern uint8_t reset_indicator;
\r
65 extern uint8_t alarmflag;
\r
66 volatile uint8_t wdcounter=1;
\r
69 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
\r
70 ISR(WATCHDOG_vect) {
\r
75 if (reset_indicator==1) reset_indicator++;
\r
76 else if (reset_indicator==2) mode=0;
\r
83 volatile uint8_t bytes[8];
\r
94 volatile pack1_t pack1;
\r
99 #if defined(__AVR_ATtiny25__)
\r
100 volatile uint8_t bytes[16];
\r
102 volatile uint8_t bytes[64];
\r
105 uint8_t status; //1
\r
107 uint16_t voltage; //4
\r
108 int16_t current; //6
\r
109 uint8_t threshold; //8
\r
111 uint8_t page1[8]; //9
\r
112 #if defined(__AVR_ATtiny25__)
\r
114 uint8_t page2[8]; //17
\r
116 uint8_t page3[8]; //25
\r
126 uint8_t page4[8]; //33
\r
130 uint16_t r_day_max;
\r
131 uint16_t r_week_max;
\r
134 uint8_t page5[8]; //41
\r
135 uint8_t page6[8]; //49
\r
136 uint8_t page7[8]; //57
\r
141 volatile pack2_t pack2;
\r
146 volatile int16_t am2302_temp;
\r
147 volatile uint16_t am2302_hum;
\r
150 uint8_t userRegister[1];
\r
152 double temperatureC,humidityRH,hhum;
\r
156 #define CH0_M MAX1164x_C_SCAN0|MAX1164x_C_SGL
\r
157 #define CH1_M MAX1164x_C_SCAN0|MAX1164x_C_SGL|MAX1164x_C_CS0
\r
158 #define CH0_CH1 MAX1164x_C_SCAN0
\r
161 uint16_t weekmaxarr[8];
\r
163 //Kompensieren der Abhänigkeit von RS/RO von Temperatur und Luftfeuchte
\r
164 inline double interp(double t, double h) {
\r
169 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;
\r
174 uint8_t startup=10;
\r
180 DDRB&=~(1<<PORTB0); //Eingang
\r
181 __asm__ __volatile__ ("nop");
\r
182 PORTB|=(1<<PORTB0); //Pullup
\r
183 __asm__ __volatile__ ("nop");
\r
184 __asm__ __volatile__ ("nop");
\r
185 __asm__ __volatile__ ("nop");
\r
186 __asm__ __volatile__ ("nop");
\r
187 __asm__ __volatile__ ("nop");
\r
188 r=PINB&(1<<PORTB0);
\r
189 __asm__ __volatile__ ("nop");
\r
190 PORTB&=~(1<<PORTB0);
\r
191 __asm__ __volatile__ ("nop");
\r
192 DDRB|=(1<<PORTB0); //Eingang
\r
193 return (r==0); //Offen mal HIH4030
\r
198 #define EEPROM_R0 0
\r
199 #define EEPROM_R0d 2
\r
200 #define EEPROM_R0w 4
\r
201 #define EEPROM_dol 6
\r
204 uint16_t readEEPROM(uint8_t addr,uint16_t def) {
\r
206 while(EECR & (1<<EEPE));
\r
212 while(EECR & (1<<EEPE));
\r
221 void writeEEPROM(uint8_t addr,uint16_t val) {
\r
222 while(EECR & (1<<EEPE));
\r
223 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
226 EECR |= (1<<EEMPE);
\r
228 while(EECR & (1<<EEPE));
\r
229 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
232 EECR |= (1<<EEMPE);
\r
237 PRR|=(1<<PRADC); // adc for save Power
\r
239 PORTB=0xFF-(1<<PORTB0); //Schalter kann gegen Masse sein und zieht dann immer Strom
\r
240 DDRB|=(1<<PORTB0); //Als Ausgang und 0
\r
242 DDRB|=(1<<PINB1);//Ausgang und 1
\r
245 WDTCSR |= ((1<<WDCE) ); // Enable the WD Change Bit//| (1<<WDE)
\r
246 WDTCSR |= (1<<WDIE) | // Enable WDT Interrupt
\r
247 //(1<<WDP0) |(1<<WDP2) | (1<<WDP1); // Set Timeout to ~2 seconds
\r
248 (1<<WDP3) | (1<<WDP0); // Set Timeout to ~8 seconds
\r
261 pack2.R0=readEEPROM(EEPROM_R0,1);
\r
264 pack2.r_day_max=readEEPROM(EEPROM_R0d,1);
\r
265 pack2.r_week_max=readEEPROM(EEPROM_R0w,1);
\r
266 pack2.tol_d=readEEPROM(EEPROM_dol,0);
\r
267 pack2.tol_s8=0; //Tag faengt mit Einschalten an
\r
268 for(uint8_t i=0;i<7;i++) {
\r
269 weekmaxarr[i]=pack2.r_week_max;
\r
278 USI_TWI_Master_Initialise();
\r
281 SHT2x_ReadUserRegister(userRegister);
\r
282 SHT2x_WriteUserRegister(userRegister); //write changed user reg
\r
283 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);//#define MAX1164x_C_CS0
\r
284 _delay_ms(30); //Internal Referenz start
\r
288 //DDRB|=(1<<PINB1);
\r
290 if (pack1.config==0x1F) {
\r
294 writeEEPROM(EEPROM_R0,0);
\r
296 if (pack1.config==0x05) {
\r
297 pack1.config=0x7F;
\r
299 pack2.r_week_max=1;
\r
301 pack2.tol_s8=0; //Tag faengt mit Einschalten an
\r
303 writeEEPROM(EEPROM_R0,0xFF);
\r
304 writeEEPROM(EEPROM_R0d,0xFF); //Maximum des Tages
\r
305 writeEEPROM(EEPROM_R0w,0xFF); //Maximum der Letzten 7 Tage
\r
306 writeEEPROM(EEPROM_dol,0xFF); //Anzahl der Betriebstage
\r
307 for(uint8_t i=0;i<7;i++) {
\r
313 if (pack2.tol_s8>(10000)) {//10800 ist theortisch der Tag aber meistens zu lang
\r
315 pack2.tol_d++; //rund 180 Jahre :-)
\r
316 pack2.r_week_max=0;
\r
317 weekmaxarr[7]=pack2.r_day_max;
\r
318 for(uint8_t i=0;i<7;i++) {
\r
319 weekmaxarr[i]=weekmaxarr[i+1];
\r
321 if (weekmaxarr[i]>pack2.r_week_max) pack2.r_week_max=weekmaxarr[i];
\r
323 if (pack2.tol_d>7) {
\r
324 pack2.R0=pack2.r_week_max;
\r
326 pack2.R0=pack2.r_day_max;
\r
328 //R0=//R0-0.5*(pack2.R0/100-R0);
\r
329 R0=R0-(R0-pack2.R0/100.0)*0.5 ;
\r
331 writeEEPROM(EEPROM_R0,pack2.R0);
\r
332 writeEEPROM(EEPROM_R0d,pack2.r_day_max); //Maximum des Tages
\r
333 writeEEPROM(EEPROM_R0w,pack2.r_week_max); //Maximum der Letzten 7 Tage
\r
334 writeEEPROM(EEPROM_dol,pack2.tol_d); //Anzahl der Betriebstage
\r
337 if (startup!=0) startup--;
\r
338 SHT2x_MeasurePoll(HUMIDITY, &sRH);
\r
339 // --- measure temperature with "Polling Mode" (no hold master) ---
\r
340 SHT2x_MeasurePoll(TEMP, &sT);
\r
341 //-- calculate humidity and temperature --
\r
342 temperatureC = SHT2x_CalcTemperatureC(sT);
\r
343 humidityRH = SHT2x_CalcRH(sRH);
\r
344 ip=interp(temperatureC,humidityRH);
\r
346 humidityRH=calibr_hum(temperatureC,-0.2,humidityRH)*10.0;
\r
347 //humidityRH=humidityRH*10.0;
\r
348 temperatureC =temperatureC *10.0-2;
\r
352 am2302_hum= humidityRH*10.0;
\r
353 am2302_temp=temperatureC*25.6;
\r
354 //am2302_temp=am2302_temp-45;
\r
355 config_info2[5]=8;
\r
357 hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
\r
358 //am2302_hum=0.318*hhum +76.0;
\r
359 am2302_hum=0.31*hhum +80;
\r
360 am2302_temp=temperatureC*25.6;
\r
363 //PORTB&=~(1<<PINB1);
\r
365 //Kritische Sektion !___Ein Breakpoint in dieser Section kann den TGS8100 zerstoeren!___________________________
\r
366 PORTB&=~(1<<PINB1); //Auf 0 Ziehen
\r
368 mr+=MAX1164x_read();
\r
370 mr+=MAX1164x_read();
\r
372 //ENDE Kritische Sektion !______________________________
\r
373 //l=mr/2.0*2.048/4096;
\r
374 // l maximal 2 mr max 4096 //mr 2V=8000
\r
376 if (pack2.cmode) { //cmode=0 V 0..2 V cmode=1 V 1.5..3.5V
\r
377 //l+=1.5; //Spannung real
\r
382 if (pack2.cmode==0) {
\r
383 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
\r
389 if (pack2.cmode==1) {
\r
390 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
\r
399 pack2.current=l*100;
\r
407 writeEEPROM(EEPROM_R0,pack2.R0);
\r
410 if (l*100>pack2.r_day_max) {
\r
411 pack2.r_day_max=l*100;
\r
413 } else if (l<R0) l=R0; //negative Werte am Anfang verhintern
\r
416 l=l*160*4; //fuer DS18B20
\r
425 if (t8>pack1.TH) af=1;
\r
426 if (t8<=pack1.TL) af=1;
\r
434 if ((gcontrol==2)||(gcontrol==3)) {
\r
440 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
\r
441 if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
\r
443 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
\r
444 if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
\r
448 MCUCR|=(1<<SE)|(1<<SM1);
\r
449 MCUCR&=~(1<<ISC01);
\r