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

Dependents:   MiCS6814_GasSensor_Hello

Committer:
edamame22
Date:
Thu May 25 03:16:47 2017 +0000
Revision:
4:a15eee9bb0e6
Parent:
3:93b7f86b72e1
added pin name definition for I2C in header file.

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 1:5caeba593de4 64 if (write_len>2 || write_len<1) {
edamame22 0:e0794665caf1 65 return WRITE_LENGTH_ERROR; // write_len is wrong
edamame22 0:e0794665caf1 66 }
edamame22 0:e0794665caf1 67 if (read_len!=2 && read_len!=4) {
edamame22 0:e0794665caf1 68 return READ_LENGTH_ERROR; // read_len is wrong
edamame22 0:e0794665caf1 69 }
edamame22 0:e0794665caf1 70 *ret_val=0; // clear return buf
edamame22 0:e0794665caf1 71
edamame22 0:e0794665caf1 72 cmd[0] = addr_reg;
edamame22 0:e0794665caf1 73 if (write_len==2) {
edamame22 0:e0794665caf1 74 cmd[1] = __dta;
edamame22 0:e0794665caf1 75 }
edamame22 0:e0794665caf1 76 _i2c.write(_address, cmd, write_len);
MACRUM 3:93b7f86b72e1 77 wait(0.1f);
edamame22 0:e0794665caf1 78 _i2c.read(_address, val, read_len);
edamame22 0:e0794665caf1 79 if (read_len==2) {
edamame22 0:e0794665caf1 80 *ret_val = (uint16_t) (val[0]<<8 | val[1]) ; // set return value to caller
edamame22 1:5caeba593de4 81 if (*ret_val==0) {
edamame22 1:5caeba593de4 82 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 83 }
edamame22 1:5caeba593de4 84 } else if (read_len==4) {
edamame22 0:e0794665caf1 85 checksum = (uint8_t)(val[0] + val[1] + val[2]);
edamame22 1:5caeba593de4 86 if (checksum != val[3]) {
edamame22 0:e0794665caf1 87 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 88 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 89 return CHECKSUM_ERROR; //checksum is wrong
edamame22 0:e0794665caf1 90 }
edamame22 0:e0794665caf1 91 *ret_val = (uint16_t) ((val[1] << 8) + val[2]);
edamame22 0:e0794665caf1 92 }
edamame22 0:e0794665caf1 93 return READ_OK;
edamame22 0:e0794665caf1 94 }
edamame22 0:e0794665caf1 95
edamame22 0:e0794665caf1 96 void MiCS6814_GasSensor::CheckFirmwareVersion()
edamame22 0:e0794665caf1 97 {
edamame22 0:e0794665caf1 98 uint16_t readBuf=0;
edamame22 0:e0794665caf1 99 unsigned char ret_val = read_Multibytes(CMD_READ_EEPROM, ADDR_IS_SET, 2, 2, &readBuf); // read firmware version
edamame22 0:e0794665caf1 100 // only support version = 2
edamame22 0:e0794665caf1 101 MBED_ASSERT(ret_val == READ_OK && readBuf == 1126);
edamame22 0:e0794665caf1 102 }
edamame22 0:e0794665caf1 103
edamame22 1:5caeba593de4 104 void MiCS6814_GasSensor::HeaterPower(const unsigned char Switch_On)
edamame22 0:e0794665caf1 105 {
edamame22 1:5caeba593de4 106 char tmp[2];
edamame22 1:5caeba593de4 107 DEBUG_PRINT("Sensor heater Power :%s\r\n", Switch_On==1? "On":"Off");
edamame22 1:5caeba593de4 108 tmp[0] = CMD_CONTROL_PWR;
edamame22 1:5caeba593de4 109 tmp[1] = Switch_On;
edamame22 0:e0794665caf1 110 _i2c.write(_address, tmp, 2);
MACRUM 3:93b7f86b72e1 111 wait(0.1f);
edamame22 0:e0794665caf1 112 }
edamame22 0:e0794665caf1 113
MACRUM 3:93b7f86b72e1 114 uint16_t MiCS6814_GasSensor::readR0_A0(unsigned char index)
edamame22 0:e0794665caf1 115 {
edamame22 1:5caeba593de4 116 uint16_t readBuf= 0;
edamame22 1:5caeba593de4 117 const unsigned char A0_table[3]= {ADDR_USER_ADC_HN3, ADDR_USER_ADC_CO, ADDR_USER_ADC_NO2};
edamame22 0:e0794665caf1 118 unsigned char ret_val;
edamame22 1:5caeba593de4 119
MACRUM 3:93b7f86b72e1 120 ret_val= read_Multibytes(CMD_READ_EEPROM, A0_table[index], 2, 2, &readBuf);
MACRUM 3:93b7f86b72e1 121 DEBUG_PRINT("A0_[%d]: %d, ", index, readBuf);
edamame22 0:e0794665caf1 122 if (ret_val == READ_OK) {
edamame22 0:e0794665caf1 123 return readBuf;
edamame22 0:e0794665caf1 124 }
MACRUM 3:93b7f86b72e1 125 // read failed, A0_[index]
MACRUM 3:93b7f86b72e1 126 DEBUG_PRINT("A0_[%d] read error: %d\r\n", index, ret_val);
edamame22 0:e0794665caf1 127 return 0;
edamame22 0:e0794665caf1 128 }
edamame22 0:e0794665caf1 129
MACRUM 3:93b7f86b72e1 130 uint16_t MiCS6814_GasSensor::readRs_An(unsigned char index)
edamame22 0:e0794665caf1 131 {
edamame22 1:5caeba593de4 132 uint16_t readBuf= 0;
edamame22 1:5caeba593de4 133 const unsigned char An_table[3]= {CH_VALUE_NH3, CH_VALUE_CO, CH_VALUE_NO2};
edamame22 0:e0794665caf1 134 unsigned char ret_val;
edamame22 1:5caeba593de4 135
MACRUM 3:93b7f86b72e1 136 ret_val= read_Multibytes(An_table[index], 0, 1, 2, &readBuf);
MACRUM 3:93b7f86b72e1 137 DEBUG_PRINT("An_[%d]: %d, ", index, readBuf);
edamame22 0:e0794665caf1 138 if (ret_val == READ_OK) {
edamame22 0:e0794665caf1 139 return readBuf;
edamame22 0:e0794665caf1 140 }
MACRUM 3:93b7f86b72e1 141 // read failed, An_[index]
MACRUM 3:93b7f86b72e1 142 DEBUG_PRINT("An_[%d] read error: %d\r\n", index, ret_val);
edamame22 0:e0794665caf1 143 return 0;
edamame22 0:e0794665caf1 144 }
edamame22 0:e0794665caf1 145
MACRUM 3:93b7f86b72e1 146 float MiCS6814_GasSensor::getGas(const GAS_TYPE gas_type)
edamame22 0:e0794665caf1 147 {
edamame22 0:e0794665caf1 148 int A0_[3], An_[3];
edamame22 1:5caeba593de4 149 const unsigned char GasType_2index[8] = {0, 1, 2, 0, 0, 1, 1, 1};
MACRUM 3:93b7f86b72e1 150 unsigned char index = GasType_2index[gas_type];
edamame22 1:5caeba593de4 151 float Ratio[3]; //will be calculated. Ratio 0,1,2
edamame22 0:e0794665caf1 152
edamame22 0:e0794665caf1 153 // prepare necessary Ratio[x] according to gas_type
MACRUM 3:93b7f86b72e1 154 An_[index] = readRs_An(index); // read An_[x]
MACRUM 3:93b7f86b72e1 155 A0_[index] = readR0_A0(index); // read R0[x]
MACRUM 3:93b7f86b72e1 156 Ratio[index] = (float)An_[index]/(float)A0_[index]*(1023.0-A0_[index])/(1023.0-An_[index]);
MACRUM 3:93b7f86b72e1 157 DEBUG_PRINT("Ratio[%d]: %.3f, ", index, Ratio[index]);
edamame22 0:e0794665caf1 158
edamame22 0:e0794665caf1 159 float calcu_val = 0.0f;
edamame22 0:e0794665caf1 160 // calculate concentration value of specified gas_type
edamame22 1:5caeba593de4 161 switch(gas_type) {
MACRUM 3:93b7f86b72e1 162 case CO:
edamame22 0:e0794665caf1 163 calcu_val = pow(Ratio[1], -1.179f)*4.385f;
edamame22 0:e0794665caf1 164 break;
MACRUM 3:93b7f86b72e1 165 case NO2:
edamame22 0:e0794665caf1 166 calcu_val = pow(Ratio[2], 1.007f)/6.855f;
edamame22 0:e0794665caf1 167 break;
MACRUM 3:93b7f86b72e1 168 case NH3:
edamame22 0:e0794665caf1 169 calcu_val = pow(Ratio[0], -1.67f)/1.47f;
edamame22 0:e0794665caf1 170 break;
MACRUM 3:93b7f86b72e1 171 case C3H8:
edamame22 0:e0794665caf1 172 calcu_val = pow(Ratio[0], -2.518f)*570.164f;
edamame22 0:e0794665caf1 173 break;
MACRUM 3:93b7f86b72e1 174 case C4H10:
edamame22 0:e0794665caf1 175 calcu_val = pow(Ratio[0], -2.138f)*398.107f;
edamame22 0:e0794665caf1 176 break;
MACRUM 3:93b7f86b72e1 177 case CH4:
edamame22 0:e0794665caf1 178 calcu_val = pow(Ratio[1], -4.363f)*630.957f;
edamame22 0:e0794665caf1 179 break;
MACRUM 3:93b7f86b72e1 180 case H2:
edamame22 0:e0794665caf1 181 calcu_val = pow(Ratio[1], -1.8f)*0.73f;
edamame22 0:e0794665caf1 182 break;
MACRUM 3:93b7f86b72e1 183 case C2H5OH:
edamame22 0:e0794665caf1 184 calcu_val = pow(Ratio[1], -1.552f)*1.622f;
edamame22 0:e0794665caf1 185 break;
edamame22 0:e0794665caf1 186 default:
edamame22 0:e0794665caf1 187 break;
edamame22 0:e0794665caf1 188 }
edamame22 0:e0794665caf1 189 return calcu_val;
edamame22 0:e0794665caf1 190 }
edamame22 0:e0794665caf1 191
edamame22 0:e0794665caf1 192 void MiCS6814_GasSensor::initialize()
edamame22 0:e0794665caf1 193 {
edamame22 0:e0794665caf1 194 CheckFirmwareVersion();
edamame22 1:5caeba593de4 195 HeaterPower(HEATER_ON); // turn on the heater.
edamame22 0:e0794665caf1 196 }