315c589ffc5fcd21248e4ac742de4661f8971706
[owSlave2.git] / DS18B20_DS2408 / DS18B20_DS2408.c
1 \r
2 // Copyright (c) 2018, 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/TWI_Master.h"\r
42 #include "../common/owSlave_tools.h"\r
43 #include "../common/I2C/SHT3x.h"\r
44 #include "../common/calibr.h"\r
45 \r
46 \r
47 #include <math.h>\r
48 \r
49 extern void OWINIT();\r
50 extern void EXTERN_SLEEP();\r
51 extern uint8_t stat_to_sample;\r
52 \r
53 uint8_t owid1[8]={0x28, 0xA3, 0xD9, 0x84, 0x00, 0x16, 0x05, 0x18};/**/\r
54 uint8_t owid2[8]={0x29, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x01, 0x73};/**/\r
55 uint8_t config_info1[26]={0x01,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x02,29,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};\r
56 uint8_t config_info2[26]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0,0,0,0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};\r
57 \r
58 OWST_EXTERN_VARS\r
59 \r
60 OWST_WDT_ISR\r
61 \r
62 \r
63 \r
64 typedef union {\r
65         volatile uint8_t bytes[8];\r
66         struct {\r
67                 int16_t temp;  //0\r
68                 uint8_t TH;  //2\r
69                 uint8_t TL;  //3\r
70                 uint8_t config;  //4\r
71                 uint8_t rrFF; //5\r
72                 uint8_t rr00; //6\r
73                 uint8_t rr10; //7\r
74         };\r
75 } pack1_t;\r
76 volatile pack1_t pack1;\r
77 \r
78 typedef union {\r
79         volatile uint8_t bytes[0x20];\r
80         struct {\r
81                 uint8_t PIO_Logic_State;     //         0088h\r
82                 uint8_t PIO_Output_Latch_State;\r
83                 uint8_t PIO_Activity_Latch_State;\r
84                 uint8_t Conditional_Search_Channel_Selection_Mask;\r
85                 uint8_t Conditional_Search_Channel_Polarity_Selection;\r
86                 uint8_t Status; //008D\r
87                 uint8_t FF1;\r
88                 uint8_t FF2;\r
89                 \r
90         };\r
91 } pack2_t;\r
92 volatile pack2_t pack2;\r
93 \r
94 uint8_t values[10];\r
95 uint8_t ap=1;\r
96 \r
97 uint8_t crc8_f() {\r
98         uint8_t lscrc=0x0;\r
99         for(uint8_t i=0;i<5;i++) {\r
100                 uint8_t v=values[i];\r
101                 //if (v==0) v=0xFF;\r
102                 uint8_t bit=1;\r
103                 uint8_t lb;\r
104                 for(uint8_t j=0;j<8;j++) {\r
105                         if ((v&bit)==bit) lb=1; else lb=0;\r
106                         if ((lscrc&1)!=lb)      lscrc=(lscrc>>1)^0x8c; else     lscrc=(lscrc>>1);\r
107                         bit=bit*2;\r
108                 \r
109                 \r
110                 }\r
111         }\r
112         return lscrc;\r
113 }\r
114 \r
115 \r
116 //Umstellung\r
117 \r
118 //rh=(T-25)*(0,01+0,00008*x)-2,0468+0,0367*x-0,0000015955*x*x\r
119 //d1 for 3V\r
120 #define d1 -39.7  \r
121 #define d2 0.01\r
122 inline uint16_t calcSHT75_T(double real_t) {\r
123         return (real_t-d1)/d2;\r
124 }\r
125 \r
126 inline uint16_t calcSHT75RH_lin(double real_RHlin) {\r
127         return 11501.1-0.280297*sqrt(1667284153.0-7977500.0*real_RHlin);\r
128 }\r
129 \r
130 inline double calcSHT75H_tcorr(double real_t,double real_RHtrue) {\r
131         return real_RHtrue-(real_t-25)*(0.01+0.00008*calcSHT75RH_lin(real_RHtrue));\r
132 \r
133 }\r
134 \r
135 \r
136 double T=20.0;\r
137 double RH=60;\r
138 \r
139 int main(void){\r
140         OWST_INIT_USI_ON;\r
141         pack2.FF1=0xFF;\r
142         pack2.FF2=0xFF;\r
143          //0x0E 0x19 0x48 0x00\r
144          if (RH<8) RH=8;\r
145          uint16_t lt=calcSHT75_T(T);\r
146                 double lfc=calcSHT75H_tcorr(T,RH);\r
147          uint16_t lf=calcSHT75RH_lin(lfc);\r
148         values[0]=0x00;\r
149         values[1]=lt&0xFF; if (values[1]==0) values[1]=1;\r
150         values[2]=lt>>8; if (values[2]==0) values[2]=1;\r
151         values[3]=lf&0xFF; if (values[3]==0) values[3]=1;\r
152         values[4]=lf>>8; if (values[4]==0) values[4]=1;\r
153         values[5]=0x5D; \r
154         values[6]=0x00;\r
155         values[7]=0x00;\r
156         values[5]=crc8_f();     \r
157         \r
158         OWINIT();\r
159 \r
160         TWI_Master_Initialise();\r
161         initSHT3x(0);\r
162         _delay_ms(100);\r
163 \r
164 \r
165         getSHT3xHumTemp(0,&T,&RH);\r
166         OWST_WDR_CONFIG8;\r
167         sei();\r
168         stat_to_sample=0x55;\r
169         while (1) {\r
170                 //stat_to_sample=0;\r
171                 if (reset_indicator) {\r
172                 //      ap=0;\r
173                 //      stat_to_sample=0;\r
174                 //      reset_indicator=0;\r
175                 }\r
176                 if (wdcounter>3) {\r
177                         \r
178                         wdcounter=0;\r
179                         RH=RH+0.2;\r
180                         getSHT3xHumTemp(0,&T,&RH);\r
181                         //pack1.temp=T*16.0;\r
182                         lt=calcSHT75_T(T);\r
183                         lfc=calcSHT75H_tcorr(T,RH);\r
184                         lf=calcSHT75RH_lin(lfc);\r
185                 values[0]=0x00;\r
186                 values[1]=lt&0xFF; if (values[1]==0) values[1]=1;\r
187                 values[2]=lt>>8; if (values[2]==0) values[2]=1;\r
188                 values[3]=lf&0xFF; if (values[3]==0) values[3]=1;\r
189                 values[4]=lf>>8; if (values[4]==0) values[4]=1;\r
190                  values[5]=crc8_f();\r
191                  if (values[5]==0) values[3]=values[3]+1;  //CRC darf nicht 0 sein ... warum auch immer\r
192                  values[5]=crc8_f();\r
193                 }\r
194                 pack2.Status|=0x80;\r
195                 if (gcontrol&1) {\r
196                         uint8_t bb=1;\r
197                         for(uint8_t i=0;i<8;i++) {\r
198                                 if ((pack2.PIO_Logic_State&bb)!=(pack2.PIO_Output_Latch_State&bb)) pack2.PIO_Activity_Latch_State|=bb;\r
199                                 bb=bb*2;\r
200                         }\r
201             pack2.PIO_Logic_State=pack2.PIO_Output_Latch_State;\r
202                         gcontrol&=~0x01;\r
203                 }\r
204                 if (gcontrol&2) {\r
205                         pack2.PIO_Activity_Latch_State=0;\r
206             gcontrol&=~0x02;\r
207                 }\r
208                 if (gcontrol&4) {\r
209                         stat_to_sample=values[ap];\r
210                         ap++;           \r
211                         if (ap>5) {\r
212                                         ap=0;\r
213                         }\r
214                         gcontrol&=~0x04;\r
215                 }               \r
216                 if (gcontrol&8) {\r
217                         ap=1;\r
218                         stat_to_sample=values[ap];\r
219                         ap++;\r
220                         //if (ap>5) ap=1;\r
221                         gcontrol&=~0x08;\r
222                 } \r
223                 if ((gcontrol&16)==16) {\r
224                         \r
225         \r
226                         uint16_t w=T*16.0;\r
227                         uint8_t t8=w>>4;\r
228                         uint8_t af=0;\r
229                         if (t8>pack1.TH) af=1;\r
230                         if (t8<=pack1.TL) af=1;\r
231                         cli();\r
232                         pack1.temp=w;\r
233                         //pack.temp++;\r
234                         alarmflag=af;\r
235                         sei();\r
236                         gcontrol=0;\r
237                         EXTERN_SLEEP();\r
238                 }\r
239 \r
240                 OWST_MAIN_END\r
241         }\r
242 \r
243 \r
244 \r
245 }\r