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 <avr/pgmspace.h>
41 #include "../common/I2C/USI_TWI_Master.h"
42 #include "../common/I2C/MAX1164x.h"
43 #include "../common/I2C/SHT2x.h"
44 #include <avr/pgmspace.h>
47 extern void EXTERN_SLEEP();
50 const int16_t hl[5] PROGMEM = {0,400,650,850,1000}; //H*10
53 const int16_t il[5][5] PROGMEM= {
54 {470, 247, 140, 77, 49},
55 {336, 104, 0, -62, -83},
56 {207, -20, -139, -198, -223},
57 {86, -139, -248, -315, -342},
58 {-30, -248, -371, -431, -462}};
64 extern void EXTERN_SLEEP();
67 uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/
68 uint8_t owid2[8]={0x26, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x67};/**/
69 uint8_t config_info1[16]={0x08,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
70 uint8_t config_info2[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x07,0x08, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
73 #error "Variable not correct"
77 extern uint8_t gcontrol;
78 extern uint8_t reset_indicator;
79 extern uint8_t alarmflag;
80 volatile uint8_t wdcounter=1;
83 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
89 if (reset_indicator==1) reset_indicator++;
90 else if (reset_indicator==2) mode=0;
97 volatile uint8_t bytes[8];
108 volatile pack1_t pack1;
113 #if defined(__AVR_ATtiny25__)
114 volatile uint8_t bytes[16];
116 volatile uint8_t bytes[64];
121 uint16_t voltage; //4
123 uint8_t threshold; //8
125 uint8_t page1[8]; //9
126 #if defined(__AVR_ATtiny25__)
128 uint8_t page2[8]; //17
129 uint8_t page3[8]; //25
130 uint8_t page4[8]; //33
131 uint8_t page5[8]; //41
132 uint8_t page6[8]; //49
133 uint8_t page7[8]; //57
138 volatile pack2_t pack2;
143 volatile int16_t am2302_temp;
144 volatile uint16_t am2302_hum;
147 uint8_t userRegister[1];
149 volatile double temperatureC,humidityRH;
153 #define CH0_M MAX1164x_C_SCAN0|MAX1164x_C_SGL
154 #define CH1_M MAX1164x_C_SCAN0|MAX1164x_C_SGL|MAX1164x_C_CS0
155 #define CH0_CH1 MAX1164x_C_SCAN0
158 //T und H mit 10 multipliziert
160 double interp(int16_t t, int16_t h) {
163 int16_t x1,x2,y1_,y2;
166 for(uint8_t i=0;i<5;i++) {
167 int16_t hlw=pgm_read_word(&(hl[i]));
171 if (h2>4) {h2=4;h1=3;}
172 t2=((uint8_t)t/100); //t mit 10 multipliziert
175 x1=pgm_read_word(&(hl[h1]));
176 x2=pgm_read_word(&(hl[h2]));
177 y1_=pgm_read_word(&(il[t1][h1]));
178 y2=pgm_read_word(&(il[t1][h2]));
179 double d=(double)(h-x1)/(x2-x1);
181 y1_=pgm_read_word(&(il[t2][h1]));
182 y2=pgm_read_word(&(il[t2][h2]));
186 return exp((ya+(yb-ya)/(double)(x2-x1)*(t-x1))/1000.0);
195 PRR|=(1<<PRADC); // adc for save Power
211 MCUCR &=~(1<<PUD); //All Pins Pullup...
214 while(EECR & (1<<EEPE));
220 while(EECR & (1<<EEPE));
225 pack2.page3[0]=hr&0xFF;
226 pack2.page3[1]=(hr>>8)&0xFF;
230 /*volatile double test=interp(0,800);
231 test=interp(100,900);
232 test=interp(110,400);
233 test=interp(300,200);
234 test=interp(300,210);
235 test=interp(280,600);
236 test=interp(100,900);
239 WDTCSR |= ((1<<WDCE) ); // Enable the WD Change Bit//| (1<<WDE)
240 WDTCSR |= (1<<WDIE) | // Enable WDT Interrupt
241 (1<<WDP3) | (1<<WDP0); // Set Timeout to ~8 seconds
244 USI_TWI_Master_Initialise();
247 SHT2x_ReadUserRegister(userRegister);
248 //(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;
249 SHT2x_WriteUserRegister(userRegister); //write changed user reg
250 // --- measure humidity with "Hold Master Mode (HM)" ---
251 SHT2x_MeasurePoll(HUMIDITY, &sRH);
252 // --- measure temperature with "Polling Mode" (no hold master) ---
253 SHT2x_MeasurePoll(TEMP, &sT);
254 //-- calculate humidity and temperature --
255 temperatureC = SHT2x_CalcTemperatureC(sT);
256 humidityRH = SHT2x_CalcRH(sRH);
257 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);//#define MAX1164x_C_CS0
258 _delay_ms(30); //Internal Referenz start
265 if (startup!=0) startup--;
266 //PORTB|=(1<<PINB1); //Dauer 440ms
267 SHT2x_MeasurePoll(HUMIDITY, &sRH);
268 // --- measure temperature with "Polling Mode" (no hold master) ---
269 SHT2x_MeasurePoll(TEMP, &sT);
270 //-- calculate humidity and temperature --
271 temperatureC = SHT2x_CalcTemperatureC(sT)*10.0;
272 humidityRH = SHT2x_CalcRH(sRH)*10.0;
273 double hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
274 am2302_hum=0.318*hhum +76.0;
275 am2302_temp=temperatureC*25.6;
276 //PORTB&=~(1<<PINB1);
282 l=l*2.048/4096+1.5; //Spannung real
284 l=l*2.048/4096; //Spannung real
288 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_CH1);
290 pack2.page3[4]=cmode;
295 MAX1164x_config(MAX1164x_S_SEL2|MAX1164x_S_SEL0,CH0_M);
297 pack2.page3[4]=cmode;
303 pack2.page3[2]=hr&0xFF;
304 pack2.page3[3]=(hr>>8)&0xFF;
307 float ip=interp(temperatureC,humidityRH);
309 pack2.page3[5]=hr&0xFF;
310 pack2.page3[6]=(hr>>8)&0xFF;
317 pack2.page3[0]=hr&0xFF;
318 pack2.page3[1]=(hr>>8)&0xFF;
320 while(EECR & (1<<EEPE));
321 EECR = (0<<EEPM1)|(0<<EEPM0);
326 while(EECR & (1<<EEPE));
327 EECR = (0<<EEPM1)|(0<<EEPM0);
337 l=l*160; //fuer DS18B20
346 if (t8>pack1.TH) af=1;
347 if (t8<=pack1.TL) af=1;
355 if ((gcontrol==2)||(gcontrol==3)) {
361 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
362 if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
364 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
365 if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
369 MCUCR|=(1<<SE)|(1<<SM1);