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

Dependents:   MiCS6814_GasSensor_Hello

Committer:
edamame22
Date:
Wed May 24 13:17:32 2017 +0900
Revision:
1:5caeba593de4
Parent:
multigas_sensor.cpp@0:e0794665caf1
Child:
3:93b7f86b72e1
Modified Some functions, and updated Doxygen comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edamame22 0:e0794665caf1 1 /**
edamame22 0:e0794665caf1 2 ******************************************************************************
edamame22 1:5caeba593de4 3 * @file MiCS6814_GasSensor.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 1:5caeba593de4 29
edamame22 0:e0794665caf1 30 #include "mbed.h"
edamame22 1:5caeba593de4 31 #include "MiCS6814_GasSensor.h"
edamame22 0:e0794665caf1 32
edamame22 0:e0794665caf1 33 MiCS6814_GasSensor::MiCS6814_GasSensor(PinName sda, PinName scl, char slave_adr)
edamame22 0:e0794665caf1 34 :
edamame22 0:e0794665caf1 35 _i2c_p(new I2C(sda, scl)),
edamame22 0:e0794665caf1 36 _i2c(*_i2c_p),
edamame22 0:e0794665caf1 37 _address(slave_adr)
edamame22 0:e0794665caf1 38 {
edamame22 0:e0794665caf1 39 initialize();
edamame22 0:e0794665caf1 40 }
edamame22 0:e0794665caf1 41
edamame22 0:e0794665caf1 42 MiCS6814_GasSensor::MiCS6814_GasSensor(I2C &i2c_obj, char slave_adr)
edamame22 0:e0794665caf1 43 :
edamame22 0:e0794665caf1 44 _i2c_p(NULL),
edamame22 0:e0794665caf1 45 _i2c(i2c_obj),
edamame22 0:e0794665caf1 46 _address(slave_adr)
edamame22 0:e0794665caf1 47 {
edamame22 0:e0794665caf1 48 initialize();
edamame22 0:e0794665caf1 49 }
edamame22 0:e0794665caf1 50
edamame22 0:e0794665caf1 51 MiCS6814_GasSensor::~MiCS6814_GasSensor()
edamame22 0:e0794665caf1 52 {
edamame22 1:5caeba593de4 53 HeaterPower(HEATER_OFF); // turn off the heater
edamame22 1:5caeba593de4 54 if (NULL != _i2c_p) {
edamame22 0:e0794665caf1 55 delete _i2c_p;
edamame22 0:e0794665caf1 56 }
edamame22 0:e0794665caf1 57 }
edamame22 0:e0794665caf1 58
edamame22 0:e0794665caf1 59 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 60 {
edamame22 0:e0794665caf1 61 char cmd[2], val[4]; // write buf, read buf
edamame22 0:e0794665caf1 62 uint8_t checksum=0;
edamame22 0:e0794665caf1 63
edamame22 0:e0794665caf1 64 if (ret_val==NULL) {
edamame22 0:e0794665caf1 65 return NULL_RETURN_BUFFER_PTR; // ret_val is a NULL pointer
edamame22 0:e0794665caf1 66 }
edamame22 1:5caeba593de4 67 if (write_len>2 || write_len<1) {
edamame22 0:e0794665caf1 68 return WRITE_LENGTH_ERROR; // write_len is wrong
edamame22 0:e0794665caf1 69 }
edamame22 0:e0794665caf1 70 if (read_len!=2 && read_len!=4) {
edamame22 0:e0794665caf1 71 return READ_LENGTH_ERROR; // read_len is wrong
edamame22 0:e0794665caf1 72 }
edamame22 0:e0794665caf1 73 *ret_val=0; // clear return buf
edamame22 0:e0794665caf1 74
edamame22 0:e0794665caf1 75 cmd[0] = addr_reg;
edamame22 0:e0794665caf1 76 if (write_len==2) {
edamame22 0:e0794665caf1 77 cmd[1] = __dta;
edamame22 0:e0794665caf1 78 }
edamame22 0:e0794665caf1 79 _i2c.write(_address, cmd, write_len);
edamame22 0:e0794665caf1 80 _i2c.read(_address, val, read_len);
edamame22 0:e0794665caf1 81 if (read_len==2) {
edamame22 0:e0794665caf1 82 *ret_val = (uint16_t) (val[0]<<8 | val[1]) ; // set return value to caller
edamame22 1:5caeba593de4 83 if (*ret_val==0) {
edamame22 1:5caeba593de4 84 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 1:5caeba593de4 85 }
edamame22 1:5caeba593de4 86 } else if (read_len==4) {
edamame22 0:e0794665caf1 87 checksum = (uint8_t)(val[0] + val[1] + val[2]);
edamame22 1:5caeba593de4 88 if (checksum != val[3]) {
edamame22 0:e0794665caf1 89 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 90 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 91 return CHECKSUM_ERROR; //checksum is wrong
edamame22 0:e0794665caf1 92 }
edamame22 0:e0794665caf1 93 *ret_val = (uint16_t) ((val[1] << 8) + val[2]);
edamame22 0:e0794665caf1 94 }
edamame22 0:e0794665caf1 95 return READ_OK;
edamame22 0:e0794665caf1 96 }
edamame22 0:e0794665caf1 97
edamame22 0:e0794665caf1 98 void MiCS6814_GasSensor::CheckFirmwareVersion()
edamame22 0:e0794665caf1 99 {
edamame22 0:e0794665caf1 100 uint16_t readBuf=0;
edamame22 0:e0794665caf1 101 unsigned char ret_val = read_Multibytes(CMD_READ_EEPROM, ADDR_IS_SET, 2, 2, &readBuf); // read firmware version
edamame22 0:e0794665caf1 102 // only support version = 2
edamame22 0:e0794665caf1 103 MBED_ASSERT(ret_val == READ_OK && readBuf == 1126);
edamame22 0:e0794665caf1 104 }
edamame22 0:e0794665caf1 105
edamame22 1:5caeba593de4 106 void MiCS6814_GasSensor::HeaterPower(const unsigned char Switch_On)
edamame22 0:e0794665caf1 107 {
edamame22 1:5caeba593de4 108 char tmp[2];
edamame22 1:5caeba593de4 109 DEBUG_PRINT("Sensor heater Power :%s\r\n", Switch_On==1? "On":"Off");
edamame22 1:5caeba593de4 110 tmp[0] = CMD_CONTROL_PWR;
edamame22 1:5caeba593de4 111 tmp[1] = Switch_On;
edamame22 0:e0794665caf1 112 _i2c.write(_address, tmp, 2);
edamame22 0:e0794665caf1 113 }
edamame22 0:e0794665caf1 114
edamame22 0:e0794665caf1 115 uint16_t MiCS6814_GasSensor::readR0_A0(unsigned char _indix)
edamame22 0:e0794665caf1 116 {
edamame22 1:5caeba593de4 117 uint16_t readBuf= 0;
edamame22 1:5caeba593de4 118 const unsigned char A0_table[3]= {ADDR_USER_ADC_HN3, ADDR_USER_ADC_CO, ADDR_USER_ADC_NO2};
edamame22 0:e0794665caf1 119 unsigned char ret_val;
edamame22 1:5caeba593de4 120
edamame22 0:e0794665caf1 121 ret_val= read_Multibytes(CMD_READ_EEPROM, A0_table[_indix], 2, 2, &readBuf);
edamame22 0:e0794665caf1 122 DEBUG_PRINT("A0_[%d]: %d, ", _indix, readBuf);
edamame22 0:e0794665caf1 123 if (ret_val == READ_OK) {
edamame22 0:e0794665caf1 124 return readBuf;
edamame22 0:e0794665caf1 125 }
edamame22 1:5caeba593de4 126 // read failed, A0_[_indix]
edamame22 0:e0794665caf1 127 DEBUG_PRINT("A0_[%d] read error: %d\r\n", _indix, ret_val);
edamame22 0:e0794665caf1 128 return 0;
edamame22 0:e0794665caf1 129 }
edamame22 0:e0794665caf1 130
edamame22 0:e0794665caf1 131 uint16_t MiCS6814_GasSensor::readRs_An(unsigned char _indix)
edamame22 0:e0794665caf1 132 {
edamame22 1:5caeba593de4 133 uint16_t readBuf= 0;
edamame22 1:5caeba593de4 134 const unsigned char An_table[3]= {CH_VALUE_NH3, CH_VALUE_CO, CH_VALUE_NO2};
edamame22 0:e0794665caf1 135 unsigned char ret_val;
edamame22 1:5caeba593de4 136
edamame22 0:e0794665caf1 137 ret_val= read_Multibytes(An_table[_indix], 0, 1, 2, &readBuf);
edamame22 0:e0794665caf1 138 DEBUG_PRINT("An_[%d]: %d, ", _indix, readBuf);
edamame22 0:e0794665caf1 139 if (ret_val == READ_OK) {
edamame22 0:e0794665caf1 140 return readBuf;
edamame22 0:e0794665caf1 141 }
edamame22 1:5caeba593de4 142 // read failed, An_[_indix]
edamame22 0:e0794665caf1 143 DEBUG_PRINT("An_[%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 float MiCS6814_GasSensor::calcGas(const GAS_TYPE gas_type)
edamame22 0:e0794665caf1 148 {
edamame22 0:e0794665caf1 149 int A0_[3], An_[3];
edamame22 1:5caeba593de4 150 const unsigned char GasType_2index[8] = {0, 1, 2, 0, 0, 1, 1, 1};
edamame22 0:e0794665caf1 151 unsigned char _index = GasType_2index[gas_type];
edamame22 1:5caeba593de4 152 float Ratio[3]; //will be calculated. Ratio 0,1,2
edamame22 0:e0794665caf1 153
edamame22 0:e0794665caf1 154 // prepare necessary Ratio[x] according to gas_type
edamame22 0:e0794665caf1 155 An_[_index] = readRs_An(_index); // read An_[x]
edamame22 0:e0794665caf1 156 A0_[_index] = readR0_A0(_index); // read R0[x]
edamame22 0:e0794665caf1 157 Ratio[_index] = (float)An_[_index]/(float)A0_[_index]*(1023.0-A0_[_index])/(1023.0-An_[_index]);
edamame22 0:e0794665caf1 158 DEBUG_PRINT("Ratio[%d]: %.3f, ", _index, Ratio[_index]);
edamame22 0:e0794665caf1 159
edamame22 0:e0794665caf1 160 float calcu_val = 0.0f;
edamame22 0:e0794665caf1 161 // calculate concentration value of specified gas_type
edamame22 1:5caeba593de4 162 switch(gas_type) {
edamame22 1:5caeba593de4 163 case CO: {
edamame22 0:e0794665caf1 164 calcu_val = pow(Ratio[1], -1.179f)*4.385f;
edamame22 0:e0794665caf1 165 break;
edamame22 0:e0794665caf1 166 }
edamame22 1:5caeba593de4 167 case NO2: {
edamame22 0:e0794665caf1 168 calcu_val = pow(Ratio[2], 1.007f)/6.855f;
edamame22 0:e0794665caf1 169 break;
edamame22 0:e0794665caf1 170 }
edamame22 1:5caeba593de4 171 case NH3: {
edamame22 0:e0794665caf1 172 calcu_val = pow(Ratio[0], -1.67f)/1.47f;
edamame22 0:e0794665caf1 173 break;
edamame22 0:e0794665caf1 174 }
edamame22 1:5caeba593de4 175 case C3H8: {
edamame22 0:e0794665caf1 176 calcu_val = pow(Ratio[0], -2.518f)*570.164f;
edamame22 0:e0794665caf1 177 break;
edamame22 0:e0794665caf1 178 }
edamame22 1:5caeba593de4 179 case C4H10: {
edamame22 0:e0794665caf1 180 calcu_val = pow(Ratio[0], -2.138f)*398.107f;
edamame22 0:e0794665caf1 181 break;
edamame22 0:e0794665caf1 182 }
edamame22 1:5caeba593de4 183 case CH4: {
edamame22 0:e0794665caf1 184 calcu_val = pow(Ratio[1], -4.363f)*630.957f;
edamame22 0:e0794665caf1 185 break;
edamame22 0:e0794665caf1 186 }
edamame22 1:5caeba593de4 187 case H2: {
edamame22 0:e0794665caf1 188 calcu_val = pow(Ratio[1], -1.8f)*0.73f;
edamame22 0:e0794665caf1 189 break;
edamame22 0:e0794665caf1 190 }
edamame22 1:5caeba593de4 191 case C2H5OH: {
edamame22 0:e0794665caf1 192 calcu_val = pow(Ratio[1], -1.552f)*1.622f;
edamame22 0:e0794665caf1 193 break;
edamame22 0:e0794665caf1 194 }
edamame22 0:e0794665caf1 195 default:
edamame22 0:e0794665caf1 196 break;
edamame22 0:e0794665caf1 197 }
edamame22 0:e0794665caf1 198 return calcu_val;
edamame22 0:e0794665caf1 199 }
edamame22 0:e0794665caf1 200
edamame22 0:e0794665caf1 201 void MiCS6814_GasSensor::initialize()
edamame22 0:e0794665caf1 202 {
edamame22 0:e0794665caf1 203 CheckFirmwareVersion();
edamame22 1:5caeba593de4 204 HeaterPower(HEATER_ON); // turn on the heater.
edamame22 0:e0794665caf1 205 }