2 // Copyright (c) 2017, 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 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
\r
35 // All rights reserved.
\r
37 // Redistribution and use in source and binary forms, with or without
\r
38 // modification, are permitted provided that the following conditions are
\r
41 // * Redistributions of source code must retain the above copyright
\r
42 // notice, this list of conditions and the following disclaimer.
\r
43 // * Redistributions in binary form must reproduce the above copyright
\r
44 // notice, this list of conditions and the following disclaimer in the
\r
45 // documentation and/or other materials provided with the
\r
47 // * All advertising materials mentioning features or use of this
\r
48 // software must display the following acknowledgement: This product
\r
49 // includes software developed by tm3d.de and its contributors.
\r
50 // * Neither the name of tm3d.de nor the names of its contributors may
\r
51 // be used to endorse or promote products derived from this software
\r
52 // without specific prior written permission.
\r
54 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
55 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
56 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
\r
57 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
\r
58 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
59 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
60 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
61 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
62 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
63 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
64 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
66 //!!!!!Max Program size 7551 Byte
\r
71 #define F_CPU 8000000UL
\r
73 #include <avr/interrupt.h>
\r
74 #include <util/delay.h>
\r
75 #include <avr/wdt.h>
\r
76 #include <avr/sleep.h>
\r
77 #include <avr/pgmspace.h>
\r
78 #include "../common/I2C/USI_TWI_Master.h"
\r
79 #include "../common/I2C/MAX1164x.h"
\r
80 #include "../common/I2C/SHT2xV2.h"
\r
81 #include "../common/calibr.h"
\r
83 extern void OWINIT();
\r
84 extern void EXTERN_SLEEP();
\r
86 uint8_t owid[8]={0x20, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x02, 0x5D};/**/
\r
87 uint8_t config_info[26]={0x01,14,0x04,0x08, 8,8,11,0x08,0x02,7,7,17,17,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
\r
90 #error "Variable not correct"
\r
93 extern uint8_t mode;
\r
94 extern uint8_t gcontrol;
\r
95 extern uint8_t reset_indicator;
\r
96 extern uint8_t alarmflag;
\r
97 volatile uint8_t wdcounter=1;
\r
99 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
\r
100 ISR(WATCHDOG_vect) {
\r
105 if (reset_indicator==1) reset_indicator++;
\r
106 else if (reset_indicator==2) mode=0;
\r
111 volatile uint8_t bytes[0x20];
\r
151 volatile pack_t pack;
\r
157 volatile int16_t am2302_temp;
\r
158 volatile uint16_t am2302_hum;
\r
161 uint8_t userRegister[1];
\r
163 double temperatureC,humidityRH,hhum;
\r
170 #define CH0_M MAX1164x_C_SCAN0|MAX1164x_C_SGL
\r
171 #define CH1_M MAX1164x_C_SCAN0|MAX1164x_C_SGL|MAX1164x_C_CS0
\r
172 #define CH0_CH1 MAX1164x_C_SCAN0
\r
175 uint16_t weekmaxarr[8];
\r
177 //Kompensieren der Abhänigkeit von RS/RO von Temperatur und Luftfeuchte
\r
178 inline double interp(double t, double h) {
\r
183 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
188 uint8_t startup=10;
\r
194 DDRB&=~(1<<PORTB0); //Eingang
\r
195 __asm__ __volatile__ ("nop");
\r
196 PORTB|=(1<<PORTB0); //Pullup
\r
197 __asm__ __volatile__ ("nop");
\r
198 __asm__ __volatile__ ("nop");
\r
199 __asm__ __volatile__ ("nop");
\r
200 __asm__ __volatile__ ("nop");
\r
201 __asm__ __volatile__ ("nop");
\r
202 r=PINB&(1<<PORTB0);
\r
203 __asm__ __volatile__ ("nop");
\r
204 PORTB&=~(1<<PORTB0);
\r
205 __asm__ __volatile__ ("nop");
\r
206 DDRB|=(1<<PORTB0); //Eingang
\r
207 return (r==0); //Offen mal HIH4030
\r
212 #define EEPROM_R0 0
\r
213 #define EEPROM_R0d 2
\r
214 #define EEPROM_R0w 4
\r
215 #define EEPROM_dol 6
\r
218 uint16_t readEEPROM(uint8_t addr,uint16_t def) {
\r
220 while(EECR & (1<<EEPE));
\r
226 while(EECR & (1<<EEPE));
\r
235 void writeEEPROM(uint8_t addr,uint16_t val) {
\r
236 while(EECR & (1<<EEPE));
\r
237 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
240 EECR |= (1<<EEMPE);
\r
242 while(EECR & (1<<EEPE));
\r
243 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
246 EECR |= (1<<EEMPE);
\r
256 uint16_t pr_day_max;
\r
257 uint16_t pr_week_max;
\r
283 PRR|=(1<<PRADC); // adc for save Power
\r
285 PORTB=0xFF-(1<<PORTB0); //Schalter kann gegen Masse sein und zieht dann immer Strom
\r
286 DDRB|=(1<<PORTB0); //Als Ausgang und 0
\r
288 DDRB|=(1<<PINB1);//Ausgang und 1
\r
291 WDTCSR |= ((1<<WDCE) ); // Enable the WD Change Bit//| (1<<WDE)
\r
292 WDTCSR |= (1<<WDIE) | // Enable WDT Interrupt
\r
293 //(1<<WDP0) |(1<<WDP2) | (1<<WDP1); // Set Timeout to ~2 seconds
\r
294 (1<<WDP3) | (1<<WDP0); // Set Timeout to ~8 seconds
\r
298 writeEEPROM(EEPROM_R0,0xFF);
\r
299 writeEEPROM(EEPROM_R0d,0xFF); //Maximum des Tages
\r
300 writeEEPROM(EEPROM_R0w,0xFF); //Maximum der Letzten 7 Tage
\r
301 writeEEPROM(EEPROM_dol,0xFF); //Anzahl der Betriebstage
\r
309 pR0=readEEPROM(EEPROM_R0,1);
\r
312 pr_day_max=readEEPROM(EEPROM_R0d,1);
\r
313 pr_week_max=readEEPROM(EEPROM_R0w,1);
\r
314 ptol_d=readEEPROM(EEPROM_dol,0);
\r
315 ptol_s8=0; //Tag faengt mit Einschalten an
\r
316 for(uint8_t i=0;i<7;i++) {
\r
317 weekmaxarr[i]=pr_week_max;
\r
320 USI_TWI_Master_Initialise();
\r
323 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);//#define MAX1164x_C_CS0
\r
324 _delay_ms(30); //Internal Referenz start
\r
330 //DDRB|=(1<<PINB1);
\r
336 writeEEPROM(EEPROM_R0,0);
\r
340 if (wdcounter>0) { //8s
\r
342 if (ptol_s8>(10000)) {//10800 ist theortisch der Tag aber meistens zu lang
\r
344 ptol_d++; //rund 180 Jahre :-)
\r
346 weekmaxarr[7]=pr_day_max;
\r
347 for(uint8_t i=0;i<7;i++) {
\r
348 weekmaxarr[i]=weekmaxarr[i+1];
\r
350 if (weekmaxarr[i]>pr_week_max) pr_week_max=weekmaxarr[i];
\r
357 //R0=//R0-0.5*(pack2.R0/100-R0);
\r
358 R0=R0-(R0-pR0/100.0)*0.5 ;
\r
360 writeEEPROM(EEPROM_R0,pR0);
\r
361 writeEEPROM(EEPROM_R0d,pr_day_max); //Maximum des Tages
\r
362 writeEEPROM(EEPROM_R0w,pr_week_max); //Maximum der Letzten 7 Tage
\r
363 writeEEPROM(EEPROM_dol,ptol_d); //Anzahl der Betriebstage
\r
366 if (startup!=0) startup--;
\r
367 getSHT2xHumTemp(&temperatureC,&humidityRH);
\r
368 ip=interp(temperatureC,humidityRH);
\r
370 RH=calibr_hum(temperatureC,-0.2,humidityRH);
\r
371 TC =temperatureC -0.2;
\r
373 //Kritische Sektion !___Ein Breakpoint in dieser Section kann den TGS8100 zerstoeren!___________________________
\r
374 PORTB&=~(1<<PINB1); //Auf 0 Ziehen
\r
376 mr+=MAX1164x_read();
\r
378 mr+=MAX1164x_read();
\r
380 //ENDE Kritische Sektion !______________________________
\r
381 //l=mr/2.0*2.048/4096;
\r
382 // l maximal 2 mr max 4096 //mr 2V=8000
\r
384 if (pcmode) { //cmode=0 V 0..2 V cmode=1 V 1.5..3.5V
\r
385 //l+=1.5; //Spannung real
\r
391 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
\r
398 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
\r
415 writeEEPROM(EEPROM_R0,pR0);
\r
418 if (l*100>pr_day_max) {
\r
421 l=exp((1-(l/R0))*6.05)-1;// exp((1-($val)/55)*5.75); (5.75 geht über 125 6.05 geht bis 240... mittlere Linie im Datenblatt)
\r
423 l=l*100; //fuer DS2450
\r
426 else l=0; //negative Werte am Anfang verhintern
\r
433 //PORTB|=(1<<PINB1);
\r
436 for(uint8_t i=0;i<4;i++){
\r
437 if (pack.convc1&bb1) {
\r
438 if (pack.convc2&(bb)) {pack.bytes[i*2]=0;pack.bytes[i*2+1]=0;}
\r
440 if (pack.convc2&(bb)) {pack.bytes[i*2]=0xFF;pack.bytes[i*2+1]=0xFF;}
\r
446 if (pack.convc1&1) {
\r
447 uint16_t ct=(temperatureC*100.0)+32767;
\r
448 cli();pack.A=ct;sei();
\r
450 if (pack.CSA2&0x08) //AEH
\r
451 if (pack.bytes[1]>pack.HA) {alarmflag=1;pack.CSA2|=0x20;}
\r
452 if (pack.CSA2&0x04) //AEL
\r
453 if (pack.bytes[1]<pack.LA) {alarmflag=1;pack.CSA2|=0x10;}
\r
456 if (pack.convc1&2) {
\r
457 cli();pack.B=humidityRH*100;sei();
\r
458 if (pack.CSB2&0x08) //AEH
\r
459 if (pack.bytes[1]>pack.HB) {alarmflag=1;pack.CSB2|=0x20;}
\r
460 if (pack.CSB2&0x04) //AEL
\r
461 if (pack.bytes[1]<pack.LB) {alarmflag=1;pack.CSB2|=0x10;}
\r
464 if (pack.convc1&4) {
\r
465 cli();pack.C=l;sei();
\r
466 if (pack.CSC2&0x08) //AEH
\r
467 if (pack.bytes[1]>pack.HC) {alarmflag=1;pack.CSC2|=0x20;}
\r
468 if (pack.CSC2&0x04) //AEL
\r
469 if (pack.bytes[1]<pack.LC) {alarmflag=1;pack.CSC2|=0x10;}
\r
471 if (pack.convc1&8) {
\r
472 if (pack.CSD2&0x01) ADMUX=3; else ADMUX=0x83;
\r
475 while ((ADCSRA&(1<<ADSC)));
\r
476 cli();pack.D=RA;sei();
\r
477 if (pack.CSD2&0x08) //AEH
\r
478 if (pack.bytes[1]>pack.HD) {alarmflag=1;pack.CSD2|=0x20;}
\r
479 if (pack.CSD2&0x04) //AEL
\r
480 if (pack.bytes[1]<pack.LD) {alarmflag=1;pack.CSD2|=0x10;}
\r
484 //PORTB&=~(1<<PINB1);
\r
488 for(uint8_t i=0;i<4;i++) {
\r
489 if (pack.bytes[8+i*2]&0x80) { //Chanel as output
\r
490 if (pack.bytes[8+i*2]&0x40) {
\r
501 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
\r
502 if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
\r
504 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
\r
505 if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
\r
509 MCUCR|=(1<<SE)|(1<<SM1);
\r
510 MCUCR&=~(1<<ISC01);
\r