From 6467d8138460abffd9ddabc7b0a9f00c764af19c Mon Sep 17 00:00:00 2001 From: Tobias Date: Tue, 24 Oct 2017 17:01:11 +0200 Subject: [PATCH] Linux Git --- DS2450_APDS9960/DS2450_APDS9960.c | 564 ++++++++--------- DS2450_IAQCORE/DS2450_IAQCORE.c | 462 +++++++------- common/I2C/APDS9960.c | 274 ++++---- common/I2C/APDS9960.h | 88 +-- common/I2C/BME680.c | 762 +++++++++++------------ common/I2C/BME680.h | 90 +-- common/I2C/IAQCORE.c | 114 ++-- common/I2C/IAQCORE.h | 86 +-- common/I2C/SGP30.c | 454 +++++++------- common/I2C/SGP30.h | 76 +-- programmer/programmer/programmer.asm | 25 +- programmer/programmer/programmer.asmproj | 20 +- 12 files changed, 1514 insertions(+), 1501 deletions(-) diff --git a/DS2450_APDS9960/DS2450_APDS9960.c b/DS2450_APDS9960/DS2450_APDS9960.c index 4ded72e..b8eee45 100644 --- a/DS2450_APDS9960/DS2450_APDS9960.c +++ b/DS2450_APDS9960/DS2450_APDS9960.c @@ -1,283 +1,283 @@ - -// 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. - -#define F_CPU 8000000UL -#include -#include -#include -#include -#include -#include -#include "../common/I2C/USI_TWI_Master.h" -#include "../common/I2C/APDS9960.h" - -extern void OWINIT(); -extern void EXTERN_SLEEP(); - -uint8_t owid[8]={0x20, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x23, 0x20};/**/ -//uint8_t config_info[26]={0x03,13,0x03,13,0x03,13,0x3,15,0x02,20,20,20,20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; -uint8_t config_info[26]={16,15,17,15,18,15,3,15,0x02,23,23,23,23,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - -#if (owid>128) -#error "Variable not correct" -#endif - -extern uint8_t mode; -extern uint8_t gcontrol; -extern uint8_t reset_indicator; -extern uint8_t alarmflag; - - -typedef union { - volatile uint8_t bytes[0x20]; - struct { - //Page0 - uint16_t A; //0 - uint16_t B; //2 - uint16_t C; //4 - uint16_t D; //6 - //Page1 - uint8_t CSA1; - uint8_t CSA2; - uint8_t CSB1; - uint8_t CSB2; - uint8_t CSC1; - uint8_t CSC2; - uint8_t CSD1; - uint8_t CSD2; - //Page2 - uint8_t LA; - uint8_t HA; - uint8_t LB; - uint8_t HB; - uint8_t LC; - uint8_t HC; - uint8_t LD; - uint8_t HD; - //Page3 - uint8_t FC1; - uint8_t FC2; - uint8_t FC3; - uint8_t FC4; - uint8_t VCCP; - uint8_t FC5; - uint8_t FC6; - uint8_t FC7; - uint8_t convc1; - uint8_t convc2; - - - }; -} pack_t; -volatile pack_t pack; - - -uint16_t light[4]; -double rlight[4]; -uint8_t gain=2; -uint8_t atime=192; -//uint8_t atime=255; -double r_gain; -double r_atime; - -void getLight(uint8_t setparameterr) { - if (setparameterr) { - r_gain=(1<<(2*gain)); - r_atime=(256.0-atime)*2.78; - APDS9960setGain(gain); - APDS9960setATime(atime); - _delay_ms(700); - } - APDS9960getRGBC(&light[0],&light[1],&light[2],&light[3]); -} - - - - -int main(void){ - pack.A=0; - pack.B=0; - pack.C=0; - pack.D=0; - pack.CSA1=0x08; - pack.CSA2=0x8C; - pack.CSB1=0x08; - pack.CSB2=0x8C; - pack.CSC1=0x08; - pack.CSC2=0x8C; - pack.CSD1=0x08; - pack.CSD2=0x8C; - pack.HA=0xFF; - pack.LA=0x00; - pack.HB=0xFF; - pack.LB=0x00; - pack.HC=0xFF; - pack.LC=0x00; - pack.HD=0xFF; - pack.LD=0x00; - pack.VCCP=0; - - PORTA=0xFF; - PORTB=0xFF; - - OWINIT(); - - MCUCR &=~(1<0xF000) { - //while (light[3]>900) { - if (gain!=0) { - gain--; - getLight(1); - } else break; - } - if (light[3]<0x2222) { - //if (light[3]<0x22) { - if (gain<3) { - gain++; - getLight(1); - } - - } - for(uint8_t i=0;i<4;i++) { - rlight[i]=(double)light[i]/r_atime/r_gain*70.0; - rlight[i]=(log(rlight[i])*1000)+32767.0; - } - cli();pack.A=rlight[0];sei(); - //cli();pack.A=r_gain;sei(); - alarmflag=0; - if (pack.CSA2&0x08) //AEH - if (pack.bytes[1]>pack.HA) {alarmflag=1;pack.CSA2|=0x20;} - if (pack.CSA2&0x04) //AEL - if (pack.bytes[1]pack.HB) {alarmflag=1;pack.CSB2|=0x20;} - if (pack.CSB2&0x04) //AEL - if (pack.bytes[1]pack.HC) {alarmflag=1;pack.CSC2|=0x20;} - if (pack.CSC2&0x04) //AEL - if (pack.bytes[1]pack.HD) {alarmflag=1;pack.CSD2|=0x20;} - if (pack.CSD2&0x04) //AEL - if (pack.bytes[1] +#include +#include +#include +#include +#include +#include "../common/I2C/USI_TWI_Master.h" +#include "../common/I2C/APDS9960.h" + +extern void OWINIT(); +extern void EXTERN_SLEEP(); + +uint8_t owid[8]={0x20, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x23, 0x20};/**/ +//uint8_t config_info[26]={0x03,13,0x03,13,0x03,13,0x3,15,0x02,20,20,20,20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; +uint8_t config_info[26]={16,15,17,15,18,15,3,15,0x02,23,23,23,23,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + +#if (owid>128) +#error "Variable not correct" +#endif + +extern uint8_t mode; +extern uint8_t gcontrol; +extern uint8_t reset_indicator; +extern uint8_t alarmflag; + + +typedef union { + volatile uint8_t bytes[0x20]; + struct { + //Page0 + uint16_t A; //0 + uint16_t B; //2 + uint16_t C; //4 + uint16_t D; //6 + //Page1 + uint8_t CSA1; + uint8_t CSA2; + uint8_t CSB1; + uint8_t CSB2; + uint8_t CSC1; + uint8_t CSC2; + uint8_t CSD1; + uint8_t CSD2; + //Page2 + uint8_t LA; + uint8_t HA; + uint8_t LB; + uint8_t HB; + uint8_t LC; + uint8_t HC; + uint8_t LD; + uint8_t HD; + //Page3 + uint8_t FC1; + uint8_t FC2; + uint8_t FC3; + uint8_t FC4; + uint8_t VCCP; + uint8_t FC5; + uint8_t FC6; + uint8_t FC7; + uint8_t convc1; + uint8_t convc2; + + + }; +} pack_t; +volatile pack_t pack; + + +uint16_t light[4]; +double rlight[4]; +uint8_t gain=2; +uint8_t atime=192; +//uint8_t atime=255; +double r_gain; +double r_atime; + +void getLight(uint8_t setparameterr) { + if (setparameterr) { + r_gain=(1<<(2*gain)); + r_atime=(256.0-atime)*2.78; + APDS9960setGain(gain); + APDS9960setATime(atime); + _delay_ms(700); + } + APDS9960getRGBC(&light[0],&light[1],&light[2],&light[3]); +} + + + + +int main(void){ + pack.A=0; + pack.B=0; + pack.C=0; + pack.D=0; + pack.CSA1=0x08; + pack.CSA2=0x8C; + pack.CSB1=0x08; + pack.CSB2=0x8C; + pack.CSC1=0x08; + pack.CSC2=0x8C; + pack.CSD1=0x08; + pack.CSD2=0x8C; + pack.HA=0xFF; + pack.LA=0x00; + pack.HB=0xFF; + pack.LB=0x00; + pack.HC=0xFF; + pack.LC=0x00; + pack.HD=0xFF; + pack.LD=0x00; + pack.VCCP=0; + + PORTA=0xFF; + PORTB=0xFF; + + OWINIT(); + + MCUCR &=~(1<0xF000) { + //while (light[3]>900) { + if (gain!=0) { + gain--; + getLight(1); + } else break; + } + if (light[3]<0x2222) { + //if (light[3]<0x22) { + if (gain<3) { + gain++; + getLight(1); + } + + } + for(uint8_t i=0;i<4;i++) { + rlight[i]=(double)light[i]/r_atime/r_gain*70.0; + rlight[i]=(log(rlight[i])*1000)+32767.0; + } + cli();pack.A=rlight[0];sei(); + //cli();pack.A=r_gain;sei(); + alarmflag=0; + if (pack.CSA2&0x08) //AEH + if (pack.bytes[1]>pack.HA) {alarmflag=1;pack.CSA2|=0x20;} + if (pack.CSA2&0x04) //AEL + if (pack.bytes[1]pack.HB) {alarmflag=1;pack.CSB2|=0x20;} + if (pack.CSB2&0x04) //AEL + if (pack.bytes[1]pack.HC) {alarmflag=1;pack.CSC2|=0x20;} + if (pack.CSC2&0x04) //AEL + if (pack.bytes[1]pack.HD) {alarmflag=1;pack.CSD2|=0x20;} + if (pack.CSD2&0x04) //AEL + if (pack.bytes[1] -#include -#include -#include -#include -#include -#include "../common/I2C/USI_TWI_Master.h" -#include "../common/I2C/IAQCORE.h" - -extern void OWINIT(); -extern void EXTERN_SLEEP(); - -uint8_t owid[8]={0x20, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x02, 0x5D};/**/ -uint8_t config_info[26]={0x06,13,0x06,13,0x06,13,0x06,13,0x02,20,20,20,20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - -#if (owid>128) -#error "Variable not correct" -#endif - -extern uint8_t mode; -extern uint8_t gcontrol; -extern uint8_t reset_indicator; -extern uint8_t alarmflag; - - -typedef union { - volatile uint8_t bytes[0x20]; - struct { - //Page0 - uint16_t A; //0 - uint16_t B; //2 - uint16_t C; //4 - uint16_t D; //6 - //Page1 - uint8_t CSA1; - uint8_t CSA2; - uint8_t CSB1; - uint8_t CSB2; - uint8_t CSC1; - uint8_t CSC2; - uint8_t CSD1; - uint8_t CSD2; - //Page2 - uint8_t LA; - uint8_t HA; - uint8_t LB; - uint8_t HB; - uint8_t LC; - uint8_t HC; - uint8_t LD; - uint8_t HD; - //Page3 - uint8_t FC1; - uint8_t FC2; - uint8_t FC3; - uint8_t FC4; - uint8_t VCCP; - uint8_t FC5; - uint8_t FC6; - uint8_t FC7; - uint8_t convc1; - uint8_t convc2; - - - }; -} pack_t; -volatile pack_t pack; - - - -IAQCORE_Data_t IAQCORE_Data; - - -int main(void){ - pack.A=0; - pack.B=0; - pack.C=0; - pack.D=0; - pack.CSA1=0x08; - pack.CSA2=0x8C; - pack.CSB1=0x08; - pack.CSB2=0x8C; - pack.CSC1=0x08; - pack.CSC2=0x8C; - pack.CSD1=0x08; - pack.CSD2=0x8C; - pack.HA=0xFF; - pack.LA=0x00; - pack.HB=0xFF; - pack.LB=0x00; - pack.HC=0xFF; - pack.LC=0x00; - pack.HD=0xFF; - pack.LD=0x00; - pack.VCCP=0; - OWINIT(); - - MCUCR &=~(1<pack.HA) {alarmflag=1;pack.CSA2|=0x20;} - if (pack.CSA2&0x04) //AEL - if (pack.bytes[1]pack.HB) {alarmflag=1;pack.CSB2|=0x20;} - if (pack.CSB2&0x04) //AEL - if (pack.bytes[1]pack.HC) {alarmflag=1;pack.CSC2|=0x20;} - if (pack.CSC2&0x04) //AEL - if (pack.bytes[1]pack.HD) {alarmflag=1;pack.CSD2|=0x20;} - if (pack.CSD2&0x04) //AEL - if (pack.bytes[1] +#include +#include +#include +#include +#include +#include "../common/I2C/USI_TWI_Master.h" +#include "../common/I2C/IAQCORE.h" + +extern void OWINIT(); +extern void EXTERN_SLEEP(); + +uint8_t owid[8]={0x20, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x02, 0x5D};/**/ +uint8_t config_info[26]={0x06,13,0x06,13,0x06,13,0x06,13,0x02,20,20,20,20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + +#if (owid>128) +#error "Variable not correct" +#endif + +extern uint8_t mode; +extern uint8_t gcontrol; +extern uint8_t reset_indicator; +extern uint8_t alarmflag; + + +typedef union { + volatile uint8_t bytes[0x20]; + struct { + //Page0 + uint16_t A; //0 + uint16_t B; //2 + uint16_t C; //4 + uint16_t D; //6 + //Page1 + uint8_t CSA1; + uint8_t CSA2; + uint8_t CSB1; + uint8_t CSB2; + uint8_t CSC1; + uint8_t CSC2; + uint8_t CSD1; + uint8_t CSD2; + //Page2 + uint8_t LA; + uint8_t HA; + uint8_t LB; + uint8_t HB; + uint8_t LC; + uint8_t HC; + uint8_t LD; + uint8_t HD; + //Page3 + uint8_t FC1; + uint8_t FC2; + uint8_t FC3; + uint8_t FC4; + uint8_t VCCP; + uint8_t FC5; + uint8_t FC6; + uint8_t FC7; + uint8_t convc1; + uint8_t convc2; + + + }; +} pack_t; +volatile pack_t pack; + + + +IAQCORE_Data_t IAQCORE_Data; + + +int main(void){ + pack.A=0; + pack.B=0; + pack.C=0; + pack.D=0; + pack.CSA1=0x08; + pack.CSA2=0x8C; + pack.CSB1=0x08; + pack.CSB2=0x8C; + pack.CSC1=0x08; + pack.CSC2=0x8C; + pack.CSD1=0x08; + pack.CSD2=0x8C; + pack.HA=0xFF; + pack.LA=0x00; + pack.HB=0xFF; + pack.LB=0x00; + pack.HC=0xFF; + pack.LC=0x00; + pack.HD=0xFF; + pack.LD=0x00; + pack.VCCP=0; + OWINIT(); + + MCUCR &=~(1<pack.HA) {alarmflag=1;pack.CSA2|=0x20;} + if (pack.CSA2&0x04) //AEL + if (pack.bytes[1]pack.HB) {alarmflag=1;pack.CSB2|=0x20;} + if (pack.CSB2&0x04) //AEL + if (pack.bytes[1]pack.HC) {alarmflag=1;pack.CSC2|=0x20;} + if (pack.CSC2&0x04) //AEL + if (pack.bytes[1]pack.HD) {alarmflag=1;pack.CSD2|=0x20;} + if (pack.CSD2&0x04) //AEL + if (pack.bytes[1] - -#include - - -#include "USI_TWI_Master.h" -#include "APDS9960.h" - -#define WC 0b01110010 -#define RC 0b01110011 - -int8_t initAPDS9960() { - I2c_StartCondition(); - I2c_WriteByte(WC); - I2c_WriteByte(0x80); - I2c_StartCondition(); - I2c_WriteByte (RC); - uint8_t b1 =I2c_ReadByte(NO_ACK); - I2c_StopCondition(); - if (b1==0xFF) return 0; else { - I2c_StartCondition(); - I2c_WriteByte(WC); - I2c_WriteByte(0x80); - I2c_WriteByte(0x0B); //ALS Enable PowerON - I2c_StopCondition(); - } - return 1; -} -void APDS9960setATime(uint8_t ATime) { - I2c_StartCondition(); - I2c_WriteByte(WC); - I2c_WriteByte(0x81); - I2c_WriteByte(ATime); - I2c_StopCondition(); -} -void APDS9960setGain(uint8_t Gain) { - I2c_StartCondition(); - I2c_WriteByte(WC); - I2c_WriteByte(0x8F); - I2c_WriteByte(Gain&0x03); - I2c_StopCondition(); -} -uint8_t APDS9960getAVALID() {return 0;} -uint8_t APDS9960getASAT() {return 0;} - -void APDS9960getRGBC(uint16_t* R,uint16_t* G,uint16_t* B,uint16_t* C) { - I2c_StartCondition(); - I2c_WriteByte(WC); - I2c_WriteByte(0x94); - I2c_StartCondition(); - I2c_WriteByte (RC); - *C=I2c_ReadByte(ACK)|I2c_ReadByte(ACK)<<8; - *R=I2c_ReadByte(ACK)|I2c_ReadByte(ACK)<<8; - *G=I2c_ReadByte(ACK)|I2c_ReadByte(ACK)<<8; - *B=I2c_ReadByte(ACK)|I2c_ReadByte(NO_ACK)<<8; - I2c_StopCondition(); -} - - -/* -uint8_t checkMAX44009(uint8_t nr) { - volatile uint8_t b1; - nr=(nr<<1)&0x02f; - - I2c_StartCondition(); - I2c_WriteByte(0b10010100|nr); - I2c_WriteByte(0x03); - I2c_StartCondition(); - I2c_WriteByte (0b10010101|nr); - b1 =I2c_ReadByte(NO_ACK); - I2c_StopCondition(); - return b1!=0xFF; - -} - - -double MAX44009getlux(uint8_t nr) { - volatile uint8_t b1,b2; - nr=(nr<<1)&0x02f; - - I2c_StartCondition(); - I2c_WriteByte(0b10010100|nr); - I2c_WriteByte(0x03); - I2c_StartCondition(); - I2c_WriteByte (0b10010101|nr); - b1 =I2c_ReadByte(NO_ACK); - I2c_StopCondition(); - I2c_StartCondition(); - I2c_WriteByte(0b10010100|nr); - I2c_WriteByte(0x04); - I2c_StartCondition(); - I2c_WriteByte (0b10010101|nr); - b2 =I2c_ReadByte(NO_ACK); - I2c_StopCondition(); - uint8_t exponent = (b1 & 0xF0) >> 4;// upper four bits of high byte register - uint8_t mantissa = (b1 & 0x0F) << 4;// lower four bits of high byte register = - // upper four bits of mantissa - mantissa += b2 & 0x0F; // lower four bits of low byte register = - // lower four bits of mantissa - - return (double)mantissa * (double)(1 << (uint16_t)exponent) * 0.045; - - - -} +// Copyright (c) 2015, 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. +#define F_CPU 8000000UL +#include + +#include + + +#include "USI_TWI_Master.h" +#include "APDS9960.h" + +#define WC 0b01110010 +#define RC 0b01110011 + +int8_t initAPDS9960() { + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(0x80); + I2c_StartCondition(); + I2c_WriteByte (RC); + uint8_t b1 =I2c_ReadByte(NO_ACK); + I2c_StopCondition(); + if (b1==0xFF) return 0; else { + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(0x80); + I2c_WriteByte(0x0B); //ALS Enable PowerON + I2c_StopCondition(); + } + return 1; +} +void APDS9960setATime(uint8_t ATime) { + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(0x81); + I2c_WriteByte(ATime); + I2c_StopCondition(); +} +void APDS9960setGain(uint8_t Gain) { + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(0x8F); + I2c_WriteByte(Gain&0x03); + I2c_StopCondition(); +} +uint8_t APDS9960getAVALID() {return 0;} +uint8_t APDS9960getASAT() {return 0;} + +void APDS9960getRGBC(uint16_t* R,uint16_t* G,uint16_t* B,uint16_t* C) { + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(0x94); + I2c_StartCondition(); + I2c_WriteByte (RC); + *C=I2c_ReadByte(ACK)|I2c_ReadByte(ACK)<<8; + *R=I2c_ReadByte(ACK)|I2c_ReadByte(ACK)<<8; + *G=I2c_ReadByte(ACK)|I2c_ReadByte(ACK)<<8; + *B=I2c_ReadByte(ACK)|I2c_ReadByte(NO_ACK)<<8; + I2c_StopCondition(); +} + + +/* +uint8_t checkMAX44009(uint8_t nr) { + volatile uint8_t b1; + nr=(nr<<1)&0x02f; + + I2c_StartCondition(); + I2c_WriteByte(0b10010100|nr); + I2c_WriteByte(0x03); + I2c_StartCondition(); + I2c_WriteByte (0b10010101|nr); + b1 =I2c_ReadByte(NO_ACK); + I2c_StopCondition(); + return b1!=0xFF; + +} + + +double MAX44009getlux(uint8_t nr) { + volatile uint8_t b1,b2; + nr=(nr<<1)&0x02f; + + I2c_StartCondition(); + I2c_WriteByte(0b10010100|nr); + I2c_WriteByte(0x03); + I2c_StartCondition(); + I2c_WriteByte (0b10010101|nr); + b1 =I2c_ReadByte(NO_ACK); + I2c_StopCondition(); + I2c_StartCondition(); + I2c_WriteByte(0b10010100|nr); + I2c_WriteByte(0x04); + I2c_StartCondition(); + I2c_WriteByte (0b10010101|nr); + b2 =I2c_ReadByte(NO_ACK); + I2c_StopCondition(); + uint8_t exponent = (b1 & 0xF0) >> 4;// upper four bits of high byte register + uint8_t mantissa = (b1 & 0x0F) << 4;// lower four bits of high byte register = + // upper four bits of mantissa + mantissa += b2 & 0x0F; // lower four bits of low byte register = + // lower four bits of mantissa + + return (double)mantissa * (double)(1 << (uint16_t)exponent) * 0.045; + + + +} */ \ No newline at end of file diff --git a/common/I2C/APDS9960.h b/common/I2C/APDS9960.h index a6ab18a..c21b23f 100644 --- a/common/I2C/APDS9960.h +++ b/common/I2C/APDS9960.h @@ -1,45 +1,45 @@ -#ifndef APDS9960_H -#define APDS9960_H -// Copyright (c) 2015, 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. - -int8_t initAPDS9960(); -void APDS9960setATime(uint8_t ATime); -void APDS9960setGain(uint8_t Gain); -uint8_t APDS9960getAVALID(); -uint8_t APDS9960getASAT(); - -void APDS9960getRGBC(uint16_t* R,uint16_t* G,uint16_t* B,uint16_t* C); - - - +#ifndef APDS9960_H +#define APDS9960_H +// Copyright (c) 2015, 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. + +int8_t initAPDS9960(); +void APDS9960setATime(uint8_t ATime); +void APDS9960setGain(uint8_t Gain); +uint8_t APDS9960getAVALID(); +uint8_t APDS9960getASAT(); + +void APDS9960getRGBC(uint16_t* R,uint16_t* G,uint16_t* B,uint16_t* C); + + + #endif \ No newline at end of file diff --git a/common/I2C/BME680.c b/common/I2C/BME680.c index 3f425ba..9907867 100644 --- a/common/I2C/BME680.c +++ b/common/I2C/BME680.c @@ -1,242 +1,242 @@ -// Copyright (c) 2015, 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 - - -/** Array Index to Field data mapping for Calibration Data*/ -#define BME680_T2_LSB_REG (1) -#define BME680_T2_MSB_REG (2) -#define BME680_T3_REG (3) -#define BME680_P1_LSB_REG (5) -#define BME680_P1_MSB_REG (6) -#define BME680_P2_LSB_REG (7) -#define BME680_P2_MSB_REG (8) -#define BME680_P3_REG (9) -#define BME680_P4_LSB_REG (11) -#define BME680_P4_MSB_REG (12) -#define BME680_P5_LSB_REG (13) -#define BME680_P5_MSB_REG (14) -#define BME680_P7_REG (15) -#define BME680_P6_REG (16) -#define BME680_P8_LSB_REG (19) -#define BME680_P8_MSB_REG (20) -#define BME680_P9_LSB_REG (21) -#define BME680_P9_MSB_REG (22) -#define BME680_P10_REG (23) -#define BME680_H2_MSB_REG (25) -#define BME680_H2_LSB_REG (26) -#define BME680_H1_LSB_REG (26) -#define BME680_H1_MSB_REG (27) -#define BME680_H3_REG (28) -#define BME680_H4_REG (29) -#define BME680_H5_REG (30) -#define BME680_H6_REG (31) -#define BME680_H7_REG (32) -#define BME680_T1_LSB_REG (33) -#define BME680_T1_MSB_REG (34) -#define BME680_GH2_LSB_REG (35) -#define BME680_GH2_MSB_REG (36) -#define BME680_GH1_REG (37) -#define BME680_GH3_REG (38) - - -#include -#include - -#include "USI_TWI_Master.h" -#include "BME680.h" - -#define WC 0b11101100 -#define RC 0b11101101 -typedef union { - volatile uint8_t d[41]; - struct { - uint8_t fr1; - int16_t t2; - int8_t t3; - uint8_t fr2; //4 - uint16_t p1; - int16_t p2; //7 - int8_t p3; - uint8_t fr3; //10 - int16_t p4; - int16_t p5; - int8_t p7; - int8_t p6; - uint8_t fr4[2]; //17-18 - int16_t p8; - int16_t p9; - uint8_t p10; - uint8_t fr5; //24 - uint16_t h2_; //25 +26 - uint8_t h1_; //halb und halb 26+27 - int8_t h3; - int8_t h4; - int8_t h5; - uint8_t h6; - int8_t h7;//32 - uint16_t t1; - int16_t gh2; - int8_t gh1; - int8_t gh3; //38 - int8_t fr6;// 39; - int8_t fr7; //40; - uint16_t h2; //Berechnung - uint16_t h1; //Berechnung - int32_t t_fine; //Berechnung bei Temperaturmessung +// Copyright (c) 2015, 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 + + +/** Array Index to Field data mapping for Calibration Data*/ +#define BME680_T2_LSB_REG (1) +#define BME680_T2_MSB_REG (2) +#define BME680_T3_REG (3) +#define BME680_P1_LSB_REG (5) +#define BME680_P1_MSB_REG (6) +#define BME680_P2_LSB_REG (7) +#define BME680_P2_MSB_REG (8) +#define BME680_P3_REG (9) +#define BME680_P4_LSB_REG (11) +#define BME680_P4_MSB_REG (12) +#define BME680_P5_LSB_REG (13) +#define BME680_P5_MSB_REG (14) +#define BME680_P7_REG (15) +#define BME680_P6_REG (16) +#define BME680_P8_LSB_REG (19) +#define BME680_P8_MSB_REG (20) +#define BME680_P9_LSB_REG (21) +#define BME680_P9_MSB_REG (22) +#define BME680_P10_REG (23) +#define BME680_H2_MSB_REG (25) +#define BME680_H2_LSB_REG (26) +#define BME680_H1_LSB_REG (26) +#define BME680_H1_MSB_REG (27) +#define BME680_H3_REG (28) +#define BME680_H4_REG (29) +#define BME680_H5_REG (30) +#define BME680_H6_REG (31) +#define BME680_H7_REG (32) +#define BME680_T1_LSB_REG (33) +#define BME680_T1_MSB_REG (34) +#define BME680_GH2_LSB_REG (35) +#define BME680_GH2_MSB_REG (36) +#define BME680_GH1_REG (37) +#define BME680_GH3_REG (38) + + +#include +#include + +#include "USI_TWI_Master.h" +#include "BME680.h" + +#define WC 0b11101100 +#define RC 0b11101101 +typedef union { + volatile uint8_t d[41]; + struct { + uint8_t fr1; + int16_t t2; + int8_t t3; + uint8_t fr2; //4 + uint16_t p1; + int16_t p2; //7 + int8_t p3; + uint8_t fr3; //10 + int16_t p4; + int16_t p5; + int8_t p7; + int8_t p6; + uint8_t fr4[2]; //17-18 + int16_t p8; + int16_t p9; + uint8_t p10; + uint8_t fr5; //24 + uint16_t h2_; //25 +26 + uint8_t h1_; //halb und halb 26+27 + int8_t h3; + int8_t h4; + int8_t h5; + uint8_t h6; + int8_t h7;//32 + uint16_t t1; + int16_t gh2; + int8_t gh1; + int8_t gh3; //38 + int8_t fr6;// 39; + int8_t fr7; //40; + uint16_t h2; //Berechnung + uint16_t h1; //Berechnung + int32_t t_fine; //Berechnung bei Temperaturmessung uint8_t res_heat_range;/**bme680_bus_read( - bme680->dev_addr, - BME680_CALIB_I2C_ADDR_2, - (a_data_u8 + - BME680_CALIB_DATA_LENGTH_GAS), - BME680_CALIB_DATA_LENGTH); - - - - */ - - -void setup_read(uint8_t addr) { - I2c_StartCondition(); - I2c_WriteByte(WC); - I2c_WriteByte(addr); //Ctrl hum - I2c_StartCondition(); - I2c_WriteByte (RC); - -} - -void setup_write(uint8_t addr) { - I2c_StartCondition(); - I2c_WriteByte(WC); - I2c_WriteByte(addr); //Ctrl hum -} - - -uint8_t readone(uint8_t addr) { - setup_read(addr); - uint8_t b=I2c_ReadByte(NO_ACK); - I2c_StopCondition(); - return b; -} - -void writeone(uint8_t addr,uint8_t b) { - setup_write(addr); - I2c_WriteByte(b); //Ctrl hum - I2c_StopCondition(); -} - -int8_t initBME680() { - uint8_t b1=readone(0xD0); - setup_read(BME680_CALIB_I2C_ADDR_1); - for(uint8_t i=0;i>4; - calib.res_heat_val=readone(0); - calib.range_switching_error=(readone(0x04)& 0xF0)>>4; - - - calib.h1 = (uint16_t)(((((uint16_t)calib.d[ BME680_H1_MSB_REG])) - << 4) | (calib.d[ BME680_H1_LSB_REG] &0x0F)); - calib.h2 = (uint16_t)(((((uint16_t)calib.d[ BME680_H2_MSB_REG])) - << 4) | ((calib.d[ BME680_H2_LSB_REG]) >> 4)); - - - /* - - I2c_StartCondition(); - I2c_WriteByte(WC); - I2c_WriteByte(0x72); //Ctrl hum - I2c_WriteByte(0x01); //1x oversembling hum - I2c_WriteByte(0x74); //Ctrl hum - I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont - I2c_StopCondition(); - - */ - calib.ltemp=25; - - return b1==0x61; - -} - - + int8_t range_switching_error;/**bme680_bus_read( + bme680->dev_addr, + BME680_CALIB_I2C_ADDR_2, + (a_data_u8 + + BME680_CALIB_DATA_LENGTH_GAS), + BME680_CALIB_DATA_LENGTH); + + + + */ + + +void setup_read(uint8_t addr) { + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(addr); //Ctrl hum + I2c_StartCondition(); + I2c_WriteByte (RC); + +} + +void setup_write(uint8_t addr) { + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(addr); //Ctrl hum +} + + +uint8_t readone(uint8_t addr) { + setup_read(addr); + uint8_t b=I2c_ReadByte(NO_ACK); + I2c_StopCondition(); + return b; +} + +void writeone(uint8_t addr,uint8_t b) { + setup_write(addr); + I2c_WriteByte(b); //Ctrl hum + I2c_StopCondition(); +} + +int8_t initBME680() { + uint8_t b1=readone(0xD0); + setup_read(BME680_CALIB_I2C_ADDR_1); + for(uint8_t i=0;i>4; + calib.res_heat_val=readone(0); + calib.range_switching_error=(readone(0x04)& 0xF0)>>4; + + + calib.h1 = (uint16_t)(((((uint16_t)calib.d[ BME680_H1_MSB_REG])) + << 4) | (calib.d[ BME680_H1_LSB_REG] &0x0F)); + calib.h2 = (uint16_t)(((((uint16_t)calib.d[ BME680_H2_MSB_REG])) + << 4) | ((calib.d[ BME680_H2_LSB_REG]) >> 4)); + + + /* + + I2c_StartCondition(); + I2c_WriteByte(WC); + I2c_WriteByte(0x72); //Ctrl hum + I2c_WriteByte(0x01); //1x oversembling hum + I2c_WriteByte(0x74); //Ctrl hum + I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont + I2c_StopCondition(); + + */ + calib.ltemp=25; + + return b1==0x61; + +} + + const float lookup_k1_range[16] PROGMEM = { 1, 1, 1, 1, 1,0.99, 1, 0.992, 1, 1, 0.998, 0.995, 1, 0.99, 1, 1}; const float lookup_k2_range[16] PROGMEM = { 8e6, 4e6, 2e6, 1e6,499500.4995, 248262.1648, 125000, 63004.03226, -31281.28128, 15625, 7812.5, 3906.25,1953.125,976.5625, 488.28125, 244.140625}; - +31281.28128, 15625, 7812.5, 3906.25,1953.125,976.5625, 488.28125, 244.140625}; + double bme680_compensate_gas_double(uint16_t gas_adc_u16, uint8_t gas_range_u8) { double gas_res_d = 0; @@ -256,12 +256,12 @@ double bme680_compensate_gas_double(uint16_t gas_adc_u16, uint8_t gas_range_u8) gas_res_d = var1*a2/(gas_adc_u16-512.0+var1); return gas_res_d; } - - -void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){ - - - + + +void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){ + + + int32_t var1 ; int32_t var2 ; int32_t var3 ; @@ -284,8 +284,8 @@ void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){ res_heat_x100 = (int32_t)(((var4 / var5) - 250) * 34); res_heat = (uint8_t) ((res_heat_x100 + 50) / 100); - } - uint16_t duration=100; + } + uint16_t duration=100; uint8_t factor = 0; while ((duration) > 0x3F) { @@ -293,144 +293,144 @@ void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G){ factor += 1; } (duration) = (duration) + (factor * 64); - - //I2c_WriteByte(0x74); //Ctrl hum - //I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont - // [71] <- 10; [72] <- 04; [73] <- 0C; [74] <- 91; [75] <- 00; - // [70] <- 00 [71] <- 10; [72] <- 04; [73] <- 0C; [74] <- 91; [75] <- 00; - setup_write(0x70); - I2c_WriteByte(0x00); - I2c_WriteByte(0x71); - I2c_WriteByte(0x10); - I2c_WriteByte(0x72); - I2c_WriteByte(0x04); - I2c_WriteByte(0x73); - I2c_WriteByte(0x0C); - I2c_WriteByte(0x74); - I2c_WriteByte(0x90); - I2c_WriteByte(0x75); - I2c_WriteByte(0x00); - - I2c_WriteByte(0x5A); - I2c_WriteByte(res_heat); - I2c_WriteByte(0x64); - I2c_WriteByte(duration); - I2c_StopCondition(); - - - writeone(0x74,0x91); - _delay_ms(1000); - - uint8_t bx=0x91; - while ((bx&0x01)==0x01) { - bx=readone(0x74); - _delay_ms(5); - } - - //volatile uint8_t rs=readone(0x2B); - uint32_t Th,Hh,Ph; - setup_read(0x1F); - Ph=I2c_ReadByte(ACK);Ph=Ph<<8; - Ph|=I2c_ReadByte(ACK);Ph=Ph<<4; - Ph|=I2c_ReadByte(ACK)>>4; - Th=I2c_ReadByte(ACK);Th=Th<<8; - Th|=I2c_ReadByte(ACK);Th=Th<<4; - Th|=I2c_ReadByte(ACK)>>4; - Hh=I2c_ReadByte(ACK);Hh=Hh<<8; - Hh|=I2c_ReadByte(NO_ACK); - I2c_StopCondition(); - setup_read(0x2A); - volatile uint8_t g1=I2c_ReadByte(ACK); - volatile uint8_t g2=I2c_ReadByte(NO_ACK); - I2c_StopCondition(); - *G=(((uint16_t)g1)<<2)|(g2>>6); - *P=*G; - *G= bme680_compensate_gas_double(*G,g2&0xF)/10.0; - - int32_t temp_comp = 0; - - var1 = ((int32_t)Th >> 3) - - ((int32_t)calib.t1 << 1); - var2 = (var1 * (int32_t)calib.t2) >> 11; - var3 = ((((var1 >> 1) * (var1 >> 1)) >> 12) * - ((int32_t)calib.t3 << 4)) >> 14; - calib.t_fine = var2 + var3; - temp_comp = ((calib.t_fine * 5) + 128) >> 8; - - int32_t temp_scaled = 0; - int32_t var6 = 0; - int32_t humidity_comp = 0; - - temp_scaled = (((int32_t)calib.t_fine * 5) + 128) >> 8; - var1 = (int32_t)Hh - - ((int32_t)((int32_t)calib.h1 << 4)) - - (((temp_scaled * (int32_t)calib.h3) / - ((int32_t)100)) >> 1); - - var2 = ((int32_t)calib.h2 * - (((temp_scaled * (int32_t)calib.h4) / - ((int32_t)100)) + (((temp_scaled * - ((temp_scaled * (int32_t)calib.h5) / - ((int32_t)100))) >> 6) / ((int32_t)100)) + (int32_t)(1 << 14))) >> 10; - - var3 = var1 * var2; - - var4 = ((((int32_t)calib.h6) << 7) + - ((temp_scaled * (int32_t)calib.h7) / - ((int32_t)100))) >> 4; - - var5 = ((var3 >> 14) * (var3 >> 14)) >> 10; - var6 = (var4 * var5) >> 1; - - humidity_comp = (var3 + var6) >> 12; - if (humidity_comp > BME680_MAX_HUMIDITY_VALUE) - humidity_comp = BME680_MAX_HUMIDITY_VALUE; - else if (humidity_comp < BME680_MIN_HUMIDITY_VALUE) - humidity_comp = BME680_MIN_HUMIDITY_VALUE; - - int32_t pressure_comp = 0;//int -> 5684 - - var1 = (((int32_t)calib.t_fine) >> 1) - (int32_t)64000; - var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * - (int32_t)calib.p6) >> 2; - var2 = var2 + ((var1 * (int32_t)calib.p5) << 1); - var2 = (var2 >> 2) + ((int32_t)calib.p4 << 16); - var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) * - ((int32_t)calib.p3 << 5)) >> 3) + - (((int32_t)calib.p2 * var1) >> 1); - var1 = var1 >> 18; - var1 = (((int32_t)32768 + var1) * (int32_t)calib.p1) >> 15; - pressure_comp = (int32_t)1048576 - Ph; - pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125)); - var4 = ((int32_t)1 << 31); - if (pressure_comp >= var4) - pressure_comp = ((pressure_comp / (uint32_t)var1) << 1); - else - pressure_comp = ((pressure_comp << 1) / (uint32_t)var1); - var1 = ((int32_t)calib.p9 * (int32_t)(((pressure_comp >> 3) * - (pressure_comp >> 3)) >> 13)) >> 12; - var2 = ((int32_t)(pressure_comp >> 2) * - (int32_t)calib.p8) >> 13; - var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) * - (int32_t)(pressure_comp >> 8) * - (int32_t)calib.p10) >> 17; - - pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 + - ((int32_t)calib.p7 << 7)) >> 4); - - - - - - *T=(int16_t)temp_comp; - calib.ltemp=temp_comp/100; - //*P=pressure_comp; - *H=(uint16_t)(humidity_comp/10); - //*P=rs; - *T=g1; - *H=g2; - //*P= - - -} - + + //I2c_WriteByte(0x74); //Ctrl hum + //I2c_WriteByte(0b01010101); //2x oversembling t - 16x oversemmping p - mode cont + // [71] <- 10; [72] <- 04; [73] <- 0C; [74] <- 91; [75] <- 00; + // [70] <- 00 [71] <- 10; [72] <- 04; [73] <- 0C; [74] <- 91; [75] <- 00; + setup_write(0x70); + I2c_WriteByte(0x00); + I2c_WriteByte(0x71); + I2c_WriteByte(0x10); + I2c_WriteByte(0x72); + I2c_WriteByte(0x04); + I2c_WriteByte(0x73); + I2c_WriteByte(0x0C); + I2c_WriteByte(0x74); + I2c_WriteByte(0x90); + I2c_WriteByte(0x75); + I2c_WriteByte(0x00); + + I2c_WriteByte(0x5A); + I2c_WriteByte(res_heat); + I2c_WriteByte(0x64); + I2c_WriteByte(duration); + I2c_StopCondition(); + + + writeone(0x74,0x91); + _delay_ms(1000); + + uint8_t bx=0x91; + while ((bx&0x01)==0x01) { + bx=readone(0x74); + _delay_ms(5); + } + + //volatile uint8_t rs=readone(0x2B); + uint32_t Th,Hh,Ph; + setup_read(0x1F); + Ph=I2c_ReadByte(ACK);Ph=Ph<<8; + Ph|=I2c_ReadByte(ACK);Ph=Ph<<4; + Ph|=I2c_ReadByte(ACK)>>4; + Th=I2c_ReadByte(ACK);Th=Th<<8; + Th|=I2c_ReadByte(ACK);Th=Th<<4; + Th|=I2c_ReadByte(ACK)>>4; + Hh=I2c_ReadByte(ACK);Hh=Hh<<8; + Hh|=I2c_ReadByte(NO_ACK); + I2c_StopCondition(); + setup_read(0x2A); + volatile uint8_t g1=I2c_ReadByte(ACK); + volatile uint8_t g2=I2c_ReadByte(NO_ACK); + I2c_StopCondition(); + *G=(((uint16_t)g1)<<2)|(g2>>6); + *P=*G; + *G= bme680_compensate_gas_double(*G,g2&0xF)/10.0; + + int32_t temp_comp = 0; + + var1 = ((int32_t)Th >> 3) - + ((int32_t)calib.t1 << 1); + var2 = (var1 * (int32_t)calib.t2) >> 11; + var3 = ((((var1 >> 1) * (var1 >> 1)) >> 12) * + ((int32_t)calib.t3 << 4)) >> 14; + calib.t_fine = var2 + var3; + temp_comp = ((calib.t_fine * 5) + 128) >> 8; + + int32_t temp_scaled = 0; + int32_t var6 = 0; + int32_t humidity_comp = 0; + + temp_scaled = (((int32_t)calib.t_fine * 5) + 128) >> 8; + var1 = (int32_t)Hh - + ((int32_t)((int32_t)calib.h1 << 4)) - + (((temp_scaled * (int32_t)calib.h3) / + ((int32_t)100)) >> 1); + + var2 = ((int32_t)calib.h2 * + (((temp_scaled * (int32_t)calib.h4) / + ((int32_t)100)) + (((temp_scaled * + ((temp_scaled * (int32_t)calib.h5) / + ((int32_t)100))) >> 6) / ((int32_t)100)) + (int32_t)(1 << 14))) >> 10; + + var3 = var1 * var2; + + var4 = ((((int32_t)calib.h6) << 7) + + ((temp_scaled * (int32_t)calib.h7) / + ((int32_t)100))) >> 4; + + var5 = ((var3 >> 14) * (var3 >> 14)) >> 10; + var6 = (var4 * var5) >> 1; + + humidity_comp = (var3 + var6) >> 12; + if (humidity_comp > BME680_MAX_HUMIDITY_VALUE) + humidity_comp = BME680_MAX_HUMIDITY_VALUE; + else if (humidity_comp < BME680_MIN_HUMIDITY_VALUE) + humidity_comp = BME680_MIN_HUMIDITY_VALUE; + + int32_t pressure_comp = 0;//int -> 5684 + + var1 = (((int32_t)calib.t_fine) >> 1) - (int32_t)64000; + var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * + (int32_t)calib.p6) >> 2; + var2 = var2 + ((var1 * (int32_t)calib.p5) << 1); + var2 = (var2 >> 2) + ((int32_t)calib.p4 << 16); + var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) * + ((int32_t)calib.p3 << 5)) >> 3) + + (((int32_t)calib.p2 * var1) >> 1); + var1 = var1 >> 18; + var1 = (((int32_t)32768 + var1) * (int32_t)calib.p1) >> 15; + pressure_comp = (int32_t)1048576 - Ph; + pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125)); + var4 = ((int32_t)1 << 31); + if (pressure_comp >= var4) + pressure_comp = ((pressure_comp / (uint32_t)var1) << 1); + else + pressure_comp = ((pressure_comp << 1) / (uint32_t)var1); + var1 = ((int32_t)calib.p9 * (int32_t)(((pressure_comp >> 3) * + (pressure_comp >> 3)) >> 13)) >> 12; + var2 = ((int32_t)(pressure_comp >> 2) * + (int32_t)calib.p8) >> 13; + var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) * + (int32_t)(pressure_comp >> 8) * + (int32_t)calib.p10) >> 17; + + pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 + + ((int32_t)calib.p7 << 7)) >> 4); + + + + + + *T=(int16_t)temp_comp; + calib.ltemp=temp_comp/100; + //*P=pressure_comp; + *H=(uint16_t)(humidity_comp/10); + //*P=rs; + *T=g1; + *H=g2; + //*P= + + +} + diff --git a/common/I2C/BME680.h b/common/I2C/BME680.h index 159d3f3..7311e1d 100644 --- a/common/I2C/BME680.h +++ b/common/I2C/BME680.h @@ -1,46 +1,46 @@ -#ifndef BME680_H -#define BME680_H -// Copyright (c) 2015, 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. - -int8_t initBME680(); -void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G); -/*void APDS9960setATime(uint8_t ATime); -void APDS9960setGain(uint8_t Gain); -uint8_t APDS9960getAVALID(); -uint8_t APDS9960getASAT(); - -void APDS9960getRGBC(uint16_t* R,uint16_t* G,uint16_t* B,uint16_t* C); -*/ - - +#ifndef BME680_H +#define BME680_H +// Copyright (c) 2015, 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. + +int8_t initBME680(); +void readBMP680(int16_t *T,uint16_t *H,uint32_t *P,uint16_t *G); +/*void APDS9960setATime(uint8_t ATime); +void APDS9960setGain(uint8_t Gain); +uint8_t APDS9960getAVALID(); +uint8_t APDS9960getASAT(); + +void APDS9960getRGBC(uint16_t* R,uint16_t* G,uint16_t* B,uint16_t* C); +*/ + + #endif \ No newline at end of file diff --git a/common/I2C/IAQCORE.c b/common/I2C/IAQCORE.c index 357912d..2512f61 100644 --- a/common/I2C/IAQCORE.c +++ b/common/I2C/IAQCORE.c @@ -1,57 +1,57 @@ -// 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. -#define F_CPU 8000000UL -#include - -#include - - -#include "USI_TWI_Master.h" -#include "IAQCORE.h" - - -uint8_t readIAQCORE(IAQCORE_Data_t *d) { - uint8_t *da=(uint8_t*)d; - I2c_StartCondition(); - I2c_WriteByte(0b10110101); - da[1]=I2c_ReadByte(ACK); - da[0]=I2c_ReadByte(ACK); - da[2]=I2c_ReadByte(ACK); - da[6]=I2c_ReadByte(ACK); - da[5]=I2c_ReadByte(ACK); - da[4]=I2c_ReadByte(ACK); - da[3]=I2c_ReadByte(ACK); - da[8]=I2c_ReadByte(ACK); - da[7]=I2c_ReadByte(NO_ACK); - I2c_StopCondition(); - return 0; -} +// 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. +#define F_CPU 8000000UL +#include + +#include + + +#include "USI_TWI_Master.h" +#include "IAQCORE.h" + + +uint8_t readIAQCORE(IAQCORE_Data_t *d) { + uint8_t *da=(uint8_t*)d; + I2c_StartCondition(); + I2c_WriteByte(0b10110101); + da[1]=I2c_ReadByte(ACK); + da[0]=I2c_ReadByte(ACK); + da[2]=I2c_ReadByte(ACK); + da[6]=I2c_ReadByte(ACK); + da[5]=I2c_ReadByte(ACK); + da[4]=I2c_ReadByte(ACK); + da[3]=I2c_ReadByte(ACK); + da[8]=I2c_ReadByte(ACK); + da[7]=I2c_ReadByte(NO_ACK); + I2c_StopCondition(); + return 0; +} diff --git a/common/I2C/IAQCORE.h b/common/I2C/IAQCORE.h index 12c538b..ac9476a 100644 --- a/common/I2C/IAQCORE.h +++ b/common/I2C/IAQCORE.h @@ -1,44 +1,44 @@ -#ifndef IAQCORE_H -#define IAQCORE_H -// Copyright (c) 2015, 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. - -typedef struct { -uint16_t CO2; -int8_t state; -int32_t resistance; -uint16_t TVOC; -} IAQCORE_Data_t; - -uint8_t readIAQCORE(IAQCORE_Data_t *d); - +#ifndef IAQCORE_H +#define IAQCORE_H +// Copyright (c) 2015, 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. + +typedef struct { +uint16_t CO2; +int8_t state; +int32_t resistance; +uint16_t TVOC; +} IAQCORE_Data_t; + +uint8_t readIAQCORE(IAQCORE_Data_t *d); + #endif \ No newline at end of file diff --git a/common/I2C/SGP30.c b/common/I2C/SGP30.c index ee19d6a..62841c4 100644 --- a/common/I2C/SGP30.c +++ b/common/I2C/SGP30.c @@ -1,228 +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 + + +#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); - -} - - + + +int8_t check_convert_buf(uint8_t *b,uint16_t *v1,uint16_t *v2) { + int8_t ret=1; + if (sensirion_common_check_crc(b,2,b[2])) { *v1=((int16_t)b[0])<<8|b[1];} else {*v1=0;ret=0;} + if (sensirion_common_check_crc(b+3,2,b[5])) {*v2=((int16_t)b[3])<<8|b[4];} else {*v2=0;ret=0;} + return ret; +} + + + +uint16_t readEEPROM(uint8_t addr,uint16_t def) { + uint16_t hr; + EEARH=0; + while(EECR & (1<>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); + +} + + diff --git a/common/I2C/SGP30.h b/common/I2C/SGP30.h index c285008..d576b18 100644 --- a/common/I2C/SGP30.h +++ b/common/I2C/SGP30.h @@ -1,39 +1,39 @@ -#ifndef SGP30_H -#define SGP30_H -// 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. - -int8_t initSGP30(); -void runSGP30(uint16_t *CO2,uint16_t *VOC,uint16_t *ETH,uint16_t *H2); - - +#ifndef SGP30_H +#define SGP30_H +// 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. + +int8_t initSGP30(); +void runSGP30(uint16_t *CO2,uint16_t *VOC,uint16_t *ETH,uint16_t *H2); + + #endif \ No newline at end of file diff --git a/programmer/programmer/programmer.asm b/programmer/programmer/programmer.asm index 5a51e4e..b08cc96 100644 --- a/programmer/programmer/programmer.asm +++ b/programmer/programmer/programmer.asm @@ -83,14 +83,14 @@ jreset: .ORG 0x0EC0 -//7552 Byte + start: cli ldi temp,0 mov zero,temp - set_clock 0x00 ;8mhz + set_clock 0x01 ;4mhz ldi mode,OWM_SLEEP - ldi temp,(1<true exception_table 2 - 1 + 0 @@ -38,7 +38,7 @@ - com.atmel.avrdbg.tool.atmelice + com.atmel.avrdbg.tool.avrdragon @@ -51,10 +51,22 @@ J41800000789 Atmel-ICE - debugWIRE + ISP - J41800000789 + 00A200054127 0x1E930C + + + + 125000 + + ISP + + com.atmel.avrdbg.tool.avrdragon + 00A200054127 + AVR Dragon + + 125000 -- 2.34.1