1 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the
14 // * All advertising materials mentioning features or use of this
15 // software must display the following acknowledgement: This product
16 // includes software developed by tm3d.de and its contributors.
17 // * Neither the name of tm3d.de nor the names of its contributors may
18 // be used to endorse or promote products derived from this software
19 // without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #define F_CPU 4000000UL
35 #define F_CPU 8000000UL
40 #include <util/delay.h>
41 #include <avr/pgmspace.h>
43 #include "USI_TWI_Master.h"
48 #define CRC8_POLYNOMIAL 0x31
49 #define CRC8_INIT 0xFF
53 uint8_t sensirion_common_generate_crc(uint8_t *data, uint8_t count)
55 uint16_t current_byte;
56 uint8_t crc = CRC8_INIT;
59 /* calculates 8-Bit checksum with given polynomial */
60 for (current_byte = 0; current_byte < count; ++current_byte) {
61 crc ^= (data[current_byte]);
62 for (crc_bit = 8; crc_bit > 0; --crc_bit) {
64 crc = (crc << 1) ^ CRC8_POLYNOMIAL;
72 int8_t sensirion_common_check_crc(uint8_t *data, uint8_t count, uint8_t checksum)
74 if (sensirion_common_generate_crc(data, count) != checksum)
79 void readSGPXX(uint16_t com, uint8_t* data, uint8_t len) {
82 I2c_WriteByte(com>>8); //Ctrl hum
83 I2c_WriteByte(com&0x00FF); //Ctrl hum
87 for(uint8_t i=0;i<len-1;i++) {
88 data[i]=I2c_ReadByte(ACK);
90 data[len-1]=I2c_ReadByte(NO_ACK);
94 void writeSGPXX(uint16_t com, uint8_t* data, uint8_t len) {
97 I2c_WriteByte(com>>8); //Ctrl hum
98 I2c_WriteByte(com&0x00FF); //Ctrl hum
99 for(uint8_t i=0;i<len;i++) {
100 I2c_WriteByte(data[i]);
109 uint16_t bl1S=0,bl2S=0;
114 int8_t check_convert_buf(uint8_t *b,uint16_t *v1,uint16_t *v2) {
116 if (sensirion_common_check_crc(b,2,b[2])) { *v1=((int16_t)b[0])<<8|b[1];} else {*v1=0;ret=0;}
117 if (sensirion_common_check_crc(b+3,2,b[5])) {*v2=((int16_t)b[3])<<8|b[4];} else {*v2=0;ret=0;}
123 uint16_t readEEPROM(uint8_t addr,uint16_t def) {
126 while(EECR & (1<<EEPE));
132 while(EECR & (1<<EEPE));
141 void writeEEPROM(uint8_t addr,uint16_t val) {
143 while(EECR & (1<<EEPE));
144 EECR = (0<<EEPM1)|(0<<EEPM0);
149 while(EECR & (1<<EEPE));
150 EECR = (0<<EEPM1)|(0<<EEPM0);
157 void save_baseline(uint16_t b1,uint16_t b2 ) {
162 void read_baseline(uint16_t *b1,uint16_t *b2) {
163 *b1=readEEPROM(0,0xFFFF);
164 *b2=readEEPROM(2,0xFFFF);
167 void set_baseline(uint16_t b1,uint16_t b2) {
169 b[3]=(uint16_t)b1>>8; //irgendwie werden die Werte vertauscht zwischen lesen und schreiben
170 b[4]=(uint16_t)b1&0x00FF;
171 b[5]=sensirion_common_generate_crc(b+3,2);
172 b[0]=(uint16_t)b2>>8;
173 b[1]=(uint16_t)b2&0x00FF;
174 b[2]=sensirion_common_generate_crc(b,2);
175 writeSGPXX(0x201e,b,6);
182 readSGPXX(0x2032,b,3);
184 writeSGPXX(0x2003,0,0);
187 //set_baseline(0x2210,0x3320);
188 //readSGPXX(0x2015,b,6);
189 read_baseline(&b1,&b2);
192 //readSGPXX(0x2015,b,6);
194 //check_convert_buf(b,&bl1,&bl2);
201 void runSGP30(uint16_t *CO2,uint16_t *VOC,uint16_t *ETH,uint16_t *H2){
203 readSGPXX(0x2008,b,6);
204 check_convert_buf(b,CO2,VOC);
208 readSGPXX(0x2015,b,6);
210 check_convert_buf(b,&bl1,&bl2);
211 int8_t bl1d=bl1-bl1S;
212 int8_t bl2d=bl2-bl2S;
213 if (bl1d<0) bl1d=-bl1d;
214 if (bl2d<0) bl2d=-bl2d;
215 if (bl1d>4) {bl1S=bl1;eq=0;}
216 if (bl2d>4) {bl2S=bl2;eq=0;}
217 getbl=200; //Naechste bruefunf in 100 s
220 save_baseline(bl1,bl2);
223 readSGPXX(0x2050,b,6);
224 check_convert_buf(b,ETH,H2);