Many changes from 2018
[owSlave2.git] / common / I2C / CDM7160.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 #define F_CPU 8000000UL\r
33 #include <avr/io.h>\r
34 \r
35 #include <util/delay.h>\r
36 \r
37 \r
38 #include "TWI_Master.h"\r
39 #include "CDM7160.h"\r
40 \r
41 \r
42 \r
43 \r
44 uint16_t CDM7160getCO2_() {\r
45         volatile uint8_t b1,b2;\r
46         while((PINA&(1<<PINA0))!=0);\r
47         I2c_StartCondition();\r
48         I2c_WriteByte(0b11010010);\r
49         I2c_WriteByte(0x03);\r
50         I2c_StartCondition();\r
51         I2c_WriteByte (0b11010011);\r
52         b1 =I2c_ReadByte(ACK);\r
53         b2 =I2c_ReadByte(NO_ACK);\r
54         I2c_StopCondition();\r
55         \r
56         return b1|(b2<<8);\r
57         \r
58         \r
59 \r
60 }\r
61 uint16_t CDM7160getCO2() {\r
62         volatile uint8_t b[16],i;\r
63         while((PINA&(1<<PINA0))!=0);\r
64         I2c_StartCondition();\r
65         I2c_WriteByte(0b11010010);\r
66         I2c_WriteByte(0x00);\r
67         I2c_StartCondition();\r
68         I2c_WriteByte (0b11010011);\r
69         for(i=0;i<15;i++) {\r
70                 b[i] =I2c_ReadByte(ACK);\r
71         }\r
72         b[15] =I2c_ReadByte(NO_ACK);\r
73         I2c_StopCondition();\r
74         \r
75         return b[3]|(b[4]<<8);\r
76         \r
77         \r
78 \r
79 }\r
80 \r
81 void CDM7160setMode(uint8_t mode) {\r
82         while((PINA&(1<<PINA0))!=0);\r
83         I2c_StartCondition();\r
84         I2c_WriteByte(0b11010010);\r
85         I2c_WriteByte(0x01);\r
86         I2c_StartCondition();\r
87         I2c_WriteByte (0b11010011);\r
88         uint8_t b =I2c_ReadByte(NO_ACK);\r
89         I2c_StopCondition();\r
90         _delay_ms(2);\r
91         b&=0xFC;\r
92         b|=(mode<<1)&0x3;\r
93         I2c_StartCondition();\r
94         I2c_WriteByte(0b11010010);\r
95         I2c_WriteByte(0x01);\r
96         I2c_WriteByte(b);\r
97         I2c_StopCondition();\r
98 }\r
99 \r
100 void CDM7160softReset() {\r
101         while((PINA&(1<<PINA0))!=0);\r
102         I2c_StartCondition();\r
103         I2c_WriteByte(0b11010010);\r
104         I2c_WriteByte(0x00);\r
105         I2c_WriteByte(0x01);\r
106         I2c_StopCondition();\r
107 }\r
108 \r
109 \r
110 void CDM7160setFMode(uint8_t fmode) //Set Filtermode 0 average, 1 IIR\r
111 {\r
112         while((PINA&(1<<PINA0))!=0);\r
113         I2c_StartCondition();\r
114         I2c_WriteByte(0b11010010);\r
115         I2c_WriteByte(0x01);\r
116         I2c_StartCondition();\r
117         I2c_WriteByte (0b11010011);\r
118         volatile uint8_t b =I2c_ReadByte(NO_ACK);\r
119         I2c_StopCondition();\r
120         _delay_ms(2);\r
121         b&=0xFB;\r
122         b|=((fmode&1)<<2);\r
123         I2c_StartCondition();\r
124         I2c_WriteByte(0b11010010);\r
125         I2c_WriteByte(0x01);\r
126         I2c_WriteByte(b);\r
127         I2c_StopCondition();\r
128 }\r
129 void CDM7160setAvCount(uint8_t count) {//Set the Averaging count\r
130         while((PINA&(   1<<PINA0))!=0);\r
131         I2c_StartCondition();\r
132         I2c_WriteByte(0b11010010);\r
133         I2c_WriteByte(0x07);\r
134         I2c_WriteByte(count&0x3F);\r
135         I2c_StopCondition();\r
136 \r
137 }; \r