New Devices
[owSlave2.git] / DS18B20_Thermocouble_DS2438_SHT / DS18B20_DS2438.c
1
2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
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
14 //    distribution.
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.
21 //
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.
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 #include "../common/I2C/USI_TWI_Master.h"
42 #include "../common/I2C/SHT2x.h"
43
44
45 extern void OWINIT();
46 extern void EXTERN_SLEEP();
47
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};
49 //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};
50 double gettemp_rs(double V) {
51         uint8_t iv=(uint8_t)(V);
52         float t0=pgm_read_float(&(k_rs[iv]));
53         float t1=pgm_read_float(&(k_rs[iv+1]));
54         return t0+(t1-t0)/1*(V-iv);
55 }
56
57
58
59
60
61 uint8_t owid1[8]={0x28, 0xA6, 0xD9, 0x84, 0x00, 0x00, 0x03, 0x68};/**/
62 uint8_t owid2[8]={0x26, 0xA7, 0xD9, 0x84, 0x00, 0x00, 0x03, 0x20};/**/
63 uint8_t config_info1[16]={0x01,0x02 ,0x00,0x00, 0x00,0x00, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 
64 uint8_t config_info2[16]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0x00,0x00, 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 
65         
66 #if (owid>128) 
67 #error "Variable not correct"
68 #endif
69
70 extern uint8_t mode;
71 extern uint8_t gcontrol;
72 extern uint8_t reset_indicator;
73 extern uint8_t alarmflag;
74 volatile uint8_t wdcounter=5;
75
76
77 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
78 ISR(WATCHDOG_vect) {
79 #else
80 ISR(WDT_vect) {
81 #endif 
82         //sleep_disable();          // Disable Sleep on Wakeup
83         wdcounter++;
84         if (reset_indicator==1) reset_indicator++;
85         else if (reset_indicator==2) mode=0;
86 /*      if (timeout==2) {
87                 DIS_TIMER;
88                 EN_OWINT;
89                 mode=OWM_SLEEP;
90         }
91         timeout++;*/
92         //sleep_enable();           // Enable Sleep Mode
93
94 }
95
96 int testSW(void) {
97         uint8_t r;
98         DDRB&=~(1<<PORTB0);  //Eingang
99         __asm__ __volatile__ ("nop");
100         PORTB|=(1<<PORTB0); //Pullup
101         __asm__ __volatile__ ("nop");
102         __asm__ __volatile__ ("nop");
103         __asm__ __volatile__ ("nop");
104         __asm__ __volatile__ ("nop");
105         __asm__ __volatile__ ("nop");
106         r=PINB&(1<<PORTB0);
107         __asm__ __volatile__ ("nop");
108         PORTB&=~(1<<PORTB0);
109         __asm__ __volatile__ ("nop");
110         DDRB|=(1<<PORTB0);  //Eingang
111         return (r==0);
112         
113         
114 }
115
116
117 typedef union {
118         volatile uint8_t bytes[8];
119         struct {
120                 uint16_t temp;  //0
121                 uint8_t TH;  //2
122                 uint8_t TL;  //3
123                 uint8_t config;  //4
124                 uint8_t rrFF; //5
125                 uint8_t rr00; //6
126                 uint8_t rr10; //7
127         };
128 } pack1_t;
129 volatile pack1_t pack1;
130
131
132
133 typedef union {
134         #if  defined(__AVR_ATtiny25__)
135         volatile uint8_t bytes[16];
136         #else
137         volatile uint8_t bytes[64];
138         #endif
139         struct {
140                 uint8_t status;  //1
141                 int16_t temp;  //2
142                 uint16_t voltage;  //4
143                 uint16_t current;  //6
144                 uint8_t threshold; //8
145                 
146                 uint8_t page1[8]; //9
147                 #if  defined(__AVR_ATtiny25__)
148                 #else
149                 uint8_t page2[8]; //17
150                 uint8_t page3[8]; //25
151                 uint8_t page4[8];  //33
152                 uint8_t page5[8];  //41
153                 uint8_t page6[8];  //49
154                 uint8_t page7[8];  //57
155                 //uint8_t crc;  //65
156                 #endif
157         };
158 } pack2_t;
159 volatile pack2_t pack2;
160
161
162 volatile double V,ktemp;
163
164 uint16_t ADmess() {
165         ADMUX=0b10101101;
166         ADCSRA|=(1<<ADSC);
167         while ((ADCSRA&(1<<ADSC)));
168         return ADC;
169 }
170
171 volatile int16_t am2302_temp;
172 volatile uint16_t am2302_hum;
173
174
175 uint8_t userRegister[1];
176 int16_t sRH,sT;
177 volatile double temperatureC,humidityRH;
178 volatile double l;
179
180 uint8_t max_adr=0;
181
182 int main(void){
183     //PRR|=(1<<PRADC);  // adc for save Power
184         pack1.temp=0x0550;
185         pack1.config=0x7F;
186         pack1.TH=75;
187         pack1.TL=70;
188         pack1.rrFF=0xFF;
189         pack1.rr00=0;
190         pack1.rr10=0x10;
191         PORTA=0xFF-(1<<PINA1)-(1<<PINA2);
192         PORTB=0xFF;
193         OWINIT();
194
195         MCUCR &=~(1<<PUD); //All Pins Pullup...
196         MCUCR |=(1<<BODS);
197
198         WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)
199         WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt
200         (1<<WDP2) | (1<<WDP1);   // Set Timeout to ~1 seconds
201         if (testSW()) {
202                 config_info2[5]=8;
203                 }else{
204                 config_info2[5]=7;
205         }
206         MCUSR=0;
207         USI_TWI_Master_Initialise();
208         SHT2x_SoftReset();
209         SHT2x_ReadUserRegister(userRegister);
210         //(userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT;
211         SHT2x_WriteUserRegister(userRegister); //write changed user reg
212         // --- measure humidity with "Hold Master Mode (HM)" ---
213         SHT2x_MeasurePoll(HUMIDITY, &sRH);
214         // --- measure temperature with "Polling Mode" (no hold master) ---
215         SHT2x_MeasurePoll(TEMP, &sT);
216         //-- calculate humidity and temperature --
217         //temperatureC = SHT2x_CalcTemperatureC(sT);
218         //humidityRH = SHT2x_CalcRH(sRH);
219         
220         gcontrol=1;
221         sei();
222         ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
223         //DDRB|=(1<<PINB1);
224
225         uint16_t ares[16],sum;
226         uint8_t par=0;
227         ares[0]=0;//ADmess();
228         for (par=1;par<16;par++) {
229                 ares[par]=ares[0];
230         }
231         par=0;
232     while(1)   {
233                 if ((gcontrol==2)||(gcontrol==3)) {
234                         wdcounter=1;
235                         gcontrol=0;
236                         
237                 }
238                 
239                 if (wdcounter>0) {
240                         if (par==0) {
241                                 //PORTB|=(1<<PINB1); //Dauer 440ms
242                                 SHT2x_MeasurePoll(HUMIDITY, &sRH);
243                                 // --- measure temperature with "Polling Mode" (no hold master) ---
244                                 SHT2x_MeasurePoll(TEMP, &sT);
245                                 //-- calculate humidity and temperature --
246                                 temperatureC = SHT2x_CalcTemperatureC(sT)*10.0-2;
247                                 humidityRH = SHT2x_CalcRH(sRH)*10.0+5;
248                                 if (humidityRH>1000) humidityRH=1000;
249                                 if (testSW()) {
250                                         am2302_hum= humidityRH*10.0;
251                                         am2302_temp=temperatureC*25.6;
252                                         //
253                                         config_info2[5]=8;
254                                 }else{
255                                         double hhum=(1.0546-0.000216*temperatureC)*(humidityRH);
256                                         //am2302_hum=0.318*hhum +76.0;
257                                         am2302_hum=0.31*hhum +80;
258                                         am2302_temp=temperatureC*25.6;
259                                         //am2302_temp=am2302_temp-45;
260                                         config_info2[5]=7;
261                                 }
262                         }
263                         ares[par]=ADmess();
264                         par++;
265                         if (par>15) par=0;
266                         wdcounter=0;
267                 }
268         
269                 if (gcontrol==1) {
270                         gcontrol=0;
271                         sum=0;
272                         for(uint8_t i=0;i<16;i++) {
273                                 sum+=ares[i];
274                         }
275                         V=sum/20.0/1024.0*1.12*1000.0/16.0;
276                         //V=sum/20.0/1024.0*1.01*1000.0/16.0;
277                         uint16_t htemp;
278                         ktemp=gettemp_rs(V);
279                         //htemp=(ktemp*16)/10;
280                         htemp=(ktemp*16+temperatureC*1.6)/10;
281
282                         uint16_t w=htemp;
283                         uint8_t t8=w>>4;
284                         uint8_t af=0;
285                         if (t8>pack1.TH) af=1;
286                         if (t8<=pack1.TL) af=1; 
287                         cli();
288                         pack1.temp=w;
289                         //pack.temp++;
290                         alarmflag=af;
291                         sei();                  
292                         EXTERN_SLEEP();         
293                 }
294                 
295
296                 
297 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
298                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
299 #endif                  
300 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
301                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
302 #endif
303                           {
304
305                         MCUCR|=(1<<SE)|(1<<SM1);
306                         MCUCR&=~(1<<ISC01);
307                 } else {
308                         MCUCR|=(1<<SE);
309                         MCUCR&=~(1<<SM1);
310                 }
311                 asm("SLEEP");
312    }
313
314
315 }