- New Device compinations
[owSlave2.git] / DS2438_DHT22 / DS2438_DHT22.c
1 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 //    distribution.
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.
20 //
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.
32
33
34 #define F_CPU 8000000UL
35 #include <avr/io.h>
36 #include <avr/interrupt.h>
37 #include <util/delay.h>
38 #include <avr/wdt.h>
39 #include <avr/sleep.h>
40 #include <avr/pgmspace.h>
41
42
43 extern void OWINIT();
44 extern void EXTERN_SLEEP();
45
46 uint8_t owid[8]={0x26, 0xA2, 0xD9, 0x84, 0xDD, 0xDD, 0x05, 0xCE};/**/
47 uint8_t config_info[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
48
49
50
51 extern uint8_t mode;
52 extern uint8_t gcontrol;
53 extern uint8_t reset_indicator;
54 extern uint8_t alarmflag;
55
56 volatile uint8_t wdcounter;
57
58
59 typedef union {
60
61         volatile uint8_t bytes[64];
62         struct {
63                 uint8_t status;  //1
64                 int16_t temp;  //2
65                 uint16_t voltage;  //4
66                 uint16_t current;  //6
67                 uint8_t threshold; //8
68                 
69                 uint8_t page1[8]; //9
70                 uint8_t page2[8]; //17
71                 uint8_t page3[8]; //25
72                 uint8_t page4[8];  //33
73                 uint8_t page5[8];  //41
74                 uint8_t page6[8];  //49
75                 uint8_t page7[8];  //57
76                 //uint8_t crc;  //65
77         };
78 } pack_t;
79 volatile pack_t pack;
80
81
82 #define DDR_SENSOR   DDRA
83 #define PORT_SENSOR  PORTA
84 #define PIN_SENSOR   PINA
85 #define SENSOR       PINA2
86
87 #define SENSOR_sda_out          DDR_SENSOR |= (1 << SENSOR)
88 #define SENSOR_sda_in                   DDR_SENSOR &= ~(1 << SENSOR);PORT_SENSOR |= (1 << SENSOR) // release sda => hi in consequence of pullup
89 #define SENSOR_sda_low    PORT_SENSOR &= ~(1 << SENSOR)
90 #define SENSOR_is_hi                    PIN_SENSOR & (1 << SENSOR)
91 #define SENSOR_is_low           !(PIN_SENSOR & (1 << SENSOR))
92
93 volatile int16_t am2302_temp;
94 volatile uint16_t am2302_hum;
95
96
97 uint8_t am_wait(uint8_t _time,uint8_t _signal){
98         TCNT1=0;
99         while(TCNT1==0);
100         if (_signal)
101         while((SENSOR_is_hi)&&(TCNT1<_time)) {}
102         else
103         while((SENSOR_is_low)&&(TCNT1<_time)) {}
104         
105         if (TCNT1>=_time) {return 1;}
106         return 0;
107 }
108
109 volatile uint8_t am2302_mode=0;
110 volatile uint8_t timeout=0;
111
112
113
114
115
116
117 uint8_t userRegister[1];
118 int16_t sRH,sT;
119 volatile double temperatureC,humidityRH;
120 volatile double l;
121
122 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
123 ISR(WATCHDOG_vect) {
124         #else
125 ISR(WDT_vect) {
126         #endif
127         wdcounter++;
128         if (reset_indicator==1) reset_indicator++;
129         else if (reset_indicator==2) mode=0;
130 }
131         
132 int testSW(void) {
133         uint8_t r;
134         DDRB&=~(1<<PORTB0);  //Eingang
135         __asm__ __volatile__ ("nop");
136         PORTB|=(1<<PORTB0); //Pullup
137         __asm__ __volatile__ ("nop");
138         __asm__ __volatile__ ("nop");
139         __asm__ __volatile__ ("nop");
140         __asm__ __volatile__ ("nop");
141         __asm__ __volatile__ ("nop");
142         r=PINB&(1<<PORTB0);
143         __asm__ __volatile__ ("nop");
144         PORTB&=~(1<<PORTB0);
145         __asm__ __volatile__ ("nop");
146         DDRB|=(1<<PORTB0);  //Eingang
147         return (r==0);
148 }
149
150 uint8_t am2302() {
151         int16_t lam2302_temp;
152         uint16_t lam2302_hum;
153         uint8_t rSREG;
154         uint8_t sensor_data[5];
155
156         TCCR1B=(1<<CS11); //Clock/8 1µs
157
158         SENSOR_sda_out;
159         SENSOR_sda_low; // MCU start signal
160         TCNT1=0;while(TCNT1==0);while(TCNT1<250) {}// start signal (pull sda down for min 0.8ms and maximum 20ms)
161         TCNT1=0;while(TCNT1==0);while(TCNT1<250) {}// start signal (pull sda down for min 0.8ms and maximum 20ms)
162         SENSOR_sda_in;
163         if (am_wait(200,1)) return 2;
164
165         // AM2302 response signal min: 75us typ:80us max:85us or 500us different types
166         if (am_wait(100,0)) return 3;
167         if (am_wait(100,1)) return 4;
168         
169         for(uint8_t i = 0; i < 5; i++)  {
170                 uint8_t sensor_byte = 0;
171                 for(uint8_t j = 1; j <= 8; j++) {// get 8 bits from sensor
172                         if (am_wait(88,0)) return 5;
173                         
174                         TCNT1=0;while(TCNT1==0);;while(TCNT1<35)                ;
175                         sensor_byte <<= 1; // add new lower byte
176                         if (SENSOR_is_hi) {// if sda high after 30us => bit=1 else bit=0
177                                 sensor_byte |= 1;
178                                 if (am_wait(45,1)) return 6;// 30us - 75us = 45us
179                         }
180                 }
181                 sensor_data[i] = sensor_byte;
182         }
183
184         // checksum
185         if ( ((sensor_data[0]+sensor_data[1]+sensor_data[2]+sensor_data[3]) & 0xff ) != sensor_data[4])
186         {
187                 PORTB&=~(1<<PINB0);
188                 return 7;
189         }
190         if (sensor_data[2]&0x80) { //minus
191                 sensor_data[2]&=~(0x80);
192                 lam2302_temp=-((sensor_data[2]<<8) + sensor_data[3]);
193         } else
194                 lam2302_temp=((sensor_data[2]<<8) + sensor_data[3]);
195         if (!testSW()) {
196                 double htemp=lam2302_temp;
197         double hhum=(1.0546-0.000216*htemp)*((sensor_data[0]<<8) + sensor_data[1]);
198                 //htemp=Temperauter * 10 
199                 //lam2302_hum=0.318*hhum +76;
200                 lam2302_hum=0.31*hhum +80; //hhum = Hum*10 -> 5V also all *5
201                 config_info[5]=7;
202         }
203         else {
204                 
205                 lam2302_hum=((sensor_data[0]<<8) + sensor_data[1]);
206                 config_info[5]=12;
207         }
208         lam2302_temp=lam2302_temp*25.6;
209         rSREG=SREG;
210         cli();
211         am2302_hum=lam2302_hum;
212         am2302_temp=lam2302_temp;
213         SREG=rSREG;
214         return 0;
215 }
216
217
218
219
220
221
222
223
224         int main(void){
225                 PRR|=(1<<PRUSI)|(1<<PRADC);   // adc and usi for save Power
226                 PORTA=0xFF;
227                 PORTB=0xFF-(1<<PORTB0); //Schalter kann gegen Masse sein und zieht dann immer Strom
228                 DDRB|=(1<<PORTB0); //Als Ausgang und 0
229                 
230                 
231                 OWINIT();
232
233                 ACSR|=(1<<ACD);  //Disable Comparator
234                 ADCSRB|=(1<<ACME); //Disable Analog multiplexer
235                 MCUCR &=~(1<<PUD); //All Pins Pullup...
236                 MCUCR |=(1<<BODS);
237
238                 
239                 DDRA|=(1<<PINA1); //stromversorgung
240 #define SENSON PORTA|=(1<<PINA1);
241 #define SENSOFF PORTA&=~(1<<PINA1);
242
243                 // Set up Watch Dog Timer for Inactivity
244                 WDTCSR |= (1<<WDCE) ;   // Enable the WD Change Bit
245                 WDTCSR =   (1<<WDIE) |              // Enable WDT Interrupt
246                         (1<<WDP2) | (1<<WDP1);   // Set Timeout to ~2 seconds           
247                 
248                 if (testSW()) {
249                         config_info[5]=12;  //hum *10
250                         }else{
251                         config_info[5]=7; //HIH4031
252                 }
253                 
254                 uint8_t i;
255                 for(i=0;i<64;i++) pack.bytes[i]=0;
256                 MCUSR=0;
257                 
258                 uint8_t err;
259                 uint8_t pn=1;
260                 SENSON
261                 _delay_ms(4000);
262                 err =am2302();
263                 
264                 sei();
265                 
266                 while(1)   {
267                         if (gcontrol) {
268                                 if (wdcounter>2) {
269                                         wdcounter=0;
270                                         SENSON
271                                 }
272                                 gcontrol=0;
273                         }
274                         if (wdcounter==2) {
275                                 err=am2302();
276                                 if (err!=0) {
277                                         pack.page3[pn]=err;
278                                         pn=pn+1;if (pn>31) pn=1;
279                                 }
280                                 SENSOFF
281                                 wdcounter=3;
282                         }
283                         if (wdcounter>=16) {
284                                 wdcounter=0;
285                                 SENSON
286                         }
287                         
288                         
289
290                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))       {
291                                 MCUCR|=(1<<SE)|(1<<SM1);
292                                 MCUCR&=~(1<<ISC01);
293                                 } else {
294                                 MCUCR|=(1<<SE);
295                                 MCUCR&=~(1<<SM1);
296                         }
297                         asm("SLEEP");
298                 }
299
300
301         }