1 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the
14 // * All advertising materials mentioning features or use of this
15 // software must display the following acknowledgement: This product
16 // includes software developed by tm3d.de and its contributors.
17 // * Neither the name of tm3d.de nor the names of its contributors may
18 // be used to endorse or promote products derived from this software
19 // without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #define F_CPU 8000000UL
37 #include <avr/interrupt.h>
38 #include <util/delay.h>
40 #include <avr/sleep.h>
45 uint8_t owid[8]={0x26, 0xA2, 0xD9, 0x84, 0xDD, 0xDD, 0x05, 0xCE};/**/
46 uint8_t config_info[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
50 extern uint8_t gcontrol;
51 extern uint8_t reset_indicator;
52 extern uint8_t alarmflag;
54 volatile uint8_t wdcounter;
58 #if defined(__AVR_ATtiny25__)
59 volatile uint8_t bytes[16];
61 volatile uint8_t bytes[64];
68 uint8_t threshold; //8
71 #if defined(__AVR_ATtiny25__)
73 uint8_t page2[8]; //17
74 uint8_t page3[8]; //25
75 uint8_t page4[8]; //33
76 uint8_t page5[8]; //41
77 uint8_t page6[8]; //49
78 uint8_t page7[8]; //57
85 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
87 #define DDR_SENSOR DDRB
88 #define PORT_SENSOR PORTB
89 #define PIN_SENSOR PINB
93 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
94 #define DDR_SENSOR DDRA
95 #define PORT_SENSOR PORTA
96 #define PIN_SENSOR PINA
102 #define SENSOR_sda_out DDR_SENSOR |= (1 << SENSOR)
103 #define SENSOR_sda_in DDR_SENSOR &= ~(1 << SENSOR);PORT_SENSOR |= (1 << SENSOR) // release sda => hi in consequence of pullup
104 #define SENSOR_sda_low PORT_SENSOR &= ~(1 << SENSOR)
105 #define SENSOR_is_hi PIN_SENSOR & (1 << SENSOR)
106 #define SENSOR_is_low !(PIN_SENSOR & (1 << SENSOR))
108 volatile int16_t am2302_temp;
109 volatile uint16_t am2302_hum;
112 uint8_t am_wait(uint8_t _time,uint8_t _signal){
116 while((SENSOR_is_hi)&&(TCNT1<_time)) {}
118 while((SENSOR_is_low)&&(TCNT1<_time)) {}
120 if (TCNT1>=_time) {return 1;}
124 volatile uint8_t am2302_mode=0;
125 volatile uint8_t timeout=0;
129 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
134 sleep_disable(); // Disable Sleep on Wakeup
136 if (reset_indicator==1) reset_indicator++;
137 else if (reset_indicator==2) mode=0;
144 sleep_enable(); // Enable Sleep Mode
150 DDRB&=~(1<<PORTB0); //Eingang
151 __asm__ __volatile__ ("nop");
152 PORTB|=(1<<PORTB0); //Pullup
153 __asm__ __volatile__ ("nop");
154 __asm__ __volatile__ ("nop");
155 __asm__ __volatile__ ("nop");
156 __asm__ __volatile__ ("nop");
157 __asm__ __volatile__ ("nop");
159 __asm__ __volatile__ ("nop");
161 __asm__ __volatile__ ("nop");
162 DDRB|=(1<<PORTB0); //Eingang
169 int16_t lam2302_temp;
170 uint16_t lam2302_hum;
172 uint8_t sensor_data[5];
173 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
174 TCCR1=(1<<CS12); //Clock/8 1µs
177 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
178 TCCR1B=(1<<CS11); //Clock/8 1µs
181 SENSOR_sda_low; // MCU start signal
182 TCNT1=0;while(TCNT1==0);while(TCNT1<250) {}// start signal (pull sda down for min 0.8ms and maximum 20ms)
183 TCNT1=0;while(TCNT1==0);while(TCNT1<250) {}// start signal (pull sda down for min 0.8ms and maximum 20ms)
185 if (am_wait(200,1)) return 2;
187 // AM2302 response signal min: 75us typ:80us max:85us
188 if (am_wait(100,0)) return 3;
189 if (am_wait(100,1)) return 4;
191 for(uint8_t i = 0; i < 5; i++) {
192 uint8_t sensor_byte = 0;
193 for(uint8_t j = 1; j <= 8; j++) {// get 8 bits from sensor
194 if (am_wait(88,0)) return 5;
196 TCNT1=0;while(TCNT1==0);;while(TCNT1<35) ;
197 sensor_byte <<= 1; // add new lower byte
198 if (SENSOR_is_hi) {// if sda high after 30us => bit=1 else bit=0
200 if (am_wait(45,1)) return 6;// 30us - 75us = 45us
203 sensor_data[i] = sensor_byte;
207 if ( ((sensor_data[0]+sensor_data[1]+sensor_data[2]+sensor_data[3]) & 0xff ) != sensor_data[4])
210 //printf("%b %b %b %b %b %b" CR, sensor_data[0], sensor_data[1], sensor_data[2], sensor_data[3], sensor_data[4], ((sensor_data[0]+sensor_data[1]+sensor_data[2]+sensor_data[3]) & 0xff ));
217 if (sensor_data[2]&0x80) { //min
218 sensor_data[2]&=~(0x80);
219 htemp=-((sensor_data[2]<<8) + sensor_data[3]);
221 htemp=((sensor_data[2]<<8) + sensor_data[3]);
222 double hhum=(1.0546-0.000216*htemp)*((sensor_data[0]<<8) + sensor_data[1]);
224 lam2302_hum=0.318*hhum +76;
225 lam2302_temp=htemp*25.6;
228 if (sensor_data[2]&0x80) { //minus
229 sensor_data[2]&=~(0x80);
230 lam2302_temp=-((sensor_data[2]<<8) + sensor_data[3]);
232 lam2302_temp=((sensor_data[2]<<8) + sensor_data[3]);
234 lam2302_hum=((sensor_data[0]<<8) + sensor_data[1]);
235 volatile uint32_t h1=lam2302_temp*lam2302_hum*3/44803;
236 lam2302_hum=lam2302_hum*16/49-h1+80;
237 //lam2302_temp=lam2302_temp*128/5;
239 int16_t h2=lam2302_temp%5;
240 lam2302_temp=lam2302_temp/5;
242 lam2302_temp+=h2*128/5;
247 if (sensor_data[2]&0x80) { //minus
248 sensor_data[2]&=~(0x80);
249 lam2302_temp=-((sensor_data[2]<<8) + sensor_data[3]);
251 lam2302_temp=((sensor_data[2]<<8) + sensor_data[3])*25.6;;
253 lam2302_hum=((sensor_data[0]<<8) + sensor_data[1]);
258 am2302_hum=lam2302_hum;
259 am2302_temp=lam2302_temp;
265 uint8_t am2302_2() { //4mhz
266 int16_t lam2302_temp;
267 uint16_t lam2302_hum;
269 uint8_t sensor_data[5];
270 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
271 TCCR1=(1<<CS11)|(1<<CS10); //Clock/8 1µs
274 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
275 TCCR1B=(1<<CS11); //Clock/8 1µs
278 SENSOR_sda_low; // MCU start signal
279 TCNT1=0;while(TCNT1==0);while(TCNT1<250) {}// start signal (pull sda down for min 0.8ms and maximum 20ms)
280 TCNT1=0;while(TCNT1==0);while(TCNT1<250) {}// start signal (pull sda down for min 0.8ms and maximum 20ms)
282 if (am_wait(200,1)) return 2;
284 // AM2302 response signal min: 75us typ:80us max:85us
285 if (am_wait(100,0)) return 3;
286 if (am_wait(100,1)) return 4;
288 for(uint8_t i = 0; i < 5; i++) {
289 uint8_t sensor_byte = 0;
290 for(uint8_t j = 1; j <= 8; j++) {// get 8 bits from sensor
291 if (am_wait(88,0)) return 5;
293 TCNT1=0;while(TCNT1==0);;while(TCNT1<35) ;
294 sensor_byte <<= 1; // add new lower byte
295 if (SENSOR_is_hi) {// if sda high after 30us => bit=1 else bit=0
297 if (am_wait(45,1)) return 6;// 30us - 75us = 45us
300 sensor_data[i] = sensor_byte;
304 if ( ((sensor_data[0]+sensor_data[1]+sensor_data[2]+sensor_data[3]) & 0xff ) != sensor_data[4])
307 //printf("%b %b %b %b %b %b" CR, sensor_data[0], sensor_data[1], sensor_data[2], sensor_data[3], sensor_data[4], ((sensor_data[0]+sensor_data[1]+sensor_data[2]+sensor_data[3]) & 0xff ));
312 lam2302_hum=0.318* ((sensor_data[0]<<8) + sensor_data[1])+76;
313 if (sensor_data[2]&0x80) { //minus
314 sensor_data[2]&=~(0x80);
315 lam2302_temp=-((sensor_data[2]<<8) + sensor_data[3])*25.6;
317 lam2302_temp=((sensor_data[2]<<8) + sensor_data[3])*25.6;
321 if (sensor_data[2]&0x80) { //minus
322 sensor_data[2]&=~(0x80);
323 lam2302_temp=-((sensor_data[2]<<8) + sensor_data[3]);
325 lam2302_temp=((sensor_data[2]<<8) + sensor_data[3]);
327 lam2302_hum=((sensor_data[0]<<8) + sensor_data[1]);
328 volatile uint32_t h1=lam2302_temp*lam2302_hum*3/44803;
329 lam2302_hum=lam2302_hum*16/49-h1+80;
330 //lam2302_temp=lam2302_temp*128/5;
332 int16_t h2=lam2302_temp%5;
333 lam2302_temp=lam2302_temp/5;
335 lam2302_temp+=h2*128/5;
341 am2302_hum=lam2302_hum;
342 am2302_temp=lam2302_temp;
350 PRR|=(1<<PRUSI)|(1<<PRADC); //Switch off usi and adc for save Power
351 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
353 PORTB=0xFF-(1<<PORTB0); //Schalter kann gegen Masse sein und zieht dann immer Strom
354 DDRB|=(1<<PORTB0); //Als Ausgang und 0
358 ACSR|=(1<<ACD); //Disable Comparator
359 ADCSRB|=(1<<ACME); //Disable Analog multiplexer
360 MCUCR &=~(1<<PUD); //All Pins Pullup...
364 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
366 PORTB|=(1<<PINB0)|(1<<PINB1)|(1<<PINB3)|(1<<PINB4)|(1<<PINB5);
367 DDRB|=(1<<PINB1); //DBLINE
368 #define SENSON PORTB|=(1<<PINB0);
369 #define SENSOFF PORTB&=~(1<<PINB0);
370 DDRB|=(1<<PINB0); //stromversorgung
372 // Set up Watch Dog Timer for Inactivity
373 WDTCR |= ((1<<WDCE) | (1<<WDE)); // Enable the WD Change Bit
374 WDTCR = (1<<WDIE) | // Enable WDT Interrupt
375 (1<<WDP2) | (1<<WDP1); // Set Timeout to ~2 seconds
378 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
381 DDRA|=(1<<PINA1); //stromversorgung
382 #define SENSON PORTA|=(1<<PINA1);
383 #define SENSOFF PORTA&=~(1<<PINA1);
385 // Set up Watch Dog Timer for Inactivity
386 WDTCSR |= (1<<WDCE) ; // Enable the WD Change Bit
387 WDTCSR = (1<<WDIE) | // Enable WDT Interrupt
388 (1<<WDP2) | (1<<WDP1); // Set Timeout to ~2 seconds
394 #if defined(__AVR_ATtiny25__)
395 for(i=0;i<16;i++) pack.bytes[i]=0;
398 for(i=0;i<64;i++) pack.bytes[i]=0;
408 if (am2302_mode==2) {
411 #if defined(__AVR_ATtiny25__)
414 pn=pn+1;if (pn>31) pn=1;
420 if (am2302_mode>=8) {
424 #if defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
425 if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
427 #if defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__) || defined(__AVR_ATtiny84A__)
428 if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
431 // CLKPR=(1<<CLKPCE);
432 // CLKPR=(1<<CLKPS2); /*0.5Mhz*/
433 //PORTB&=~(1<<PINB1);
434 MCUCR|=(1<<SE)|(1<<SM1);