A component library for MiCS-6814 Multichannel Gas Sensor (seeed)
Dependents: MiCS6814_GasSensor_Hello grove_multichannel_GasSensor
Diff: MiCS6814_GasSensor.cpp
- Revision:
- 3:93b7f86b72e1
- Parent:
- 1:5caeba593de4
diff -r 8d61377f2e2a -r 93b7f86b72e1 MiCS6814_GasSensor.cpp --- a/MiCS6814_GasSensor.cpp Wed May 24 13:38:46 2017 +0900 +++ b/MiCS6814_GasSensor.cpp Wed May 24 10:03:01 2017 +0000 @@ -61,9 +61,6 @@ char cmd[2], val[4]; // write buf, read buf uint8_t checksum=0; - if (ret_val==NULL) { - return NULL_RETURN_BUFFER_PTR; // ret_val is a NULL pointer - } if (write_len>2 || write_len<1) { return WRITE_LENGTH_ERROR; // write_len is wrong } @@ -77,6 +74,7 @@ cmd[1] = __dta; } _i2c.write(_address, cmd, write_len); + wait(0.1f); _i2c.read(_address, val, read_len); if (read_len==2) { *ret_val = (uint16_t) (val[0]<<8 | val[1]) ; // set return value to caller @@ -110,88 +108,81 @@ tmp[0] = CMD_CONTROL_PWR; tmp[1] = Switch_On; _i2c.write(_address, tmp, 2); + wait(0.1f); } -uint16_t MiCS6814_GasSensor::readR0_A0(unsigned char _indix) +uint16_t MiCS6814_GasSensor::readR0_A0(unsigned char index) { uint16_t readBuf= 0; const unsigned char A0_table[3]= {ADDR_USER_ADC_HN3, ADDR_USER_ADC_CO, ADDR_USER_ADC_NO2}; unsigned char ret_val; - ret_val= read_Multibytes(CMD_READ_EEPROM, A0_table[_indix], 2, 2, &readBuf); - DEBUG_PRINT("A0_[%d]: %d, ", _indix, readBuf); + ret_val= read_Multibytes(CMD_READ_EEPROM, A0_table[index], 2, 2, &readBuf); + DEBUG_PRINT("A0_[%d]: %d, ", index, readBuf); if (ret_val == READ_OK) { return readBuf; } - // read failed, A0_[_indix] - DEBUG_PRINT("A0_[%d] read error: %d\r\n", _indix, ret_val); + // read failed, A0_[index] + DEBUG_PRINT("A0_[%d] read error: %d\r\n", index, ret_val); return 0; } -uint16_t MiCS6814_GasSensor::readRs_An(unsigned char _indix) +uint16_t MiCS6814_GasSensor::readRs_An(unsigned char index) { uint16_t readBuf= 0; const unsigned char An_table[3]= {CH_VALUE_NH3, CH_VALUE_CO, CH_VALUE_NO2}; unsigned char ret_val; - ret_val= read_Multibytes(An_table[_indix], 0, 1, 2, &readBuf); - DEBUG_PRINT("An_[%d]: %d, ", _indix, readBuf); + ret_val= read_Multibytes(An_table[index], 0, 1, 2, &readBuf); + DEBUG_PRINT("An_[%d]: %d, ", index, readBuf); if (ret_val == READ_OK) { return readBuf; } - // read failed, An_[_indix] - DEBUG_PRINT("An_[%d] read error: %d\r\n", _indix, ret_val); + // read failed, An_[index] + DEBUG_PRINT("An_[%d] read error: %d\r\n", index, ret_val); return 0; } -float MiCS6814_GasSensor::calcGas(const GAS_TYPE gas_type) +float MiCS6814_GasSensor::getGas(const GAS_TYPE gas_type) { int A0_[3], An_[3]; const unsigned char GasType_2index[8] = {0, 1, 2, 0, 0, 1, 1, 1}; - unsigned char _index = GasType_2index[gas_type]; + unsigned char index = GasType_2index[gas_type]; float Ratio[3]; //will be calculated. Ratio 0,1,2 // prepare necessary Ratio[x] according to gas_type - An_[_index] = readRs_An(_index); // read An_[x] - A0_[_index] = readR0_A0(_index); // read R0[x] - Ratio[_index] = (float)An_[_index]/(float)A0_[_index]*(1023.0-A0_[_index])/(1023.0-An_[_index]); - DEBUG_PRINT("Ratio[%d]: %.3f, ", _index, Ratio[_index]); + An_[index] = readRs_An(index); // read An_[x] + A0_[index] = readR0_A0(index); // read R0[x] + Ratio[index] = (float)An_[index]/(float)A0_[index]*(1023.0-A0_[index])/(1023.0-An_[index]); + DEBUG_PRINT("Ratio[%d]: %.3f, ", index, Ratio[index]); float calcu_val = 0.0f; // calculate concentration value of specified gas_type switch(gas_type) { - case CO: { + case CO: calcu_val = pow(Ratio[1], -1.179f)*4.385f; break; - } - case NO2: { + case NO2: calcu_val = pow(Ratio[2], 1.007f)/6.855f; break; - } - case NH3: { + case NH3: calcu_val = pow(Ratio[0], -1.67f)/1.47f; break; - } - case C3H8: { + case C3H8: calcu_val = pow(Ratio[0], -2.518f)*570.164f; break; - } - case C4H10: { + case C4H10: calcu_val = pow(Ratio[0], -2.138f)*398.107f; break; - } - case CH4: { + case CH4: calcu_val = pow(Ratio[1], -4.363f)*630.957f; break; - } - case H2: { + case H2: calcu_val = pow(Ratio[1], -1.8f)*0.73f; break; - } - case C2H5OH: { + case C2H5OH: calcu_val = pow(Ratio[1], -1.552f)*1.622f; break; - } default: break; }