Change Time Values so it works for all Adapters
[owSlave2.git] / DS18B20_Thermocouble / DS18B20.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
42 extern void OWINIT();
43
44 //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};
45 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};
46         double gettemp_rs(double V) {
47                 uint8_t iv=(uint8_t)(V);
48                 float t0=pgm_read_float(&(j_rs[iv]));
49                 float t1=pgm_read_float(&(j_rs[iv+1]));
50                 return t0+(t1-t0)/1*(V-iv);
51         }
52
53
54 uint8_t owid[8]={0x28, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x02, 0xAC};/**/
55         
56 #if (owid>128) 
57 #error "Variable not correct"
58 #endif
59
60 extern uint8_t mode;
61 extern uint8_t gcontrol;
62 extern uint8_t reset_indicator;
63 extern uint8_t alarmflag;
64
65
66 volatile uint8_t wdcounter;
67
68
69 typedef union {
70         volatile uint8_t bytes[8];
71         struct {
72                 uint16_t temp;  //0
73                 uint8_t TH;  //2
74                 uint8_t TL;  //3
75                 uint8_t config;  //4
76                 uint8_t rrFF; //5
77                 uint8_t rr00; //6
78                 uint8_t rr10; //7
79         };
80 } pack_t;
81 volatile pack_t pack;
82
83
84
85
86 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
87 ISR(WATCHDOG_vect) {
88 #else
89 ISR(WDT_vect) {
90 #endif 
91         //sleep_disable();          // Disable Sleep on Wakeup
92         wdcounter++;
93         if (reset_indicator==1) reset_indicator++;
94         else if (reset_indicator==2) mode=0;
95 /*      if (timeout==2) {
96                 DIS_TIMER;
97                 EN_OWINT;
98                 mode=OWM_SLEEP;
99         }
100         timeout++;*/
101         //sleep_enable();           // Enable Sleep Mode
102
103 }
104
105
106 #define OWM_PORT PORTA
107 #define OWM_PIN PINA
108 #define OWM_PINN PINA0
109 #define OWM_DD DDRA
110
111 #define OWM_SET_LOW OWM_PORT&=~(1<<OWM_PINN);OWM_DD|=(1<<OWM_PINN)
112 #define OWM_SET_HIGH OWM_DD&=~(1<<OWM_PINN);OWM_PORT|=(1<<OWM_PINN)
113
114 #define OWM_IS_LOW ((OWM_PIN & (1<<OWM_PINN))==0)
115
116
117 void owm_init() {
118         OWM_PORT|=(1<<OWM_PINN); //PULL UP
119         OWM_DD&=~(1<<OWM_PINN);
120 }
121
122 #define owm_delay(us1)  _delay_us(us1)
123
124 uint8_t owm_reset() {
125         OWM_SET_LOW;
126         owm_delay(480);
127         OWM_SET_HIGH;
128         owm_delay(60);
129         if (OWM_IS_LOW) {owm_delay(420); return 1;} else {owm_delay(420); return 0;}
130         
131         
132 }
133
134 void owm_rw(uint8_t *b) {
135         uint8_t i;
136         uint8_t pp=1;
137         for(i=0;i<8;i++) {
138                 if (pp&b[0]) {
139                         OWM_SET_LOW;
140                         owm_delay(6);
141                         OWM_SET_HIGH;
142                         owm_delay(9);
143                         if (OWM_IS_LOW) {
144                                 b[0]&=~pp;
145                         }
146                         owm_delay(80-6-9);
147                         
148                         } else {
149                         OWM_SET_LOW;
150                         owm_delay(60);
151                         OWM_SET_HIGH;
152                         owm_delay(20);
153                 }
154                 pp=(pp<<1);
155         }
156 }
157
158 void owm_block(uint8_t count, uint8_t *buf){
159         uint8_t i;
160         for(i=0;i<count;i++) {
161                 owm_rw(buf+i);
162         }
163 }
164
165 inline int16_t ow_fconvert(uint8_t b1, uint8_t b2) {
166         int16_t tsht;
167         tsht=b1  |((int)b2<<8);
168         if (b2 & 0x080)
169         tsht |= 0xFFFFF0000;
170         return tsht;
171 }
172 volatile double V,ktemp;
173
174 uint16_t ADmess() {
175          ADMUX=0b10001101;
176          ADCSRA|=(1<<ADSC);
177          while ((ADCSRA&(1<<ADSC)));
178         return ADC;
179 }
180
181 int main(void){
182     //PRR|=(1<<PRUSI)|(1<<PRADC);  //Switch off usi and adc for save Power
183         pack.temp=0x0550;
184         pack.config=0x7F;
185         pack.TH=75;
186         pack.TL=70;
187         pack.rrFF=0xFF;
188         pack.rr00=0;
189         pack.rr10=0x10;
190         PORTA=0xFF-(1<<PINA1)-(1<<PINA2);
191         PORTB=0xFF;
192         OWINIT();
193
194         MCUCR &=~(1<<PUD); //All Pins Pullup...
195         MCUCR |=(1<<BODS);
196
197         WDTCSR |= ((1<<WDCE) );   // Enable the WD Change Bit//| (1<<WDE)
198         WDTCSR |=   (1<<WDIE) |              // Enable WDT Interrupt
199         (1<<WDP2) | (1<<WDP1);   // Set Timeout to ~1 seconds
200         MCUSR=0;
201         uint8_t block[13];
202         sei();
203         ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
204         owm_init();
205         owm_reset();
206         block[0]=0xCC;
207         block[1]=0x4E;
208         block[2]=0;
209         block[3]=0;
210         block[4]=0x1F;
211         owm_block(5,block);
212         owm_reset();
213         block[0]=0x33;
214         for(uint8_t i=1;i<9;i++) {
215                 block[i]=0xFF;
216         }
217         owm_block(9,block);
218         
219         if (block[1]==0x28) {  //DS18B20 angeschlossen
220                 for(uint8_t i=0;i<8;i++) {
221                         owid[i]=block[i+1];
222                 }
223                 while(EECR & (1<<EEPE));
224                 EEAR=E2END-7;
225                 EECR|=(1<EERE);
226                 if (EEDR!=0x28) { //Wenn keine ID im Eeprom uebernimm es
227                         for(uint8_t a=0;a<8;a++) {
228                                 while(EECR & (1<<EEPE));
229                                 EECR = (0<<EEPM1)|(0<<EEPM0);
230                                 EEAR = E2END-7+a;
231                                 EEDR = block[a+1];
232                                 EECR |= (1<<EEMPE);
233                                 EECR |= (1<<EEPE);
234                         }
235                 }
236         }
237         
238         uint16_t ares[16],sum;
239         uint8_t par=0;
240         ares[0]=0;//ADmess();
241         for (par=1;par<16;par++) {
242                 ares[par]=ares[0];
243         }
244         par=0;
245         wdcounter=0;
246         gcontrol=1;
247
248     while(1)   {
249                 if (wdcounter>0) {
250                         ares[par]=ADmess();
251                         par++;
252                         if (par>15) par=0;
253                         wdcounter=0;
254                 }
255                 if (gcontrol) {
256                         PORTB|=(1<<PORTB0);
257                         sum=0;
258                         for(uint8_t i=0;i<16;i++) {
259                                 sum+=ares[i];
260                         }
261                         V=sum/20.0/1024.0*1.174*1000.0/16.0;
262                         //V=sum/20.0/1024.0*1.01*1000.0/16.0;
263                         ktemp=gettemp_rs(V);
264                         owm_reset();
265                         block[0]=0xCC;
266                         block[1]=0x44;
267                         owm_block(2,block);
268                         _delay_ms(100);
269                         owm_reset();
270                         block[0]=0xCC;
271                         block[1]=0xBE;
272                         for(uint8_t i=0;i<9;i++) block[i+2]=0xFF;
273                         owm_block(11,block);
274                         uint16_t htemp;
275                         if (PINB&(1<<PINB0)) {
276                                 htemp=(ktemp*16+(block[2]|(block[3]<<8)))/10;
277                         } else {
278                                 
279                                 htemp=ktemp*16+(block[2]|(block[3]<<8));
280                         }
281                         uint8_t t8=pack.temp>>4;
282                         uint8_t af=0;
283                         if (t8>pack.TH) af=1;
284                         if (t8<=pack.TL) af=1;
285                         cli();
286                         pack.temp=htemp;
287                         alarmflag=af;
288                         sei();
289                         gcontrol=0;
290                         PORTB&=~(1<<PORTB0);
291                 }
292
293                 
294 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
295                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
296 #endif                  
297 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
298                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
299 #endif
300                           {
301 //                      CLKPR=(1<<CLKPCE);
302         //              CLKPR=(1<<CLKPS2); /*0.5Mhz*/
303 //                      PORTB&=~(1<<PINB1);
304                         MCUCR|=(1<<SE)|(1<<SM1);
305                         MCUCR&=~(1<<ISC01);
306                 } else {
307                         MCUCR|=(1<<SE);
308                         MCUCR&=~(1<<SM1);
309                 }
310                 asm("SLEEP");
311    }
312
313
314 }