Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CDM7160.h
00001 /** 00002 * Project: CDM7160 00003 * Company: yosensi.io 00004 * CodeWriter: Michal Kozlowski 00005 * File: CDM7160.h 00006 * Date: 09-09-2019 00007 * Description: CDM7160 Driver Header File 00008 */ 00009 00010 /** 00011 * #include "mbed.h" 00012 * #include "CDM7160.h" 00013 * 00014 * DigitalIn busy(PC_8); 00015 * CDM7160 CO2(PB_9, PB_8); 00016 * InterruptIn sw(USER_BUTTON); 00017 * InterruptIn irq(PC_9); 00018 * 00019 * volatile bool button, isIrq; 00020 * 00021 * void buttonCallback(){ 00022 * button = true; 00023 * } 00024 * 00025 * void irqCallback(){ 00026 * isIrq = true; 00027 * } 00028 * 00029 * int main() 00030 * { 00031 * int i=0; // Number of measurement 00032 * irq.rise(&irqCallback); 00033 * //irq.fall(&irqCallback); 00034 * sw.rise(&buttonCallback); 00035 * 00036 * 00037 * if(CO2.initIntegrated() == CDM7160_I2C_ADDR){ 00038 * printf("Initialization Success\r\n\n"); 00039 * } 00040 * if(CO2.setAlarmLimits(1500,1450) == CDM7160_SUCCESS){ 00041 * printf("Alarm Limits has been set!\r\n"); 00042 * } 00043 * 00044 * while (true) { 00045 * if(button){ 00046 * if(CO2.getAlarmLimits() == CDM7160_SUCCESS){ 00047 * printf("High Alert Limit = %d Low Alert Limit = %d\r\n", CO2.cdm7160.highAlertLimit, CO2.cdm7160.lowAlertLimit); 00048 * } 00049 * if(CO2.getAirPressureAndAltitude() == CDM7160_SUCCESS){ 00050 * printf("Atmosferic Pressure = %d [hPa] Altitude = %d [m]\r\n", CO2.cdm7160.atmosferic_pressure, CO2.cdm7160.altitude); 00051 * } 00052 * button = false; 00053 * } 00054 * 00055 * if(busy == 0){ 00056 * if(i==0){ 00057 * wait(2); 00058 * isIrq = false; 00059 * }else{ 00060 * printf("CO2 = %d [ppm]\r\n", CO2.getCO2()); 00061 * wait(2); 00062 * if(isIrq){ 00063 * printf("Interrupt !!!\r\n"); 00064 * isIrq = false; 00065 * } 00066 * } 00067 * i++; 00068 * } 00069 * } 00070 * } 00071 * 00072 */ 00073 00074 #include "mbed.h" 00075 00076 #ifndef CDM7160_H 00077 #define CDM7160_H 00078 00079 #include "mbed.h" 00080 00081 /** 00082 * Sensor I2C address 00083 */ 00084 //#define CDM7160_I2C_ADDR 0x68 // CAD0 pin to low logic 00085 #define CDM7160_I2C_ADDR 0x69 // CAD0 pin to high logic (left floating) 00086 00087 #define DIR_BIT_WRITE 0x00 00088 #define DIR_BIT_READ 0x01 00089 00090 /** 00091 * Sensor name 00092 */ 00093 #define CDM7160_NAME "CDM7160" 00094 #define CDM7160_NAME_LEN 8 00095 00096 /** 00097 * Measurements number 00098 */ 00099 #define CDM7160_MEASURE_NBR 1 00100 00101 #define CDM7160_CRC_POLYNOMIAL 0x31 00102 #define CDM7160_CRC8_INIT 0xFF 00103 00104 00105 /** 00106 * Conditions on I2C bus. 00107 */ 00108 #define CDM7160_SUCCESS 0 00109 #define CDM7160_ERROR -1 00110 00111 /** Macros - Build 2-Byte data 00112 */ 00113 #define CDM7160_2B_BUILD(hiByte, loByte) ((uint16_t)(((loByte) & 0x00FF) | (((hiByte) & 0x00FF) << 8))) 00114 00115 /** Macros - Split 2-Byte data 00116 */ 00117 #define CDM7160_HI_BYTE(reg) (((reg) >> 8) & 0xFF) 00118 #define CDM7160_LO_BYTE(reg) ((reg) & 0xFF) 00119 00120 /** 00121 * @class CDM7160 00122 * @brief Implementation of CDM7160 sensor CO2 library 00123 */ 00124 class CDM7160{ 00125 00126 public: 00127 00128 /** eReg read/write commands 00129 */ 00130 enum eReg { 00131 RST = 0x00, // software reset 00132 CTL = 0x01, // operating mode 00133 ST1 = 0x02, // status register 00134 DAL = 0x03, // LSB CO2 data 00135 DAH = 0x04, // MSB CO2 data 00136 HPA = 0x09, // atmosferic pressure 00137 HIT = 0x0A, // altitude 00138 ALHI = 0x0C, // upper limit treshold for alarm 00139 ALLO = 0x0D, // lower limit treshold for alarm 00140 FUNC = 0x0F, // PWM output 00141 ERR = 0x10 // Self-test 00142 }; 00143 00144 /** Reg: CTL / Addr: 0x01 / Permission: RW / Bits:2-0 00145 */ 00146 enum eControlMode { 00147 PWR_DOWN = 0x00, 00148 CONTIN = 0x06 00149 }; 00150 00151 /** Reg: FUNC / Addr: 0x0F / Permission: RW / Bits: 3, 2, 0 00152 */ 00153 enum eFunctionSetting { 00154 PWME_ON = 1 << 0, 00155 PWME_OFF = 0 << 0, 00156 HPAE_ON = 1 << 2, 00157 HPAE_OFF = 0 << 2, 00158 PWMR_LOW = 1 << 3, 00159 PWMR_HIGH = 0 << 3 00160 }; 00161 00162 /** cdm7160 stuct prototype 00163 */ 00164 struct cdm7160_t { 00165 uint8_t addr; 00166 int co2; 00167 uint8_t control; 00168 uint8_t status; 00169 uint16_t highAlertLimit; 00170 uint16_t lowAlertLimit; 00171 uint16_t atmosferic_pressure; 00172 uint16_t altitude; 00173 uint8_t settings; 00174 uint8_t selfDiag; 00175 }; 00176 cdm7160_t cdm7160; 00177 00178 /** Create an CDM7160 instance 00179 * which is connected to specified I2C pins with specified address 00180 * 00181 * @param sda I2C-bus SDA pin 00182 * @param scl I2C-bus SCL pin 00183 * @param addr (option) I2C-bus address (default: 0x4A) 00184 */ 00185 CDM7160(PinName sda, PinName scl, uint8_t addr = CDM7160_I2C_ADDR); 00186 00187 /** Create a CDM7160 instance 00188 * which is connected to specified I2C pins with specified address 00189 * 00190 * @param i2c_obj I2C object (instance) 00191 * @param addr (option) I2C-bus address (default: 0x4A) 00192 */ 00193 /** Constructor of CDM7160 00194 */ 00195 CDM7160(I2C &i2c_obj, uint8_t addr = CDM7160_I2C_ADDR); 00196 00197 /** Destructor of CDM7160 00198 */ 00199 ~CDM7160(); 00200 00201 00202 /** Initialize the CDM7160 00203 * Calling function control() and set sensor in continuous mode 00204 * @return CDM7160 address if successful. CDM7160_ERROR if fail 00205 */ 00206 int8_t init(); 00207 00208 /** Integrated Initialize the CDM7160 sensor 00209 * By default: set Atmospheric pressure and altitude, set sensor in countinous mode, 00210 * enable atm. pressure and altitude correction in measurement and 00211 * disable PWM output PIN 00212 * @return CDM7160 address if successful. CDM7160_ERROR if fail 00213 */ 00214 int8_t initIntegrated( uint16_t hpa = 1005, uint16_t height = 130, 00215 eControlMode mode = CONTIN, 00216 eFunctionSetting atm_alt_correct= HPAE_ON, 00217 eFunctionSetting pwm_conce = PWMR_LOW, 00218 eFunctionSetting pwm_pin = PWME_OFF); 00219 00220 /** Measure CO2 in ppm 00221 * Measurement is stored in struct variable: int co2 00222 * @return co2 if sucessful. CDM7160_ERROR if fail 00223 */ 00224 int getCO2(); 00225 00226 00227 /** Periodic Measurement mode of the CDM7160 00228 * CDM7160 goes into idle state 00229 * @param mode - operation mode (Power down or continuous) 00230 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00231 */ 00232 int8_t control(eControlMode mode = CONTIN); 00233 00234 /** Soft reset command 00235 * soft reset for CDM7160 sensor 00236 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00237 */ 00238 int8_t softReset(); 00239 00240 /** Read Status Register 00241 * status information of busy, alarm, connection on CAD0 and MSEL 00242 * Data stored in variable: uint8_t status 00243 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00244 */ 00245 int8_t getStatus(); 00246 00247 /** Set Alert High and Low Limit 00248 * @param high_limit - high limit value 00249 * @param low_limit - low limit value 00250 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00251 */ 00252 int8_t setAlarmLimits(uint16_t high_limit = 1000, uint16_t low_limit = 400); 00253 00254 /** Read Alert High and Low Limit 00255 * Data stored in variable: uint16_t highAlertLimit, uint16_t lowAlertLimit 00256 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00257 */ 00258 int8_t getAlarmLimits(); 00259 00260 /** Set Atmospheric Pressure and Altitude - to correct calculation CO2 00261 * @param hpa - atmospheric pressure value in [hPa] 00262 * @param height - altitude value in [m] 00263 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00264 */ 00265 int8_t setAirPressureAndAltitude(uint16_t hpa = 1005, uint16_t height = 130); 00266 00267 /** Read Atmospheric Pressure and Altitude - correction value calculation CO2 00268 * Data stored in variable: uint16_t atmosferic_pressure, uint16_t altitude 00269 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00270 */ 00271 int8_t getAirPressureAndAltitude(); 00272 00273 /** Read Setting Register 00274 * settings information of PWM pin enable/disable, concentration range and 00275 * atmospheric pressure and altitude correction enable/disable 00276 * Data stored in variable: uint8_t settings 00277 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00278 */ 00279 int8_t getSettings(); 00280 00281 /** Set Atmospheric Pressure and Altitude - to correct calculation CO2 00282 * @param atm_alt_correct - atm. pressure and altitude correction Enable/Disable 00283 * @param pwm_conce - PWM output contrntration High/Low 00284 * @param pwm_pin - PWM pin enable/disable 00285 * @return CDM7160_SUCCESS if successful. CDM7160_ERROR if fail 00286 */ 00287 int8_t setSettings(eFunctionSetting atm_alt_correct= HPAE_OFF, 00288 eFunctionSetting pwm_conce = PWMR_LOW, 00289 eFunctionSetting pwm_pin = PWME_OFF); 00290 00291 /** Self-test - diagnosis errors 00292 * Data stored in variable: uint8_t selfDiag 00293 * @return CDM7160 address if successful. CDM7160_ERROR if fail 00294 */ 00295 int8_t selfDiagnosis(); 00296 00297 private: 00298 00299 /** 00300 * Pointers to classes. 00301 */ 00302 I2C *pI2C; 00303 00304 /** 00305 * References to classes. 00306 */ 00307 I2C &_I2C; 00308 00309 /* 00310 * ----- PRIVATE I2C FUNCTIONS ----- 00311 */ 00312 00313 /** I2C read function. 00314 * @param data[] Data array to read from I2C 00315 * @param len Data length to read 00316 * @return Read status - 0 on success, non-0 on failure. 00317 */ 00318 int8_t read(char data[], uint8_t len); 00319 00320 /** 00321 * Write I2C register. 00322 * @param reg Register (1-Byte) to write 00323 * @returns Write status - 0 on success, non-0 on failure. 00324 */ 00325 int8_t write(uint8_t reg); 00326 00327 /** I2C write function. 00328 * @param reg Register (1-Byte) to Write 00329 * @param data[] Data array to write 00330 * @param len Data length to write 00331 * @return Write status - 0 on success, non-0 on failure. 00332 */ 00333 int8_t write(uint8_t reg, char data[], uint8_t len); 00334 00335 00336 }; 00337 #endif // CDM7160_H
Generated on Fri Jul 15 2022 13:16:54 by
