1 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
\r
2 // All rights reserved.
\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
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
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
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
33 #define F_CPU 4000000UL
\r
35 #define F_CPU 8000000UL
\r
40 #include <util/delay.h>
\r
41 #include <avr/pgmspace.h>
\r
43 #include "USI_TWI_Master.h"
\r
46 #define WC 0b10110000
\r
47 #define RC 0b10110001
\r
48 #define CRC8_POLYNOMIAL 0x31
\r
49 #define CRC8_INIT 0xFF
\r
53 uint8_t sensirion_common_generate_crc(uint8_t *data, uint8_t count)
\r
55 uint16_t current_byte;
\r
56 uint8_t crc = CRC8_INIT;
\r
59 /* calculates 8-Bit checksum with given polynomial */
\r
60 for (current_byte = 0; current_byte < count; ++current_byte) {
\r
61 crc ^= (data[current_byte]);
\r
62 for (crc_bit = 8; crc_bit > 0; --crc_bit) {
\r
64 crc = (crc << 1) ^ CRC8_POLYNOMIAL;
\r
72 int8_t sensirion_common_check_crc(uint8_t *data, uint8_t count, uint8_t checksum)
\r
74 if (sensirion_common_generate_crc(data, count) != checksum)
\r
79 void readSGPXX(uint16_t com, uint8_t* data, uint8_t len) {
\r
80 I2c_StartCondition();
\r
82 I2c_WriteByte(com>>8); //Ctrl hum
\r
83 I2c_WriteByte(com&0x00FF); //Ctrl hum
\r
85 I2c_StartCondition();
\r
87 for(uint8_t i=0;i<len-1;i++) {
\r
88 data[i]=I2c_ReadByte(ACK);
\r
90 data[len-1]=I2c_ReadByte(NO_ACK);
\r
91 I2c_StopCondition();
\r
94 void writeSGPXX(uint16_t com, uint8_t* data, uint8_t len) {
\r
95 I2c_StartCondition();
\r
97 I2c_WriteByte(com>>8); //Ctrl hum
\r
98 I2c_WriteByte(com&0x00FF); //Ctrl hum
\r
99 for(uint8_t i=0;i<len;i++) {
\r
100 I2c_WriteByte(data[i]);
\r
102 I2c_StopCondition();
\r
109 uint16_t bl1S=0,bl2S=0;
\r
114 int8_t check_convert_buf(uint8_t *b,uint16_t *v1,uint16_t *v2) {
\r
116 if (sensirion_common_check_crc(b,2,b[2])) { *v1=((int16_t)b[0])<<8|b[1];} else {*v1=0;ret=0;}
\r
117 if (sensirion_common_check_crc(b+3,2,b[5])) {*v2=((int16_t)b[3])<<8|b[4];} else {*v2=0;ret=0;}
\r
123 uint16_t readEEPROM(uint8_t addr,uint16_t def) {
\r
126 while(EECR & (1<<EEPE));
\r
132 while(EECR & (1<<EEPE));
\r
141 void writeEEPROM(uint8_t addr,uint16_t val) {
\r
143 while(EECR & (1<<EEPE));
\r
144 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
147 EECR |= (1<<EEMPE);
\r
149 while(EECR & (1<<EEPE));
\r
150 EECR = (0<<EEPM1)|(0<<EEPM0);
\r
153 EECR |= (1<<EEMPE);
\r
157 void save_baseline(uint16_t b1,uint16_t b2 ) {
\r
162 void read_baseline(uint16_t *b1,uint16_t *b2) {
\r
163 *b1=readEEPROM(0,0xFFFF);
\r
164 *b2=readEEPROM(2,0xFFFF);
\r
169 void set_baseline(uint16_t b1,uint16_t b2) {
\r
171 b[3]=(uint16_t)b1>>8; //irgendwie werden die Werte vertauscht zwischen lesen und schreiben
\r
172 b[4]=(uint16_t)b1&0x00FF;
\r
173 b[5]=sensirion_common_generate_crc(b+3,2);
\r
174 b[0]=(uint16_t)b2>>8;
\r
175 b[1]=(uint16_t)b2&0x00FF;
\r
176 b[2]=sensirion_common_generate_crc(b,2);
\r
177 writeSGPXX(0x201e,b,6);
\r
182 int8_t initSGP30() {
\r
184 readSGPXX(0x2032,b,3);
\r
186 writeSGPXX(0x2003,0,0);
\r
188 uint16_t b1=0,b2=0;
\r
189 //set_baseline(0x2210,0x3320);
\r
190 //readSGPXX(0x2015,b,6);
\r
191 read_baseline(&b1,&b2);
\r
193 set_baseline(b1,b2);
\r
194 //readSGPXX(0x2015,b,6);
\r
195 //uint16_t bl1,bl2;
\r
196 //check_convert_buf(b,&bl1,&bl2);
\r
203 void runSGP30(uint16_t *CO2,uint16_t *VOC,uint16_t *ETH,uint16_t *H2){
\r
205 readSGPXX(0x2008,b,6);
\r
206 check_convert_buf(b,CO2,VOC);
\r
210 readSGPXX(0x2015,b,6);
\r
212 check_convert_buf(b,&bl1,&bl2);
\r
213 int8_t bl1d=bl1-bl1S;
\r
214 int8_t bl2d=bl2-bl2S;
\r
215 if (bl1d<0) bl1d=-bl1d;
\r
216 if (bl2d<0) bl2d=-bl2d;
\r
217 if (bl1d>4) {bl1S=bl1;eq=0;}
\r
218 if (bl2d>4) {bl2S=bl2;eq=0;}
\r
219 getbl=200; //Naechste bruefunf in 100 s
\r
222 save_baseline(bl1,bl2);
\r
225 readSGPXX(0x2050,b,6);
\r
226 check_convert_buf(b,ETH,H2);
\r