4927b997f6d1441217843e94cff48377db19032f
[owSlave2.git] / DS18B20_Thermocouble_DS2438_SHT / DS18B20_DS2438.c
1 \r
2 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de\r
3 // All rights reserved.\r
4 //\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
7 // met:\r
8 //\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
14 //    distribution.\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
21 //\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
33 \r
34 #define F_CPU 8000000UL\r
35 #include <avr/io.h>\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/SHT2x.h"\r
43 \r
44 \r
45 extern void OWINIT();\r
46 extern void EXTERN_SLEEP();\r
47 \r
48 //const float k_rs[54] PROGMEM ={0.000000,24.125000,48.585366,72.731707,96.829268,121.097561,145.700000,170.600000,195.650000,220.625000,245.365854,269.853659,294.119048,318.195122,342.166667,366.000000,389.761905,413.428571,437.023810,460.558140,484.047619,507.511628,530.976190,554.418605,577.883721,601.395349,624.952381,648.571429,672.285714,696.073171,719.976190,744.000000,768.146341,792.439024,816.853659,841.414634,866.125000,890.975000,916.000000,941.179487,966.525000,992.025641,1017.717949,1043.589744,1069.657895,1095.945946,1122.432432,1149.184211,1176.189189,1203.472222,1231.083333,1259.000000,1287.285714,1315.941176};\r
49 const float k_rs[61] PROGMEM ={ -212.538462, -166.260870, -124.892857, -97.562500, -66.888889, -34.157895, 0.000000, 24.125000, 48.585366, 72.731707, 96.829268, 121.097561, 145.700000, 170.600000, 195.650000, 220.625000, 245.365854, 269.853659, 294.119048, 318.192771, 342.166667, 366.000000, 389.761905, 413.428571, 437.023810, 460.558140, 484.047619, 507.511628, 530.976190, 554.418605, 577.883721, 601.395349, 624.952381, 648.571429, 672.285714, 696.073171, 719.976190, 744.000000, 768.146341, 792.439024, 816.853659, 841.414634, 866.125000, 890.975000, 916.000000, 941.179487, 966.525000, 992.025641, 1017.717949, 1043.589744, 1069.657895, 1095.945946, 1122.432432, 1149.173333, 1176.189189, 1203.472222, 1231.083333, 1259.000000, 1287.285714, 1315.941176, 1344.941176};\r
50 #define k_ofs 6\r
51 \r
52 //const float j_rs[70] PROGMEM ={0, 18.302913, 34.830476, 50.783019, 70.653704, 90.505455, 110.341818, 130.165455, 149.163636, 160.791071, 180.596364, 200.398214, 220.200000, 240.000000, 250.882883, 270.603636, 290.409091, 310.216364, 330.025455, 342.472727, 360.649091, 380.461818, 400.275000, 420.087273, 435.275676, 450.703636, 470.503636, 490.298214, 510.082456, 523.486726, 540.621053, 560.370175, 580.105172, 591.979487, 610.527119, 630.213559, 644.601653, 660.534426, 680.168852, 690.787097, 710.391935, 729.123810, 740.559375, 760.126562, 770.684615, 790.235385, 800.782812, 820.331250, 834.681250, 850.446032, 870.017460, 880.600000, 900.196774, 911.099187, 930.432787, 950.073333, 960.728333, 980.396667, 1000.078333, 1010.772881, 1030.475862, 1050.187931, 1065.717241, 1080.631034, 1100.358621, 1120.089655, 1131.840000,1150.556897, 1170.294737, 1190.035088};\r
53 double gettemp_rs(double V) {\r
54         uint8_t iv=(uint8_t)(V+k_ofs);\r
55         float t0=pgm_read_float(&(k_rs[iv]));\r
56         float t1=pgm_read_float(&(k_rs[iv+1]));\r
57         return t0+(t1-t0)/1*((V+k_ofs)-iv);\r
58 }\r
59 \r
60 \r
61 \r
62 \r
63 \r
64 uint8_t owid1[8]={0x28, 0xA6, 0xD9, 0x84, 0x00, 0x00, 0x03, 0x68};/**/\r
65 uint8_t owid2[8]={0x26, 0xA7, 0xD9, 0x84, 0x00, 0x00, 0x03, 0x20};/**/\r
66 uint8_t config_info1[26]={0x01,0x02 ,0x00,0x00, 0x00,0x00, 0x00,0x00, 0x02,6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};  \r
67 uint8_t config_info2[26]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x00,0x00, 0x02,7,0x00,7,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};     \r
68         \r
69 #if (owid>128) \r
70 #error "Variable not correct"\r
71 #endif\r
72 \r
73 extern uint8_t mode;\r
74 extern uint8_t gcontrol;\r
75 extern uint8_t reset_indicator;\r
76 extern uint8_t alarmflag;\r
77 volatile uint8_t wdcounter=5;\r
78 \r
79 \r
80 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)\r
81 ISR(WATCHDOG_vect) {\r
82 #else\r
83 ISR(WDT_vect) {\r
84 #endif \r
85         //sleep_disable();          // Disable Sleep on Wakeup\r
86         wdcounter++;\r
87         if (reset_indicator==1) reset_indicator++;\r
88         else if (reset_indicator==2) mode=0;\r
89 /*      if (timeout==2) {\r
90                 DIS_TIMER;\r
91                 EN_OWINT;\r
92                 mode=OWM_SLEEP;\r
93         }\r
94         timeout++;*/\r
95         //sleep_enable();           // Enable Sleep Mode\r
96 \r
97 }\r
98 \r
99 int testSW(void) {\r
100         uint8_t r;\r
101         DDRB&=~(1<<PORTB0);  //Eingang\r
102         __asm__ __volatile__ ("nop");\r
103         PORTB|=(1<<PORTB0); //Pullup\r
104         __asm__ __volatile__ ("nop");\r
105         __asm__ __volatile__ ("nop");\r
106         __asm__ __volatile__ ("nop");\r
107         __asm__ __volatile__ ("nop");\r
108         __asm__ __volatile__ ("nop");\r
109         r=PINB&(1<<PORTB0);\r
110         __asm__ __volatile__ ("nop");\r
111         PORTB&=~(1<<PORTB0);\r
112         __asm__ __volatile__ ("nop");\r
113         DDRB|=(1<<PORTB0);  //Eingang\r
114         return (r==0);\r
115         \r
116         \r
117 }\r
118 \r
119 \r
120 typedef union {\r
121         volatile uint8_t bytes[8];\r
122         struct {\r
123                 uint16_t temp;  //0\r
124                 uint8_t TH;  //2\r
125                 uint8_t TL;  //3\r
126                 uint8_t config;  //4\r
127                 uint8_t rrFF; //5\r
128                 uint8_t rr00; //6\r
129                 uint8_t rr10; //7\r
130         };\r
131 } pack1_t;\r
132 volatile pack1_t pack1;\r
133 \r
134 \r
135 \r
136 typedef union {\r
137         #if  defined(__AVR_ATtiny25__)\r
138         volatile uint8_t bytes[16];\r
139         #else\r
140         volatile uint8_t bytes[64];\r
141         #endif\r
142         struct {\r
143                 uint8_t status;  //1\r
144                 int16_t temp;  //2\r
145                 uint16_t voltage;  //4\r
146                 uint16_t current;  //6\r
147                 uint8_t threshold; //8\r
148                 \r
149                 uint8_t page1[8]; //9\r
150                 #if  defined(__AVR_ATtiny25__)\r
151                 #else\r
152                 uint8_t page2[8]; //17\r
153                 uint8_t page3[8]; //25\r
154                 uint8_t page4[8];  //33\r
155                 uint8_t page5[8];  //41\r
156                 uint8_t page6[8];  //49\r
157                 uint8_t page7[8];  //57\r
158                 //uint8_t crc;  //65\r
159                 #endif\r
160         };\r
161 } pack2_t;\r
162 volatile pack2_t pack2;\r
163 \r
164 \r
165 volatile double V,ktemp;\r
166 \r
167 double ADmess() {\r
168         ADMUX=0b10101101;  //PA2 + //PA1 - \r
169         ADCSRA|=(1<<ADSC);\r
170         while ((ADCSRA&(1<<ADSC)));\r
171         if (ADC==0) {\r
172                 ADMUX=0b10001101; //PA2 - //PA1 +\r
173                 ADCSRA|=(1<<ADSC);\r
174                 while ((ADCSRA&(1<<ADSC)));\r
175                 return -(double)ADC;\r
176         }\r
177         return (double)ADC;\r
178 }\r
179 \r
180 volatile int16_t am2302_temp;\r
181 volatile uint16_t am2302_hum;\r
182 \r
183 \r
184 uint8_t userRegister[1];\r
185 int16_t sRH,sT;\r
186 volatile double temperatureC,humidityRH;\r
187 volatile double l;\r
188 \r
189 uint8_t max_adr=0;\r
190 \r
191 int main(void){\r
192     //PRR|=(1<<PRADC);  // adc for save Power\r
193         pack1.temp=0x0550;\r
194         pack1.config=0x7F;\r
195         pack1.TH=75;\r
196         pack1.TL=70;\r
197         pack1.rrFF=0xFF;\r
198         pack1.rr00=0;\r
199         pack1.rr10=0x10;\r
200         PORTA=0xFF-(1<<PINA1)-(1<<PINA2);\r
201         PORTB=0xFF;\r
202         OWINIT();\r
203 \r
204         MCUCR &=~(1<<PUD); //All Pins Pullup...\r
205         MCUCR |=(1<<BODS);\r
206 \r
207         WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)\r
208         WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt\r
209         (1<<WDP2) | (1<<WDP1);   // Set Timeout to ~1 seconds\r
210         if (testSW()) {\r
211                 config_info2[5]=8;\r
212                 }else{\r
213                 config_info2[5]=7;\r
214         }\r
215         MCUSR=0;\r
216         USI_TWI_Master_Initialise();\r
217         SHT2x_SoftReset();\r
218         SHT2x_ReadUserRegister(userRegister);\r
219         //(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;\r
220         SHT2x_WriteUserRegister(userRegister); //write changed user reg\r
221         // --- measure humidity with "Hold Master Mode (HM)" ---\r
222         SHT2x_MeasurePoll(HUMIDITY, &sRH);\r
223         // --- measure temperature with "Polling Mode" (no hold master) ---\r
224         SHT2x_MeasurePoll(TEMP, &sT);\r
225         //-- calculate humidity and temperature --\r
226         //temperatureC = SHT2x_CalcTemperatureC(sT);\r
227         //humidityRH = SHT2x_CalcRH(sRH);\r
228         \r
229         gcontrol=1;\r
230         sei();\r
231         ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);\r
232         //DDRB|=(1<<PINB1);\r
233 \r
234         double ares[16],sum;\r
235         uint8_t par=0;\r
236         ares[0]=0;//ADmess();\r
237         for (par=1;par<16;par++) {\r
238                 ares[par]=ares[0];\r
239         }\r
240         par=0;\r
241     while(1)   {\r
242                 if ((gcontrol==2)||(gcontrol==3)) {\r
243                         wdcounter=1;\r
244                         gcontrol=0;\r
245                         \r
246                 }\r
247                 \r
248                 if (wdcounter>0) {\r
249                         if (par==0) {\r
250                                 //PORTB|=(1<<PINB1); //Dauer 440ms\r
251                                 SHT2x_MeasurePoll(HUMIDITY, &sRH);\r
252                                 // --- measure temperature with "Polling Mode" (no hold master) ---\r
253                                 SHT2x_MeasurePoll(TEMP, &sT);\r
254                                 //-- calculate humidity and temperature --\r
255                                 temperatureC = SHT2x_CalcTemperatureC(sT)*10.0-2;\r
256                                 humidityRH = SHT2x_CalcRH(sRH)*10.0+5;\r
257                                 if (humidityRH>1000) humidityRH=1000;\r
258                                 if (testSW()) {\r
259                                         am2302_hum= humidityRH*10.0;\r
260                                         am2302_temp=temperatureC*25.6;\r
261                                         //\r
262                                         config_info2[5]=8;\r
263                                 }else{\r
264                                         double hhum=(1.0546-0.000216*temperatureC)*(humidityRH);\r
265                                         //am2302_hum=0.318*hhum +76.0;\r
266                                         am2302_hum=0.31*hhum +80;\r
267                                         am2302_temp=temperatureC*25.6;\r
268                                         //am2302_temp=am2302_temp-45;\r
269                                         config_info2[5]=7;\r
270                                 }\r
271                         }\r
272                         ares[par]=ADmess();\r
273                         par++;\r
274                         if (par>15) par=0;\r
275                         wdcounter=0;\r
276                 }\r
277         \r
278                 if (gcontrol==1) {\r
279                         gcontrol=0;\r
280                         sum=0;\r
281                         for(uint8_t i=0;i<16;i++) {\r
282                                 sum+=ares[i];\r
283                         }\r
284                         V=sum/20.0/1024.0*1.12*1000.0/16.0;//Spannung in mV\r
285                         //V=sum/20.0/1024.0*1.01*1000.0/16.0;\r
286                         int16_t htemp;\r
287                         ktemp=gettemp_rs(V);\r
288                         //htemp=(ktemp*16)/10;\r
289                         htemp=(ktemp*16+temperatureC*1.6)/10;\r
290 \r
291                         int16_t w=htemp;\r
292                         int8_t t8=w>>4;\r
293                         int8_t af=0;\r
294                         if (t8>pack1.TH) af=1;\r
295                         if (t8<=pack1.TL) af=1; \r
296                         cli();\r
297                         pack1.temp=w;\r
298                         //pack.temp++;\r
299                         alarmflag=af;\r
300                         sei();                  \r
301                         EXTERN_SLEEP();         \r
302                 }\r
303                 \r
304 \r
305                 \r
306 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)\r
307                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))\r
308 #endif                  \r
309 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)\r
310                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))\r
311 #endif\r
312                           {\r
313 \r
314                         MCUCR|=(1<<SE)|(1<<SM1);\r
315                         MCUCR&=~(1<<ISC01);\r
316                 } else {\r
317                         MCUCR|=(1<<SE);\r
318                         MCUCR&=~(1<<SM1);\r
319                 }\r
320                 asm("SLEEP");\r
321    }\r
322 \r
323 \r
324 }