X-Git-Url: http://git.smho.de/gw/?p=owSlave2.git;a=blobdiff_plain;f=common%2FI2C%2FSGP30.c;fp=common%2FI2C%2FSGP30.c;h=ee19d6a6d031ecf4e223b02d6a7d410f0c14647f;hp=0000000000000000000000000000000000000000;hb=2490e261a78b2a98ee2a9b8fc19cb3f20225926b;hpb=45806622c08cded7d77c9c9735c433de5f7fa027 diff --git a/common/I2C/SGP30.c b/common/I2C/SGP30.c new file mode 100644 index 0000000..ee19d6a --- /dev/null +++ b/common/I2C/SGP30.c @@ -0,0 +1,228 @@ +// Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the +// distribution. +// * All advertising materials mentioning features or use of this +// software must display the following acknowledgement: This product +// includes software developed by tm3d.de and its contributors. +// * Neither the name of tm3d.de nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#ifdef __4MHZ__ +#define F_CPU 4000000UL +#else +#define F_CPU 8000000UL +#endif +#include + + +#include +#include + +#include "USI_TWI_Master.h" +#include "SGP30.h" +//0x58 +#define WC 0b10110000 +#define RC 0b10110001 +#define CRC8_POLYNOMIAL 0x31 +#define CRC8_INIT 0xFF +#define CRC8_LEN 1 + + +uint8_t sensirion_common_generate_crc(uint8_t *data, uint8_t count) +{ + uint16_t current_byte; + uint8_t crc = CRC8_INIT; + uint8_t crc_bit; + + /* calculates 8-Bit checksum with given polynomial */ + for (current_byte = 0; current_byte < count; ++current_byte) { + crc ^= (data[current_byte]); + for (crc_bit = 8; crc_bit > 0; --crc_bit) { + if (crc & 0x80) + crc = (crc << 1) ^ CRC8_POLYNOMIAL; + else + crc = (crc << 1); + } + } + return crc; +} + +int8_t sensirion_common_check_crc(uint8_t *data, uint8_t count, uint8_t checksum) +{ + if (sensirion_common_generate_crc(data, count) != checksum) + return 0; + return 1; +} + +void readSGPXX(uint16_t com, uint8_t* data, uint8_t len) { + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(com>>8); //Ctrl hum + I2c_WriteByte(com&0x00FF); //Ctrl hum + _delay_ms(100); + I2c_StartCondition(); + I2c_WriteByte (RC); + for(uint8_t i=0;i>8); //Ctrl hum + I2c_WriteByte(com&0x00FF); //Ctrl hum + for(uint8_t i=0;i>8; + EECR |= (1<>8; //irgendwie werden die Werte vertauscht zwischen lesen und schreiben + b[4]=(uint16_t)b1&0x00FF; + b[5]=sensirion_common_generate_crc(b+3,2); + b[0]=(uint16_t)b2>>8; + b[1]=(uint16_t)b2&0x00FF; + b[2]=sensirion_common_generate_crc(b,2); + writeSGPXX(0x201e,b,6); + bl1S=b1; + bl2S=b2; +} + +int8_t initSGP30() { + uint8_t b[10]; + readSGPXX(0x2032,b,3); + _delay_ms(300); + writeSGPXX(0x2003,0,0); + _delay_ms(10); + uint16_t b1=0,b2=0; + //set_baseline(0x2210,0x3320); + //readSGPXX(0x2015,b,6); + read_baseline(&b1,&b2); + if (b1!=0xFFFF) { + set_baseline(b1,b2); + //readSGPXX(0x2015,b,6); + //uint16_t bl1,bl2; + //check_convert_buf(b,&bl1,&bl2); + + } + return 0x1; + +} + +void runSGP30(uint16_t *CO2,uint16_t *VOC,uint16_t *ETH,uint16_t *H2){ + uint8_t b[10]; + readSGPXX(0x2008,b,6); + check_convert_buf(b,CO2,VOC); + getbl--; + if (getbl==0) { + uint8_t eq=1; + readSGPXX(0x2015,b,6); + uint16_t bl1,bl2; + check_convert_buf(b,&bl1,&bl2); + int8_t bl1d=bl1-bl1S; + int8_t bl2d=bl2-bl2S; + if (bl1d<0) bl1d=-bl1d; + if (bl2d<0) bl2d=-bl2d; + if (bl1d>4) {bl1S=bl1;eq=0;} + if (bl2d>4) {bl2S=bl2;eq=0;} + getbl=200; //Naechste bruefunf in 100 s + if (eq==0) { + *VOC+=1000; + save_baseline(bl1,bl2); + } + } + readSGPXX(0x2050,b,6); + check_convert_buf(b,ETH,H2); + +} + +