Andrea Corrado / CCS811

Dependents:   CCS811-TEST Mbed-Connect-Cloud-Demo Mbed-Connect-Cloud-Demo HTTP-all-sensors ... more

Fork of CCS811 by MtM+

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CCS811.cpp Source File

CCS811.cpp

00001 #include "CCS811.h"
00002 
00003 CCS811::CCS811(PinName sda, PinName scl) : _i2c(sda, scl) {
00004     
00005 }
00006 
00007 /**
00008  ** Initial CCS811 need write MODE register and should Write APP START register to begin measurement.
00009  **/
00010 void CCS811::init() {
00011     wait_ms(50);
00012     
00013     char send[2];
00014     char read[8];
00015     char hwv[8];
00016     char hwd[8];
00017       
00018     read[0] = CCS811_REG_STATUS; //0x00
00019     
00020     _i2c.write(CCS811_I2C_ADDR, read, 1);
00021     _i2c.read(CCS811_I2C_ADDR, hwv, 1);
00022     
00023     wait_us(50);
00024     
00025     send[0] = CCS811_REG_APP_START; //0xF4
00026     
00027     wait_us(50);
00028     
00029     _i2c.write(CCS811_I2C_ADDR, send, 1);
00030     
00031     wait_us(50);
00032 
00033     read[0] = CCS811_REG_STATUS; //0x00
00034     
00035     wait_us(50);
00036 
00037     _i2c.write(CCS811_I2C_ADDR, read, 1);
00038     _i2c.read(CCS811_I2C_ADDR, hwd, 1);
00039     
00040     wait_us(50);
00041 
00042     
00043     send[0] = CCS811_REG_MEAS_MODE; //0x01
00044     send[1] = CCS811_MEASUREMENT_MODE1; //0x10
00045    
00046     _i2c.write(CCS811_I2C_ADDR, send, 2);
00047     wait_us(50);   
00048     
00049     wait_us(50);
00050 
00051     
00052     read[0] = CCS811_REG_STATUS; //0x00
00053     
00054     wait_us(50);
00055 
00056     
00057     _i2c.write(CCS811_I2C_ADDR, read, 1);
00058     _i2c.read(CCS811_I2C_ADDR, hwd, 1);
00059     
00060     wait_us(50);   
00061 }
00062 
00063 int CCS811::setMeasureMode(char mode) {
00064     
00065     char send[2];
00066     
00067     send[0] = CCS811_REG_MEAS_MODE;
00068     send[1] = mode;
00069     
00070     _i2c.write(CCS811_I2C_ADDR, send, 2);
00071     
00072 //    send[0] = CCS811_REG_APP_START;
00073 //    send[1] = 0x00;
00074     
00075 //    _i2c.write(CCS811_I2C_ADDR, send, 2);
00076     
00077     return 0;
00078 }
00079 
00080 bool CCS811::readstatus() {
00081 
00082     char read[8];
00083     char hwd[8];
00084        
00085     read[0] = CCS811_REG_STATUS; //0x00
00086        
00087     _i2c.write(CCS811_I2C_ADDR, read, 1);
00088     _i2c.read(CCS811_I2C_ADDR, hwd, 1);
00089        
00090     printf("STATUS 0x%X\r\n", hwd[0]);
00091     
00092     return 0;
00093 }
00094 
00095 bool CCS811::readmeas() {
00096 
00097     char read[8];
00098     char hwd[8];    
00099     
00100     read[0] = CCS811_REG_MEAS_MODE; //0x01
00101    
00102     _i2c.write(CCS811_I2C_ADDR, read, 1);
00103     _i2c.read(CCS811_I2C_ADDR, hwd, 1);
00104        
00105     printf("meas 0x%X\r\n", hwd[0]);
00106     
00107     return 0;
00108 }
00109 
00110 bool CCS811::readerror() {
00111 
00112     char read[8];
00113     char hwv[8];
00114     
00115     read[0] = CCS811_REG_ERROR_ID; //0xE0
00116 
00117     _i2c.write(CCS811_I2C_ADDR, read, 1);
00118     _i2c.read(CCS811_I2C_ADDR, hwv, 1);
00119 
00120     printf("error 0x%X \r\n", hwv[0]);
00121     
00122     return 0;
00123 }
00124 
00125 /**
00126  ** Here is that you can read CCS811 with co2 ppm and tvoc bbm is unsigned value
00127  **/
00128 int CCS811::readData(uint16_t *ECO2, uint16_t *TVOC) {
00129 
00130     char recv[8];
00131     char send[1];
00132 
00133     send[0] = CCS811_REG_ALG_RESULT_DATA;
00134     _i2c.write(CCS811_I2C_ADDR, send, 1, true);
00135     _i2c.read(CCS811_I2C_ADDR, recv, 8, false);
00136     wait_ms(1);
00137 /*
00138     pc.printf("%X %X\r\n", recv[0], recv[1]);
00139     pc.printf("%X %X\r\n", recv[2], recv[3]);
00140     pc.printf("%X %X\r\n", recv[4], recv[5]);
00141     pc.printf("%X %X\r\n", recv[6], recv[7]);
00142 */    
00143     *ECO2 = (uint16_t) (recv[0] <<8) + recv[1];
00144     *TVOC = (uint16_t) (recv[2] <<8) + recv[3];
00145     
00146     return 0;
00147     
00148 }
00149 
00150 /**
00151  ** Here for check is CCS811 hardware from i2c bus
00152  **/
00153 bool CCS811::checkHW() {
00154 
00155     char read[1];
00156     char hid[1];
00157     
00158     read[0] = CCS811_REG_HW_ID;
00159     
00160     _i2c.write(CCS811_I2C_ADDR, read, 1, false);
00161     _i2c.read(CCS811_I2C_ADDR, hid, 1, false);
00162     
00163 //    pc.printf("%X\r\n", hid[0]);
00164     
00165     if (hid[0] == 0x81) {
00166         return true;
00167     } else {
00168         return false;
00169     }
00170     
00171 }
00172 
00173 /**
00174  **  Here is provide you soft reset CCS811 module 
00175  **/
00176 bool CCS811::softRest() {
00177      
00178     char rstCMD[5] = {CCS811_REG_SW_RESET, 0x11,0xE5,0x72,0x8A};
00179 
00180     _i2c.write(CCS811_I2C_ADDR, rstCMD, 5);
00181     
00182     return false;
00183       
00184 }
00185