A component library for MiCS-6814 Multichannel Gas Sensor (seeed)

Dependents:   MiCS6814_GasSensor_Hello grove_multichannel_GasSensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MiCS6814_GasSensor.cpp Source File

MiCS6814_GasSensor.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    MiCS6814_GasSensor.cpp
00004  * @author  Boting Ren
00005  * @version V1.0.1
00006  * @date    22 May 2017
00007  * @brief   MiCS6814_GasSensor class implementation
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * Permission is hereby granted, free of charge, to any person obtaining a copy
00012  * of this software and associated documentation files (the "Software"), to deal
00013  * in the Software without restriction, including without limitation the rights
00014  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015  * copies of the Software, and to permit persons to whom the Software is
00016  * furnished to do so, subject to the following conditions:
00017  *
00018  * The above copyright notice and this permission notice shall be included in
00019  * all copies or substantial portions of the Software.
00020  *
00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027  * THE SOFTWARE.
00028  */
00029 
00030 #include "mbed.h"
00031 #include "MiCS6814_GasSensor.h"
00032 
00033 MiCS6814_GasSensor::MiCS6814_GasSensor(PinName sda, PinName scl, char slave_adr)
00034     :
00035     _i2c_p(new I2C(sda, scl)),
00036     _i2c(*_i2c_p),
00037     _address(slave_adr)
00038 {
00039     initialize();
00040 }
00041 
00042 MiCS6814_GasSensor::MiCS6814_GasSensor(I2C &i2c_obj, char slave_adr)
00043     :
00044     _i2c_p(NULL),
00045     _i2c(i2c_obj),
00046     _address(slave_adr)
00047 {
00048     initialize();
00049 }
00050 
00051 MiCS6814_GasSensor::~MiCS6814_GasSensor()
00052 {
00053     HeaterPower(HEATER_OFF);    // turn off the heater
00054     if (NULL != _i2c_p) {
00055         delete  _i2c_p;
00056     }
00057 }
00058 
00059 READ_ERROR_TYPE MiCS6814_GasSensor::read_Multibytes(unsigned char addr_reg, unsigned char __dta, unsigned char write_len, unsigned char read_len, uint16_t * ret_val)
00060 {
00061     char cmd[2], val[4];     // write buf, read buf
00062     uint8_t checksum=0;
00063 
00064     if (write_len>2 || write_len<1) {
00065         return WRITE_LENGTH_ERROR;         // write_len is wrong
00066     }
00067     if (read_len!=2 && read_len!=4) {
00068         return READ_LENGTH_ERROR;          // read_len is wrong
00069     }
00070     *ret_val=0;     // clear return buf
00071 
00072     cmd[0] = addr_reg;
00073     if (write_len==2) {
00074         cmd[1] = __dta;
00075     }
00076     _i2c.write(_address, cmd, write_len);
00077     wait(0.1f);
00078     _i2c.read(_address, val, read_len);
00079     if (read_len==2) {
00080         *ret_val = (uint16_t) (val[0]<<8 | val[1]) ;   // set return value to caller
00081         if (*ret_val==0) {
00082             DEBUG_PRINT("read_Multibytes, *ret_val=0, addr_reg: 0x%x, write_len: %x, read_len: %x\r\n", addr_reg, write_len, read_len );
00083         }
00084     } else if (read_len==4) {
00085         checksum = (uint8_t)(val[0] + val[1] + val[2]);
00086         if (checksum != val[3]) {
00087             DEBUG_PRINT("checksum failed,   val[4]: 0x%x,   0x%x,   0x%x,   0x%x, checksum: 0x%x\r\n", val[0], val[1], val[2], val[3], checksum );
00088             DEBUG_PRINT("checksum failed i2c.read, addr_reg: 0x%x, write_len: %x, read_len: %x\r\n", addr_reg, write_len, read_len );
00089             return CHECKSUM_ERROR;    //checksum is wrong
00090         }
00091         *ret_val = (uint16_t) ((val[1] << 8) + val[2]);
00092     }
00093     return READ_OK;
00094 }
00095 
00096 void MiCS6814_GasSensor::CheckFirmwareVersion()
00097 {
00098     uint16_t readBuf=0;
00099     unsigned char ret_val = read_Multibytes(CMD_READ_EEPROM, ADDR_IS_SET, 2, 2, &readBuf);      // read firmware version
00100     // only support version = 2
00101     MBED_ASSERT(ret_val == READ_OK && readBuf == 1126);
00102 }
00103 
00104 void MiCS6814_GasSensor::HeaterPower(const unsigned char Switch_On)
00105 {
00106     char tmp[2];
00107     DEBUG_PRINT("Sensor heater Power :%s\r\n", Switch_On==1? "On":"Off");
00108     tmp[0] = CMD_CONTROL_PWR;
00109     tmp[1] = Switch_On;
00110     _i2c.write(_address, tmp, 2);
00111     wait(0.1f);
00112 }
00113 
00114 uint16_t MiCS6814_GasSensor::readR0_A0(unsigned char index)
00115 {
00116     uint16_t readBuf= 0;
00117     const unsigned char A0_table[3]= {ADDR_USER_ADC_HN3, ADDR_USER_ADC_CO, ADDR_USER_ADC_NO2};
00118     unsigned char ret_val;
00119 
00120     ret_val= read_Multibytes(CMD_READ_EEPROM, A0_table[index], 2, 2, &readBuf);
00121     DEBUG_PRINT("A0_[%d]: %d,    ", index, readBuf);
00122     if (ret_val == READ_OK) {
00123         return readBuf;
00124     }
00125     // read failed, A0_[index]
00126     DEBUG_PRINT("A0_[%d] read error: %d\r\n", index, ret_val);
00127     return 0;
00128 }
00129 
00130 uint16_t MiCS6814_GasSensor::readRs_An(unsigned char index)
00131 {
00132     uint16_t readBuf= 0;
00133     const unsigned char An_table[3]= {CH_VALUE_NH3, CH_VALUE_CO, CH_VALUE_NO2};
00134     unsigned char ret_val;
00135 
00136     ret_val= read_Multibytes(An_table[index], 0, 1, 2, &readBuf);
00137     DEBUG_PRINT("An_[%d]: %d,    ", index, readBuf);
00138     if (ret_val == READ_OK) {
00139         return readBuf;
00140     }
00141     // read failed, An_[index]
00142     DEBUG_PRINT("An_[%d] read error: %d\r\n", index, ret_val);
00143     return 0;
00144 }
00145 
00146 float MiCS6814_GasSensor::getGas(const GAS_TYPE gas_type)
00147 {
00148     int A0_[3], An_[3];
00149     const unsigned char GasType_2index[8] = {0, 1, 2, 0, 0, 1, 1, 1};
00150     unsigned char index = GasType_2index[gas_type];
00151     float Ratio[3];        //will be calculated. Ratio 0,1,2
00152 
00153     // prepare necessary Ratio[x] according to gas_type
00154     An_[index] = readRs_An(index);      // read An_[x]
00155     A0_[index] = readR0_A0(index);      // read R0[x]
00156     Ratio[index] = (float)An_[index]/(float)A0_[index]*(1023.0-A0_[index])/(1023.0-An_[index]);
00157     DEBUG_PRINT("Ratio[%d]: %.3f,   ", index, Ratio[index]);
00158 
00159     float calcu_val = 0.0f;
00160     // calculate concentration value of specified gas_type
00161     switch(gas_type) {
00162         case CO:
00163             calcu_val = pow(Ratio[1], -1.179f)*4.385f;
00164             break;
00165         case NO2:
00166             calcu_val = pow(Ratio[2], 1.007f)/6.855f;
00167             break;
00168         case NH3:
00169             calcu_val = pow(Ratio[0], -1.67f)/1.47f;
00170             break;
00171         case C3H8:
00172             calcu_val = pow(Ratio[0], -2.518f)*570.164f;
00173             break;
00174         case C4H10:
00175             calcu_val = pow(Ratio[0], -2.138f)*398.107f;
00176             break;
00177         case CH4:
00178             calcu_val = pow(Ratio[1], -4.363f)*630.957f;
00179             break;
00180         case H2:
00181             calcu_val = pow(Ratio[1], -1.8f)*0.73f;
00182             break;
00183         case C2H5OH:
00184             calcu_val = pow(Ratio[1], -1.552f)*1.622f;
00185             break;
00186         default:
00187             break;
00188     }
00189     return calcu_val;
00190 }
00191 
00192 void MiCS6814_GasSensor::initialize()
00193 {
00194     CheckFirmwareVersion();
00195     HeaterPower(HEATER_ON);   // turn on the heater.
00196 }