Many changes from 2018
[owSlave2.git] / DS2438_SHT2X_DS2438_LPS225HB / DS2438_DS2438.c
1 // Copyright (c) 2018, Tobias Mueller tm(at)tm3d.de\r
2 // All rights reserved.\r
3 //\r
4 // Redistribution and use in source and binary forms, with or without\r
5 // modification, are permitted provided that the following conditions are\r
6 // met:\r
7 //\r
8 //  * Redistributions of source code must retain the above copyright\r
9 //    notice, this list of conditions and the following disclaimer.\r
10 //  * Redistributions in binary form must reproduce the above copyright\r
11 //    notice, this list of conditions and the following disclaimer in the\r
12 //    documentation and/or other materials provided with the\r
13 //    distribution.\r
14 //  * All advertising materials mentioning features or use of this\r
15 //    software must display the following acknowledgement: This product\r
16 //    includes software developed by tm3d.de and its contributors.\r
17 //  * Neither the name of tm3d.de nor the names of its contributors may\r
18 //    be used to endorse or promote products derived from this software\r
19 //    without specific prior written permission.\r
20 //\r
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
32 \r
33 //!!!!!Max Program size 7551 Byte\r
34 \r
35 #define F_CPU 8000000UL\r
36 #include <avr/io.h>\r
37 #include <avr/interrupt.h>\r
38 #include <util/delay.h>  \r
39 #include <avr/wdt.h>\r
40 #include <avr/sleep.h>\r
41 #include <avr/pgmspace.h>\r
42 #include "../common/I2C/USI_TWI_Master.h"\r
43 #include "../common/I2C/LPS225HB.h"\r
44 #include "../common/I2C/SHT2xV2.h"\r
45 #include "../common/calibr.h"\r
46 \r
47 extern void OWINIT(void);\r
48 extern void EXTERN_SLEEP(void);\r
49 \r
50 \r
51 \r
52 volatile uint8_t owid1[8]={0x26, 0x3B, 0xDA, 0x84, 0x00, 0x00, 0x03, 0xA2};/**/\r
53 volatile uint8_t owid2[8]={0x26, 0x3C, 0xDA, 0x84, 0x00, 0x00, 0x03, 0x27};/**/\r
54 volatile uint8_t config_info1[26]={0x02,23, 0x05,0x08, 0x2,20, 0x00,0x00, 0x02,29,0x00,29,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};      \r
55 volatile uint8_t config_info2[26]={0x01,0x06, 0x05,0x08, 0x04,0x07, 0,0, 0x02,0x07,0x00,0x07,0,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};      \r
56         \r
57 #if (owid>128) \r
58 #error "Variable not correct"\r
59 #endif\r
60 \r
61 extern uint8_t mode;\r
62 extern uint8_t gcontrol;\r
63 extern uint8_t reset_indicator;\r
64 extern uint8_t alarmflag;\r
65 volatile uint8_t wdcounter=1;\r
66 \r
67 \r
68 typedef union {\r
69         #if  defined(__AVR_ATtiny25__)\r
70         volatile uint8_t bytes[16];\r
71         #else\r
72         volatile uint8_t bytes[64];\r
73         #endif\r
74         struct {\r
75                 uint8_t status;  //1\r
76                 int16_t temp;  //2\r
77                 uint16_t voltage;  //4\r
78                 int16_t current;  //6\r
79                 uint8_t threshold; //8\r
80                 union {\r
81                 uint8_t page1[8]; //9\r
82                 struct {\r
83                         uint32_t etm;\r
84                         uint8_t ica;\r
85                         uint16_t offset;\r
86                         uint8_t f1;\r
87                 };\r
88                 };\r
89                 #if  defined(__AVR_ATtiny25__)\r
90                 #else\r
91                 union {\r
92                                 uint8_t page2[8]; //17\r
93                                 struct  {\r
94                                 uint32_t dis; \r
95                                 uint32_t eoc;\r
96                                 };\r
97                                 };\r
98                         \r
99                 uint8_t page3[8]; //25\r
100                 \r
101                 uint8_t page4[8];  //33\r
102                 uint8_t page5[8];  //41\r
103                 uint8_t page6[8];  //49\r
104                 uint8_t page7[8];  //57\r
105                 \r
106                 #endif\r
107         };\r
108 } pack1_t;\r
109 volatile pack1_t pack1;\r
110 \r
111 \r
112 \r
113 \r
114 typedef union {\r
115         #if  defined(__AVR_ATtiny25__)\r
116         volatile uint8_t bytes[16];\r
117         #else\r
118         volatile uint8_t bytes[64];\r
119         #endif\r
120         struct {\r
121                 uint8_t status;  //1\r
122                 int16_t temp;  //2\r
123                 uint16_t voltage;  //4\r
124                 int16_t current;  //6\r
125                 uint8_t threshold; //8\r
126                 \r
127                 uint8_t page1[8]; //9\r
128                 #if  defined(__AVR_ATtiny25__)\r
129                 #else\r
130                 uint8_t page2[8]; //17\r
131                 uint8_t page3[8]; //25\r
132                 \r
133                 uint8_t page4[8];  //33\r
134                 uint8_t page5[8];  //41\r
135                 uint8_t page6[8];  //49\r
136                 uint8_t page7[8];  //57\r
137                 \r
138                 #endif\r
139         };\r
140 } pack2_t;\r
141 volatile pack2_t pack2;\r
142 \r
143 \r
144 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)\r
145 ISR(WATCHDOG_vect) {\r
146 #else\r
147 ISR(WDT_vect) {\r
148 #endif\r
149         wdcounter++;\r
150         if (reset_indicator==1) reset_indicator++;\r
151         else if (reset_indicator==2) mode=0;\r
152 }\r
153 \r
154 \r
155 \r
156 volatile int16_t DS2438_1_TEMP;\r
157 volatile uint16_t DS2438_1_VAD;\r
158 volatile int16_t DS2438_2_TEMP;\r
159 volatile uint16_t DS2438_2_VAD;\r
160 \r
161 \r
162 uint8_t userRegister[1];\r
163 int16_t sRH,sT;\r
164 double temperatureC,humidityRH,hhum;\r
165 double l;\r
166 uint32_t P;\r
167 int16_t t;\r
168 \r
169 int testSW(void) {\r
170         uint8_t r;\r
171         DDRB&=~(1<<PORTB0);  //Eingang\r
172          __asm__ __volatile__ ("nop"); \r
173          PORTB|=(1<<PORTB0); //Pullup\r
174          __asm__ __volatile__ ("nop"); \r
175          __asm__ __volatile__ ("nop"); \r
176          __asm__ __volatile__ ("nop"); \r
177          __asm__ __volatile__ ("nop"); \r
178          __asm__ __volatile__ ("nop"); \r
179          r=PINB&(1<<PORTB0);\r
180          __asm__ __volatile__ ("nop"); \r
181          PORTB&=~(1<<PORTB0); \r
182          __asm__ __volatile__ ("nop"); \r
183         DDRB|=(1<<PORTB0);  //Eingang\r
184         return (r==0);  //Offen mal HIH4030\r
185          \r
186          \r
187 }\r
188 \r
189 \r
190 int main(void){\r
191     PRR|=(1<<PRADC);  // adc for save Power\r
192         PORTA=0xFF;\r
193         PORTB=0xFF-(1<<PORTB0); //Schalter kann gegen Masse sein und zieht dann immer Strom\r
194         DDRB|=(1<<PORTB0); //Als Ausgang und 0\r
195         OWINIT();\r
196         DDRB|=(1<<PINB1);//Ausgang und 1\r
197         DDRA|=(1<<PINA0);\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<<WDP3) | (1<<WDP0);   // Set Timeout to ~8 seconds\r
202         \r
203         MCUSR=0;\r
204 \r
205         pack2.page3[0]=0xF1; //Luftfeuchte\r
206         pack1.page3[0]=0xF4; //Spannung\r
207 \r
208 \r
209         if (testSW()) {\r
210                 config_info2[5]=12;\r
211         }else{\r
212                 config_info2[5]=7;\r
213         }\r
214 \r
215         USI_TWI_Master_Initialise();\r
216         initSHT2x();\r
217         _delay_ms(10);\r
218          LPS225HB_Init();\r
219         gcontrol=1;\r
220         sei();\r
221     while(1)   {\r
222                 if (gcontrol) {\r
223                         //wdcounter=3;\r
224                         gcontrol=0;\r
225                                 \r
226                 }\r
227                 if (wdcounter>3) {  \r
228                         getSHT2xHumTemp(&temperatureC,&humidityRH);\r
229                         double RH=calibr_hum(temperatureC,-0.2,humidityRH)*10.0;\r
230                         double TC =temperatureC *10.0-2;\r
231 \r
232 \r
233                         if (testSW()) {\r
234                                  DS2438_2_VAD=RH;\r
235                                  DS2438_2_TEMP=TC*25.6;\r
236                                  \r
237                                  config_info2[5]=12;    //10V = 100%\r
238                         }else{\r
239                                 hhum=(1.0546-0.000216*TC)*(RH);\r
240                                 //am2302_hum=0.318*hhum +76.0;\r
241                                 DS2438_2_VAD=0.31*hhum +80;\r
242                                 DS2438_2_TEMP=TC*25.6;\r
243                                 config_info2[5]=7;\r
244                         }\r
245                         //DS2438_1_TEMP=DS2438_2_TEMP;\r
246 \r
247                         LPS225HB_Readi(&t,&P) ;\r
248                         P=P-700*(uint32_t)(4096);\r
249                         P=((double)P/102.4);\r
250                         //cli();pack.A=ADC;sei();\r
251                         //cli();DS2438_1_TEMP=P;sei();\r
252                         DS2438_1_TEMP=P;\r
253                         P=P/20;\r
254                         DS2438_1_VAD=P;\r
255 \r
256                 //      gcontrol=1;\r
257                         wdcounter=0;\r
258                         \r
259                 }\r
260         \r
261                 \r
262 \r
263                 \r
264 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)\r
265                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))\r
266 #endif                  \r
267 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)\r
268                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))\r
269 #endif\r
270                           {\r
271 \r
272                         MCUCR|=(1<<SE)|(1<<SM1);\r
273                         MCUCR&=~(1<<ISC01);\r
274                 } else {\r
275                         MCUCR|=(1<<SE);\r
276                         MCUCR&=~(1<<SM1);\r
277                 }\r
278                 asm("SLEEP");\r
279    }\r
280 \r
281 \r
282 }\r