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

Dependents:   MiCS6814_GasSensor_Hello

Committer:
edamame22
Date:
Wed May 24 01:43:23 2017 +0000
Revision:
0:e0794665caf1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edamame22 0:e0794665caf1 1 /**
edamame22 0:e0794665caf1 2 ******************************************************************************
edamame22 0:e0794665caf1 3 * @file multigas_sensor.cpp
edamame22 0:e0794665caf1 4 * @author Boting Ren
edamame22 0:e0794665caf1 5 * @version V1.0.1
edamame22 0:e0794665caf1 6 * @date 22 May 2017
edamame22 0:e0794665caf1 7 * @brief MiCS6814_GasSensor class implementation
edamame22 0:e0794665caf1 8 ******************************************************************************
edamame22 0:e0794665caf1 9 * @attention
edamame22 0:e0794665caf1 10 *
edamame22 0:e0794665caf1 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
edamame22 0:e0794665caf1 12 * of this software and associated documentation files (the "Software"), to deal
edamame22 0:e0794665caf1 13 * in the Software without restriction, including without limitation the rights
edamame22 0:e0794665caf1 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
edamame22 0:e0794665caf1 15 * copies of the Software, and to permit persons to whom the Software is
edamame22 0:e0794665caf1 16 * furnished to do so, subject to the following conditions:
edamame22 0:e0794665caf1 17 *
edamame22 0:e0794665caf1 18 * The above copyright notice and this permission notice shall be included in
edamame22 0:e0794665caf1 19 * all copies or substantial portions of the Software.
edamame22 0:e0794665caf1 20 *
edamame22 0:e0794665caf1 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
edamame22 0:e0794665caf1 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
edamame22 0:e0794665caf1 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
edamame22 0:e0794665caf1 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
edamame22 0:e0794665caf1 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
edamame22 0:e0794665caf1 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
edamame22 0:e0794665caf1 27 * THE SOFTWARE.
edamame22 0:e0794665caf1 28 *
edamame22 0:e0794665caf1 29 * @par History
edamame22 0:e0794665caf1 30 * - 19 May 2017, V1.0.0, Boting Ren
edamame22 0:e0794665caf1 31 * - 22 May 2017, V1.0.1, Boting Ren
edamame22 0:e0794665caf1 32 * 1. changed class name and private variables name
edamame22 0:e0794665caf1 33 * 2. removed printf output
edamame22 0:e0794665caf1 34 * 3. removed removed timer and Cache feature
edamame22 0:e0794665caf1 35 * 4. removed code working for version 1 firmware
edamame22 0:e0794665caf1 36 * 5. moved class private variables into member functions
edamame22 0:e0794665caf1 37 * 6. other small improvements
edamame22 0:e0794665caf1 38 *
edamame22 0:e0794665caf1 39 */
edamame22 0:e0794665caf1 40 #include "mbed.h"
edamame22 0:e0794665caf1 41 #include "multigas_sensor.h"
edamame22 0:e0794665caf1 42
edamame22 0:e0794665caf1 43 MiCS6814_GasSensor::MiCS6814_GasSensor(PinName sda, PinName scl, char slave_adr)
edamame22 0:e0794665caf1 44 :
edamame22 0:e0794665caf1 45 _i2c_p(new I2C(sda, scl)),
edamame22 0:e0794665caf1 46 _i2c(*_i2c_p),
edamame22 0:e0794665caf1 47 _address(slave_adr)
edamame22 0:e0794665caf1 48 {
edamame22 0:e0794665caf1 49 initialize();
edamame22 0:e0794665caf1 50 }
edamame22 0:e0794665caf1 51
edamame22 0:e0794665caf1 52 MiCS6814_GasSensor::MiCS6814_GasSensor(I2C &i2c_obj, char slave_adr)
edamame22 0:e0794665caf1 53 :
edamame22 0:e0794665caf1 54 _i2c_p(NULL),
edamame22 0:e0794665caf1 55 _i2c(i2c_obj),
edamame22 0:e0794665caf1 56 _address(slave_adr)
edamame22 0:e0794665caf1 57 {
edamame22 0:e0794665caf1 58 initialize();
edamame22 0:e0794665caf1 59 }
edamame22 0:e0794665caf1 60
edamame22 0:e0794665caf1 61 MiCS6814_GasSensor::~MiCS6814_GasSensor()
edamame22 0:e0794665caf1 62 {
edamame22 0:e0794665caf1 63 HeaterPower(false); // turn off the heater
edamame22 0:e0794665caf1 64 if (NULL != _i2c_p)
edamame22 0:e0794665caf1 65 {
edamame22 0:e0794665caf1 66 delete _i2c_p;
edamame22 0:e0794665caf1 67 }
edamame22 0:e0794665caf1 68 }
edamame22 0:e0794665caf1 69
edamame22 0:e0794665caf1 70
edamame22 0:e0794665caf1 71 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)
edamame22 0:e0794665caf1 72 {
edamame22 0:e0794665caf1 73 char cmd[2], val[4]; // write buf, read buf
edamame22 0:e0794665caf1 74 uint8_t checksum=0;
edamame22 0:e0794665caf1 75
edamame22 0:e0794665caf1 76 if (ret_val==NULL) {
edamame22 0:e0794665caf1 77 return NULL_RETURN_BUFFER_PTR; // ret_val is a NULL pointer
edamame22 0:e0794665caf1 78 }
edamame22 0:e0794665caf1 79 if(write_len>2 || write_len<1) {
edamame22 0:e0794665caf1 80 return WRITE_LENGTH_ERROR; // write_len is wrong
edamame22 0:e0794665caf1 81 }
edamame22 0:e0794665caf1 82 if (read_len!=2 && read_len!=4) {
edamame22 0:e0794665caf1 83 return READ_LENGTH_ERROR; // read_len is wrong
edamame22 0:e0794665caf1 84 }
edamame22 0:e0794665caf1 85 *ret_val=0; // clear return buf
edamame22 0:e0794665caf1 86
edamame22 0:e0794665caf1 87 cmd[0] = addr_reg;
edamame22 0:e0794665caf1 88 if (write_len==2) {
edamame22 0:e0794665caf1 89 cmd[1] = __dta;
edamame22 0:e0794665caf1 90 }
edamame22 0:e0794665caf1 91 _i2c.write(_address, cmd, write_len);
edamame22 0:e0794665caf1 92
edamame22 0:e0794665caf1 93 _i2c.read(_address, val, read_len);
edamame22 0:e0794665caf1 94
edamame22 0:e0794665caf1 95 if (read_len==2) {
edamame22 0:e0794665caf1 96 *ret_val = (uint16_t) (val[0]<<8 | val[1]) ; // set return value to caller
edamame22 0:e0794665caf1 97 #ifdef _DEBUG
edamame22 0:e0794665caf1 98 if(*ret_val==0) 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 );
edamame22 0:e0794665caf1 99 #endif
edamame22 0:e0794665caf1 100 }else if (read_len==4) {
edamame22 0:e0794665caf1 101 checksum = (uint8_t)(val[0] + val[1] + val[2]);
edamame22 0:e0794665caf1 102 if(checksum != val[3]){
edamame22 0:e0794665caf1 103 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 );
edamame22 0:e0794665caf1 104 DEBUG_PRINT("checksum failed i2c.read, addr_reg: 0x%x, write_len: %x, read_len: %x\r\n", addr_reg, write_len, read_len );
edamame22 0:e0794665caf1 105 return CHECKSUM_ERROR; //checksum is wrong
edamame22 0:e0794665caf1 106 }
edamame22 0:e0794665caf1 107 *ret_val = (uint16_t) ((val[1] << 8) + val[2]);
edamame22 0:e0794665caf1 108 }
edamame22 0:e0794665caf1 109 return READ_OK;
edamame22 0:e0794665caf1 110 }
edamame22 0:e0794665caf1 111
edamame22 0:e0794665caf1 112 void MiCS6814_GasSensor::CheckFirmwareVersion()
edamame22 0:e0794665caf1 113 {
edamame22 0:e0794665caf1 114 uint16_t readBuf=0;
edamame22 0:e0794665caf1 115 unsigned char ret_val = read_Multibytes(CMD_READ_EEPROM, ADDR_IS_SET, 2, 2, &readBuf); // read firmware version
edamame22 0:e0794665caf1 116 // only support version = 2
edamame22 0:e0794665caf1 117 MBED_ASSERT(ret_val == READ_OK && readBuf == 1126);
edamame22 0:e0794665caf1 118 }
edamame22 0:e0794665caf1 119
edamame22 0:e0794665caf1 120
edamame22 0:e0794665caf1 121 void MiCS6814_GasSensor::HeaterPower(bool Switch_On)
edamame22 0:e0794665caf1 122 {
edamame22 0:e0794665caf1 123 const unsigned char v2tmp[2]={0, 1}; // On, Off command for version 2
edamame22 0:e0794665caf1 124 char tmp[4];
edamame22 0:e0794665caf1 125 DEBUG_PRINT("Sensor heater Power :%s\r\n", Switch_On? "On":"Off");
edamame22 0:e0794665caf1 126 tmp[0] = 11;
edamame22 0:e0794665caf1 127 tmp[1] = v2tmp[Switch_On];
edamame22 0:e0794665caf1 128 _i2c.write(_address, tmp, 2);
edamame22 0:e0794665caf1 129 }
edamame22 0:e0794665caf1 130
edamame22 0:e0794665caf1 131 uint16_t MiCS6814_GasSensor::readR0_A0(unsigned char _indix)
edamame22 0:e0794665caf1 132 {
edamame22 0:e0794665caf1 133 uint16_t readBuf=0;
edamame22 0:e0794665caf1 134 const unsigned char A0_table[3]={ADDR_USER_ADC_HN3, ADDR_USER_ADC_CO, ADDR_USER_ADC_NO2};
edamame22 0:e0794665caf1 135 unsigned char ret_val;
edamame22 0:e0794665caf1 136 // version 2 : read A0_[_indix]
edamame22 0:e0794665caf1 137 ret_val= read_Multibytes(CMD_READ_EEPROM, A0_table[_indix], 2, 2, &readBuf);
edamame22 0:e0794665caf1 138 DEBUG_PRINT("A0_[%d]: %d, ", _indix, readBuf);
edamame22 0:e0794665caf1 139 if (ret_val == READ_OK) {
edamame22 0:e0794665caf1 140 return readBuf;
edamame22 0:e0794665caf1 141 }
edamame22 0:e0794665caf1 142 // read failed, Rs[_indix]
edamame22 0:e0794665caf1 143 DEBUG_PRINT("A0_[%d] read error: %d\r\n", _indix, ret_val);
edamame22 0:e0794665caf1 144 return 0;
edamame22 0:e0794665caf1 145 }
edamame22 0:e0794665caf1 146
edamame22 0:e0794665caf1 147
edamame22 0:e0794665caf1 148 uint16_t MiCS6814_GasSensor::readRs_An(unsigned char _indix)
edamame22 0:e0794665caf1 149 {
edamame22 0:e0794665caf1 150 uint16_t readBuf=0;
edamame22 0:e0794665caf1 151 const unsigned char An_table[3]={CH_VALUE_NH3, CH_VALUE_CO, CH_VALUE_NO2};
edamame22 0:e0794665caf1 152 unsigned char ret_val;
edamame22 0:e0794665caf1 153 // version 2 : read An_[_indix]
edamame22 0:e0794665caf1 154 ret_val= read_Multibytes(An_table[_indix], 0, 1, 2, &readBuf);
edamame22 0:e0794665caf1 155 DEBUG_PRINT("An_[%d]: %d, ", _indix, readBuf);
edamame22 0:e0794665caf1 156 if (ret_val == READ_OK) {
edamame22 0:e0794665caf1 157 return readBuf;
edamame22 0:e0794665caf1 158 }
edamame22 0:e0794665caf1 159 // read failed, Rs[_indix]
edamame22 0:e0794665caf1 160 DEBUG_PRINT("An_[%d] read error: %d\r\n", _indix, ret_val);
edamame22 0:e0794665caf1 161 return 0;
edamame22 0:e0794665caf1 162 }
edamame22 0:e0794665caf1 163
edamame22 0:e0794665caf1 164
edamame22 0:e0794665caf1 165 float MiCS6814_GasSensor::calcGas(const GAS_TYPE gas_type)
edamame22 0:e0794665caf1 166 {
edamame22 0:e0794665caf1 167 int A0_[3], An_[3];
edamame22 0:e0794665caf1 168 const unsigned char GasType_2index[8] ={/* NH3 */ 0,/* CO */ 1,/* NO2 */ 2,/*C3H8 */ 0,/* C4H10 */ 0,/* CH4 */ 1,/* H2 */ 1,/* C2H5OH */ 1};
edamame22 0:e0794665caf1 169 unsigned char _index = GasType_2index[gas_type];
edamame22 0:e0794665caf1 170 float Ratio[3]; //will be calculated. ration 0,1,2
edamame22 0:e0794665caf1 171
edamame22 0:e0794665caf1 172 // prepare necessary Ratio[x] according to gas_type
edamame22 0:e0794665caf1 173 An_[_index] = readRs_An(_index); // read An_[x]
edamame22 0:e0794665caf1 174 A0_[_index] = readR0_A0(_index); // read R0[x]
edamame22 0:e0794665caf1 175 Ratio[_index] = (float)An_[_index]/(float)A0_[_index]*(1023.0-A0_[_index])/(1023.0-An_[_index]);
edamame22 0:e0794665caf1 176 DEBUG_PRINT("Ratio[%d]: %.3f, ", _index, Ratio[_index]);
edamame22 0:e0794665caf1 177
edamame22 0:e0794665caf1 178 float calcu_val = 0.0f;
edamame22 0:e0794665caf1 179 // calculate concentration value of specified gas_type
edamame22 0:e0794665caf1 180 switch(gas_type)
edamame22 0:e0794665caf1 181 {
edamame22 0:e0794665caf1 182 case CO:
edamame22 0:e0794665caf1 183 {
edamame22 0:e0794665caf1 184 calcu_val = pow(Ratio[1], -1.179f)*4.385f;
edamame22 0:e0794665caf1 185 break;
edamame22 0:e0794665caf1 186 }
edamame22 0:e0794665caf1 187 case NO2:
edamame22 0:e0794665caf1 188 {
edamame22 0:e0794665caf1 189 calcu_val = pow(Ratio[2], 1.007f)/6.855f;
edamame22 0:e0794665caf1 190 break;
edamame22 0:e0794665caf1 191 }
edamame22 0:e0794665caf1 192 case NH3:
edamame22 0:e0794665caf1 193 {
edamame22 0:e0794665caf1 194 calcu_val = pow(Ratio[0], -1.67f)/1.47f;
edamame22 0:e0794665caf1 195 break;
edamame22 0:e0794665caf1 196 }
edamame22 0:e0794665caf1 197 case C3H8:
edamame22 0:e0794665caf1 198 {
edamame22 0:e0794665caf1 199 calcu_val = pow(Ratio[0], -2.518f)*570.164f;
edamame22 0:e0794665caf1 200 break;
edamame22 0:e0794665caf1 201 }
edamame22 0:e0794665caf1 202 case C4H10:
edamame22 0:e0794665caf1 203 {
edamame22 0:e0794665caf1 204 calcu_val = pow(Ratio[0], -2.138f)*398.107f;
edamame22 0:e0794665caf1 205 break;
edamame22 0:e0794665caf1 206 }
edamame22 0:e0794665caf1 207 case CH4:
edamame22 0:e0794665caf1 208 {
edamame22 0:e0794665caf1 209 calcu_val = pow(Ratio[1], -4.363f)*630.957f;
edamame22 0:e0794665caf1 210 break;
edamame22 0:e0794665caf1 211 }
edamame22 0:e0794665caf1 212 case H2:
edamame22 0:e0794665caf1 213 {
edamame22 0:e0794665caf1 214 calcu_val = pow(Ratio[1], -1.8f)*0.73f;
edamame22 0:e0794665caf1 215 break;
edamame22 0:e0794665caf1 216 }
edamame22 0:e0794665caf1 217 case C2H5OH:
edamame22 0:e0794665caf1 218 {
edamame22 0:e0794665caf1 219 calcu_val = pow(Ratio[1], -1.552f)*1.622f;
edamame22 0:e0794665caf1 220 break;
edamame22 0:e0794665caf1 221 }
edamame22 0:e0794665caf1 222 default:
edamame22 0:e0794665caf1 223 break;
edamame22 0:e0794665caf1 224 }
edamame22 0:e0794665caf1 225
edamame22 0:e0794665caf1 226 return calcu_val;
edamame22 0:e0794665caf1 227 }
edamame22 0:e0794665caf1 228
edamame22 0:e0794665caf1 229
edamame22 0:e0794665caf1 230
edamame22 0:e0794665caf1 231 void MiCS6814_GasSensor::initialize()
edamame22 0:e0794665caf1 232 {
edamame22 0:e0794665caf1 233
edamame22 0:e0794665caf1 234 CheckFirmwareVersion();
edamame22 0:e0794665caf1 235 HeaterPower(true); // turn on the heater.
edamame22 0:e0794665caf1 236 }