8cfc5a68dbaf4befbd12f315e9f7e31fe308f347
[owSlave2.git] / DS18B20_BMP280 / DS18B20_BMP280.c
1 // Copyright (c) 2017, 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 #define F_CPU 8000000UL\r
34 #include <avr/io.h>\r
35 #include <avr/interrupt.h>\r
36 #include <util/delay.h>\r
37 #include <avr/wdt.h>\r
38 #include <avr/sleep.h>\r
39 #include <avr/pgmspace.h>\r
40 #include "../common/I2C/USI_TWI_Master.h"\r
41 #include "../common/I2C/BMP280.h"\r
42 #include "../common/owSlave_tools.h"\r
43 \r
44 \r
45 #define OWST_EXTERN_VARS\r
46 \r
47 \r
48 \r
49 \r
50 uint8_t owid[8]={0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x40};/**/\r
51 uint8_t config_info[26]={0x02,0x03, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x02,14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};\r
52         \r
53 #if (owid>128) \r
54 #error "Variable not correct"\r
55 #endif\r
56 \r
57 \r
58 \r
59 \r
60 \r
61 \r
62 typedef union {\r
63         volatile uint8_t bytes[8];\r
64         struct {\r
65                 uint16_t temp;  //0\r
66                 uint8_t TH;  //2\r
67                 uint8_t TL;  //3\r
68                 uint8_t config;  //4\r
69                 uint8_t rrFF; //5\r
70                 uint8_t rr00; //6\r
71                 uint8_t rr10; //7\r
72         };\r
73 } pack_t;\r
74 volatile pack_t pack;\r
75 \r
76 \r
77 \r
78 \r
79 \r
80 \r
81 \r
82 \r
83 int main(void){\r
84     OWST_INIT_USI_ON\r
85         \r
86         pack.temp=0x0550;\r
87         pack.config=0x7F;\r
88         pack.TH=75;\r
89         pack.TL=70;\r
90         pack.rrFF=0xFF;\r
91         pack.rr00=0;\r
92         pack.rr10=0x10;\r
93         \r
94         OWINIT();\r
95 \r
96         OWST_EN_PULLUP\r
97         USI_TWI_Master_Initialise();\r
98         _delay_ms(500);\r
99         bmp280Init();\r
100         _delay_ms(50);\r
101         bmp280Init();\r
102         //DDRB|=(1<<PINB1);\r
103         gcontrol=1;\r
104         sei();\r
105     while(1)   {\r
106         \r
107                 if (gcontrol) {\r
108                         //PORTB|=(1<<PINB1); //Dauer 5.4ms\r
109                         uint32_t P;\r
110                         int32_t t;\r
111                         bmp280ConvertInt(&t,&P,1);\r
112                         P=P-70000;\r
113                         P=P/20;\r
114                         uint16_t w=P;\r
115                         uint8_t t8=w>>4;\r
116                         uint8_t af=0;\r
117                         if (t8>pack.TH) af=1;\r
118                         if (t8<=pack.TL) af=1; \r
119                         cli();\r
120                         pack.temp=w;\r
121                         //pack.temp++;\r
122                         alarmflag=af;\r
123                         sei();                  \r
124                         EXTERN_SLEEP();\r
125                         //PORTB&=~(1<<PINB1);\r
126                 }\r
127 \r
128                 OWST_MAIN_END\r
129 \r
130    }\r
131 \r
132 \r
133 }