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