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

Dependents:   MiCS6814_GasSensor_Hello grove_multichannel_GasSensor

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.h
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 This file contains the class of a Multichannel Gas Sensor library with I2C interface
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 Public APIs (measure_xxx())
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. added more detail doxygen comments
edamame22 0:e0794665caf1 38 *
edamame22 0:e0794665caf1 39 */
edamame22 0:e0794665caf1 40 #ifndef MBED_MULTIGAS_SENSOR_H
edamame22 0:e0794665caf1 41 #define MBED_MULTIGAS_SENSOR_H
edamame22 0:e0794665caf1 42
edamame22 0:e0794665caf1 43 #include "mbed.h"
edamame22 0:e0794665caf1 44
edamame22 0:e0794665caf1 45 //#define _DEBUG
edamame22 0:e0794665caf1 46
edamame22 0:e0794665caf1 47 #define SLAVE_ADDRESS_MiCS6814 (0x04<<1)
edamame22 0:e0794665caf1 48
edamame22 0:e0794665caf1 49 #define ADDR_IS_SET 0 // This is read at initialization time, if 1126, set
edamame22 0:e0794665caf1 50 #define ADDR_FACTORY_ADC_NH3 2
edamame22 0:e0794665caf1 51 #define ADDR_FACTORY_ADC_CO 4
edamame22 0:e0794665caf1 52 #define ADDR_FACTORY_ADC_NO2 6
edamame22 0:e0794665caf1 53
edamame22 0:e0794665caf1 54 #define ADDR_USER_ADC_HN3 8
edamame22 0:e0794665caf1 55 #define ADDR_USER_ADC_CO 10
edamame22 0:e0794665caf1 56 #define ADDR_USER_ADC_NO2 12
edamame22 0:e0794665caf1 57 #define ADDR_IF_CALI 14 // IF USER HAD CALI
edamame22 0:e0794665caf1 58
edamame22 0:e0794665caf1 59 #define ADDR_I2C_ADDRESS 20
edamame22 0:e0794665caf1 60
edamame22 0:e0794665caf1 61 #define CH_VALUE_NH3 1
edamame22 0:e0794665caf1 62 #define CH_VALUE_CO 2
edamame22 0:e0794665caf1 63 #define CH_VALUE_NO2 3
edamame22 0:e0794665caf1 64
edamame22 0:e0794665caf1 65 #define CMD_ADC_RES0 1 // NH3
edamame22 0:e0794665caf1 66 #define CMD_ADC_RES1 2 // CO
edamame22 0:e0794665caf1 67 #define CMD_ADC_RES2 3 // NO2
edamame22 0:e0794665caf1 68 #define CMD_ADC_RESALL 4 // ALL CHANNEL
edamame22 0:e0794665caf1 69 #define CMD_CHANGE_I2C 5 // CHANGE I2C
edamame22 0:e0794665caf1 70 #define CMD_READ_EEPROM 6 // READ EEPROM VALUE, RETURN UNSIGNED INT
edamame22 0:e0794665caf1 71 #define CMD_SET_R0_ADC 7 // SET R0 ADC VALUE
edamame22 0:e0794665caf1 72 #define CMD_GET_R0_ADC 8 // GET R0 ADC VALUE
edamame22 0:e0794665caf1 73 #define CMD_GET_R0_ADC_FACTORY 9 // GET FACTORY R0 ADC VALUE
edamame22 0:e0794665caf1 74 #define CMD_CONTROL_LED 10
edamame22 0:e0794665caf1 75 #define CMD_CONTROL_PWR 11
edamame22 0:e0794665caf1 76
edamame22 0:e0794665caf1 77
edamame22 0:e0794665caf1 78 #ifdef _DEBUG
edamame22 0:e0794665caf1 79 extern Serial pc;
edamame22 0:e0794665caf1 80 #define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
edamame22 0:e0794665caf1 81 #else
edamame22 0:e0794665caf1 82 #define DEBUG_PRINT(...)
edamame22 0:e0794665caf1 83 #endif
edamame22 0:e0794665caf1 84
edamame22 0:e0794665caf1 85 enum GAS_TYPE{NH3, CO, NO2, C3H8, C4H10, CH4, H2, C2H5OH};
edamame22 0:e0794665caf1 86 enum READ_ERROR_TYPE{READ_OK, NULL_RETURN_BUFFER_PTR, WRITE_LENGTH_ERROR, READ_LENGTH_ERROR, CHECKSUM_ERROR};
edamame22 0:e0794665caf1 87
edamame22 0:e0794665caf1 88 /**
edamame22 0:e0794665caf1 89 * @class MiCS6814_GasSensor
edamame22 0:e0794665caf1 90 * @brief A mbed component library to measure gas data by using Grove - Multichannel Gas Sensor (seeed)
edamame22 0:e0794665caf1 91 * Please refer the details of the sensor. http://wiki.seeed.cc/Grove-Multichannel_Gas_Sensor/
edamame22 0:e0794665caf1 92 *
edamame22 0:e0794665caf1 93 */
edamame22 0:e0794665caf1 94
edamame22 0:e0794665caf1 95 class MiCS6814_GasSensor{
edamame22 0:e0794665caf1 96
edamame22 0:e0794665caf1 97 public:
edamame22 0:e0794665caf1 98 /** Create a MiCS6814_GasSensor instance
edamame22 0:e0794665caf1 99 * the sensor is connected to specified I2C pins with specified address
edamame22 0:e0794665caf1 100 * 1. create an I2C instance
edamame22 0:e0794665caf1 101 * 2. initialize private variables.
edamame22 0:e0794665caf1 102 *
edamame22 0:e0794665caf1 103 * @param[in] sda I2C-bus SDA pin
edamame22 0:e0794665caf1 104 * @param[in] scl I2C-bus SCL pin
edamame22 0:e0794665caf1 105 * @param[in] slave_adr (option) I2C-bus address (default: 0x04<<1)
edamame22 0:e0794665caf1 106 * @par sample code (K64F)
edamame22 0:e0794665caf1 107 * MiCS6814_GasSensor gas(I2C_SDA, I2C_SCL);
edamame22 0:e0794665caf1 108 */
edamame22 0:e0794665caf1 109 MiCS6814_GasSensor(PinName sda, PinName sck, char slave_adr = SLAVE_ADDRESS_MiCS6814);
edamame22 0:e0794665caf1 110
edamame22 0:e0794665caf1 111 /** Create a MiCS6814_GasSensor instance
edamame22 0:e0794665caf1 112 * the sensor is connected to specified I2C pins with specified address
edamame22 0:e0794665caf1 113 * 1. pass an I2C instance,
edamame22 0:e0794665caf1 114 * 2. then initialize private variables.
edamame22 0:e0794665caf1 115 *
edamame22 0:e0794665caf1 116 * @param[in] i2c_obj I2C object (instance)
edamame22 0:e0794665caf1 117 * @param[in] slave_adr (option) I2C-bus address (default: 0x04<<1)
edamame22 0:e0794665caf1 118 * @par sample code (K64F)
edamame22 0:e0794665caf1 119 * I2C MySlave(I2C_SDA, I2C_SC);
edamame22 0:e0794665caf1 120 * MiCS6814_GasSensor gas(MySlave);
edamame22 0:e0794665caf1 121 */
edamame22 0:e0794665caf1 122 MiCS6814_GasSensor(I2C &i2c_obj, char slave_adr = SLAVE_ADDRESS_MiCS6814);
edamame22 0:e0794665caf1 123
edamame22 0:e0794665caf1 124 /** Destructor of MiCS6814_GasSensor
edamame22 0:e0794665caf1 125 * 1. Power off heater
edamame22 0:e0794665caf1 126 * 2. Release allocated heap memory.
edamame22 0:e0794665caf1 127 */
edamame22 0:e0794665caf1 128 virtual ~MiCS6814_GasSensor();
edamame22 0:e0794665caf1 129
edamame22 0:e0794665caf1 130 /** Initialize MiCS6814_GasSensor
edamame22 0:e0794665caf1 131 * 1. Read firmware version from sensor
edamame22 0:e0794665caf1 132 * 2. Power on heater
edamame22 0:e0794665caf1 133 * 3. Clear private variables
edamame22 0:e0794665caf1 134 */
edamame22 0:e0794665caf1 135 void initialize(void);
edamame22 0:e0794665caf1 136
edamame22 0:e0794665caf1 137 /** Return a specific measured value, unit: ppm
edamame22 0:e0794665caf1 138 *
edamame22 0:e0794665caf1 139 * @param[in] gas_type one of gas type defined at enum GAS_TYPE
edamame22 0:e0794665caf1 140 * @return the measured concentration of specific gas type (ppm)
edamame22 0:e0794665caf1 141 * @par sample code
edamame22 0:e0794665caf1 142 * val = calcGas(C3H8);
edamame22 0:e0794665caf1 143 */
edamame22 0:e0794665caf1 144 float calcGas(const enum GAS_TYPE gas_type);
edamame22 0:e0794665caf1 145
edamame22 0:e0794665caf1 146 private:
edamame22 0:e0794665caf1 147
edamame22 0:e0794665caf1 148 I2C *_i2c_p;
edamame22 0:e0794665caf1 149 I2C &_i2c;
edamame22 0:e0794665caf1 150 char _address; //I2C address of this MCU
edamame22 0:e0794665caf1 151 /** Check firmware version of sensor
edamame22 0:e0794665caf1 152 * only support 2
edamame22 0:e0794665caf1 153 */
edamame22 0:e0794665caf1 154 void CheckFirmwareVersion(void);
edamame22 0:e0794665caf1 155
edamame22 0:e0794665caf1 156 /** Turn On/Off sensor heater
edamame22 0:e0794665caf1 157 *
edamame22 0:e0794665caf1 158 * @param[in] Heater_On_flag True: Turn on, False: Turn off
edamame22 0:e0794665caf1 159 */
edamame22 0:e0794665caf1 160 void HeaterPower(bool Heater_On_flag);
edamame22 0:e0794665caf1 161 /** Read 16-bit data from sensor
edamame22 0:e0794665caf1 162 *
edamame22 0:e0794665caf1 163 * @param[in] reg_addr register adderss
edamame22 0:e0794665caf1 164 * @param[in] data_4reg data for the register, it will be Zero in case no input data for above register.
edamame22 0:e0794665caf1 165 * @param[in] write_length length for I2C writing, should be 1 or 2
edamame22 0:e0794665caf1 166 * @param[in] read_length length for I2C reading, should be 2 or 4
edamame22 0:e0794665caf1 167 * @param[out] pointer for read result buffer 16-bit
edamame22 0:e0794665caf1 168 * @return status
edamame22 0:e0794665caf1 169 */
edamame22 0:e0794665caf1 170 READ_ERROR_TYPE read_Multibytes(unsigned char reg_addr, unsigned char data_4reg ,unsigned char write_length, unsigned char read_length, uint16_t *readBuf);
edamame22 0:e0794665caf1 171
edamame22 0:e0794665caf1 172 /** Read A0_[x] of sensor
edamame22 0:e0794665caf1 173 *
edamame22 0:e0794665caf1 174 * @param[in] index {0,1,2}
edamame22 0:e0794665caf1 175 * @return return A0_[x] for version 2
edamame22 0:e0794665caf1 176 */
edamame22 0:e0794665caf1 177 uint16_t readR0_A0(unsigned char _indix);
edamame22 0:e0794665caf1 178
edamame22 0:e0794665caf1 179 /** Read An_[x] of sensor
edamame22 0:e0794665caf1 180 *
edamame22 0:e0794665caf1 181 * @param[in] index {0,1,2}
edamame22 0:e0794665caf1 182 * @return return An_[x] for version 2
edamame22 0:e0794665caf1 183 */
edamame22 0:e0794665caf1 184 uint16_t readRs_An(unsigned char _indix);
edamame22 0:e0794665caf1 185
edamame22 0:e0794665caf1 186 };
edamame22 0:e0794665caf1 187
edamame22 0:e0794665caf1 188
edamame22 0:e0794665caf1 189
edamame22 0:e0794665caf1 190 #endif // MBED_MULTIGAS_SENSOR_H