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 #define F_CPU 8000000UL
\r
36 #include <avr/interrupt.h>
\r
37 #include <util/delay.h>
\r
38 #include <avr/wdt.h>
\r
39 #include <avr/sleep.h>
\r
40 #include <avr/pgmspace.h>
\r
41 #include "../common/I2C/USI_TWI_Master.h"
\r
42 #include "../common/I2C/MAX1164x.h"
\r
43 #include "../common/I2C/SHT2x.h"
\r
44 #include "../common/calibr.h"
\r
46 extern void OWINIT(void);
\r
47 extern void EXTERN_SLEEP(void);
\r
51 volatile uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/
\r
52 volatile uint8_t owid2[8]={0x26, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x67};/**/
\r
53 volatile uint8_t config_info1[16]={0x08,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
\r
54 volatile uint8_t config_info2[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x07,0x08, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
\r
57 #error "Variable not correct"
\r
60 extern uint8_t mode;
\r
61 extern uint8_t gcontrol;
\r
62 extern uint8_t reset_indicator;
\r
63 extern uint8_t alarmflag;
\r
64 volatile uint8_t wdcounter=1;
\r
67 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
\r
68 ISR(WATCHDOG_vect) {
\r
73 if (reset_indicator==1) reset_indicator++;
\r
74 else if (reset_indicator==2) mode=0;
\r
81 volatile uint8_t bytes[8];
\r
92 volatile pack1_t pack1;
\r
97 #if defined(__AVR_ATtiny25__)
\r
98 volatile uint8_t bytes[16];
\r
100 volatile uint8_t bytes[64];
\r
103 uint8_t status; //1
\r
105 uint16_t voltage; //4
\r
106 int16_t current; //6
\r
107 uint8_t threshold; //8
\r
109 uint8_t page1[8]; //9
\r
110 #if defined(__AVR_ATtiny25__)
\r
112 uint8_t page2[8]; //17
\r
114 uint8_t page3[8]; //25
\r
124 uint8_t page4[8]; //33
\r
128 uint16_t r_day_max;
\r
129 uint16_t r_week_max;
\r
132 uint8_t page5[8]; //41
\r
133 uint8_t page6[8]; //49
\r
134 uint8_t page7[8]; //57
\r
135 //uint8_t crc; //65
\r
139 volatile pack2_t pack2;
\r
144 volatile int16_t am2302_temp;
\r
145 volatile uint16_t am2302_hum;
\r
148 uint8_t userRegister[1];
\r
150 double temperatureC,humidityRH,hhum;
\r
154 #define CH0_M MAX1164x_C_SCAN0|MAX1164x_C_SGL
\r
155 #define CH1_M MAX1164x_C_SCAN0|MAX1164x_C_SGL|MAX1164x_C_CS0
\r
156 #define CH0_CH1 MAX1164x_C_SCAN0
\r
159 uint16_t weekmaxarr[8];
\r
161 inline double interp(double t, double h) {
\r
166 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
171 uint8_t startup=10;
\r
174 #define EEPROM_R0 0
\r
175 #define EEPROM_R0d 2
\r
176 #define EEPROM_R0w 4
\r
177 #define EEPROM_dol 6
\r
180 uint16_t readEEPROM(uint8_t addr,uint16_t def) {
\r
182 while(EECR & (1<<EEPE));
\r
188 while(EECR & (1<<EEPE));
\r
197 void writeEEPROM(uint8_t addr,uint16_t val) {
\r
198 while(EECR & (1<<EEPE));
\r
199 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
202 EECR |= (1<<EEMPE);
\r
204 while(EECR & (1<<EEPE));
\r
205 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
208 EECR |= (1<<EEMPE);
\r
213 //volatile double test=calibr_hum(20,1,20);
\r
214 PRR|=(1<<PRADC); // adc for save Power
\r
230 MCUCR &=~(1<<PUD); //All Pins Pullup...
\r
234 pack2.R0=readEEPROM(EEPROM_R0,1);
\r
237 pack2.r_day_max=readEEPROM(EEPROM_R0d,1);
\r
238 pack2.r_week_max=readEEPROM(EEPROM_R0w,1);
\r
239 pack2.tol_d=readEEPROM(EEPROM_dol,0);
\r
240 pack2.tol_s8=0; //Tag faengt mit Einschalten an
\r
241 for(uint8_t i=0;i<7;i++) {
\r
242 weekmaxarr[i]=pack2.r_week_max;
\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
251 USI_TWI_Master_Initialise();
\r
254 SHT2x_ReadUserRegister(userRegister);
\r
255 SHT2x_WriteUserRegister(userRegister); //write changed user reg
\r
256 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);//#define MAX1164x_C_CS0
\r
257 _delay_ms(30); //Internal Referenz start
\r
261 //DDRB|=(1<<PINB1);
\r
263 if (pack1.config==0x1F) {
\r
267 writeEEPROM(EEPROM_R0,0);
\r
269 if (pack1.config==0x05) {
\r
270 pack1.config=0x7F;
\r
272 pack2.r_week_max=1;
\r
274 pack2.tol_s8=0; //Tag faengt mit Einschalten an
\r
276 writeEEPROM(EEPROM_R0,0xFF);
\r
277 writeEEPROM(EEPROM_R0d,0xFF); //Maximum des Tages
\r
278 writeEEPROM(EEPROM_R0w,0xFF); //Maximum der Letzten 7 Tage
\r
279 writeEEPROM(EEPROM_dol,0xFF); //Anzahl der Betriebstage
\r
280 for(uint8_t i=0;i<7;i++) {
\r
286 if (pack2.tol_s8>(10000)) {//10800 ist theortisch der Tag aber meistens zu lang
\r
288 pack2.tol_d++; //rund 180 Jahre :-)
\r
289 pack2.r_week_max=0;
\r
290 weekmaxarr[7]=pack2.r_day_max;
\r
291 for(uint8_t i=0;i<7;i++) {
\r
292 weekmaxarr[i]=weekmaxarr[i+1];
\r
293 if (weekmaxarr[i]>pack2.r_week_max) pack2.r_week_max=weekmaxarr[i];
\r
295 if (pack2.tol_d>7) {
\r
296 pack2.R0=pack2.r_week_max;
\r
298 pack2.R0=pack2.r_day_max;
\r
300 R0=pack2.R0/100.0-0.5*(pack2.R0/100-R0);
\r
302 writeEEPROM(EEPROM_R0,pack2.R0);
\r
303 writeEEPROM(EEPROM_R0d,pack2.r_day_max); //Maximum des Tages
\r
304 writeEEPROM(EEPROM_R0w,pack2.r_week_max); //Maximum der Letzten 7 Tage
\r
305 writeEEPROM(EEPROM_dol,pack2.tol_d); //Anzahl der Betriebstage
\r
308 if (startup!=0) startup--;
\r
309 SHT2x_MeasurePoll(HUMIDITY, &sRH);
\r
310 // --- measure temperature with "Polling Mode" (no hold master) ---
\r
311 SHT2x_MeasurePoll(TEMP, &sT);
\r
312 //-- calculate humidity and temperature --
\r
313 temperatureC = SHT2x_CalcTemperatureC(sT);
\r
314 humidityRH = SHT2x_CalcRH(sRH);
\r
315 ip=interp(temperatureC,humidityRH);
\r
317 humidityRH=calibr_hum(temperatureC,-0.45,humidityRH)*10.0;
\r
318 //humidityRH=humidityRH*10.0;
\r
319 temperatureC =temperatureC *10.0-4.5;
\r
320 hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
\r
321 am2302_hum=0.318*hhum +76.0;
\r
322 am2302_temp=temperatureC*25.6;
\r
323 //PORTB&=~(1<<PINB1);
\r
325 //Kritische Sektion !___Ein Breakpoint in dieser Section kann den TGS8100 zerstoeren!___________________________
\r
326 PORTB&=~(1<<PINB1);
\r
328 mr+=MAX1164x_read();
\r
330 mr+=MAX1164x_read();
\r
333 //ENDE Kritische Sektion !______________________________
\r
334 //l=mr/2.0*2.048/4096;
\r
335 // l maximal 2 mr max 4096 //mr 2V=8000
\r
337 if (pack2.cmode) { //cmode=0 V 0..2 V cmode=1 V 1.5..3.5V
\r
338 //l+=1.5; //Spannung real
\r
343 if (pack2.cmode==0) {
\r
344 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
\r
350 if (pack2.cmode==1) {
\r
351 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
\r
361 pack2.current=l*100;
\r
369 writeEEPROM(EEPROM_R0,pack2.R0);
\r
372 if (l*100>pack2.r_day_max) {
\r
373 pack2.r_day_max=l*100;
\r
378 l=l*160*4; //fuer DS18B20
\r
390 if (t8>pack1.TH) af=1;
\r
391 if (t8<=pack1.TL) af=1;
\r
399 if ((gcontrol==2)||(gcontrol==3)) {
\r
405 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
\r
406 if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
\r
408 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
\r
409 if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
\r
413 MCUCR|=(1<<SE)|(1<<SM1);
\r
414 MCUCR&=~(1<<ISC01);
\r