Actually works, and with mbed os 5 latest

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

Fork of CCS811 by MtM+

Files at this revision

API Documentation at this revision

Comitter:
johnathanlyu
Date:
Tue Jun 27 05:17:02 2017 +0000
Child:
1:4acc5b63a984
Commit message:
initial MtConnect04s with MtSense02 version

Changed in this revision

CCS811.cpp Show annotated file Show diff for this revision Revisions of this file
CCS811.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CCS811.cpp	Tue Jun 27 05:17:02 2017 +0000
@@ -0,0 +1,108 @@
+#include "CCS811.h"
+
+CCS811::CCS811(I2C& i2c) : m_i2c(i2c), pc(p5, p4) {
+    
+}
+
+/**
+ ** Initial CCS811 need write MODE register and should Write APP START register to begin measurement.
+ **/
+void CCS811::init() {
+    char send[2];
+    
+    if (!checkHW()) {
+        return;
+    }else {
+        pc.printf("CCS811 is confirm!\r\n");    
+    }
+    
+    send[0] = CCS811_REG_MEAS_MODE;
+    send[1] = CCS811_MEASUREMENT_MODE1;
+    
+    m_i2c.write(CCS811_I2C_ADDR, send, 2);
+    
+    send[0] = CCS811_REG_APP_START;
+    send[1] = 0x00;
+    
+    m_i2c.write(CCS811_I2C_ADDR, send, 2);
+    
+}
+
+int CCS811::setMeasureMode(char mode) {
+    
+    char send[2];
+    
+    send[0] = CCS811_MEASUREMENT_MODE1;
+    send[1] = mode;
+    
+    m_i2c.write(CCS811_I2C_ADDR, send, 2);
+    
+    send[0] = CCS811_REG_APP_START;
+    send[1] = 0x00;
+    
+    m_i2c.write(CCS811_I2C_ADDR, send, 2);
+    
+    return 0;
+}
+
+/**
+ ** Here is that you can read CCS811 with co2 ppm and tvoc bbm is unsigned value
+ **/
+int CCS811::readData(uint16_t *ECO2, uint16_t *TVOC) {
+    
+    char recv[8];
+    
+    
+    
+    recv[0] = CCS811_REG_ALG_RESULT_DATA;
+    m_i2c.write(CCS811_I2C_ADDR, recv, 1);
+    m_i2c.read( CCS811_I2C_ADDR, recv, 8);
+
+//    pc.printf("%X %X\r\n", recv[0], recv[1]);
+//    pc.printf("%X %X\r\n", recv[2], recv[3]);
+//    pc.printf("%X %X\r\n", recv[4], recv[5]);
+//    pc.printf("%X %X\r\n", recv[6], recv[7]);
+    
+    *ECO2 = (uint16_t) (recv[0] <<8) + recv[1];
+    *TVOC = (uint16_t) (recv[2] <<8) + recv[3];
+    
+    return 0;
+    
+}
+
+/**
+ ** Here for check is CCS811 hardware from i2c bus
+ **/
+bool CCS811::checkHW() {
+
+    char read[1];
+    char hid[1];
+    
+    read[0] = CCS811_REG_HW_ID;
+    
+    m_i2c.write(CCS811_I2C_ADDR, read, 1, false);
+    m_i2c.read(CCS811_I2C_ADDR, hid, 1, false);
+    
+//    pc.printf("%X\r\n", hid[0]);
+    
+    if (hid[0] == 0x81) {
+        return true;
+    } else {
+        return false;
+    }
+    
+}
+
+/**
+ **  Here is provide you soft reset CCS811 module 
+ **/
+bool CCS811::softRest() {
+     
+    char rstCMD[5] = {CCS811_REG_SW_RESET, 0x11,0xE5,0x72,0x8A};
+
+    m_i2c.write(CCS811_I2C_ADDR, rstCMD, 5);
+    
+    return false;
+      
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CCS811.h	Tue Jun 27 05:17:02 2017 +0000
@@ -0,0 +1,50 @@
+#ifndef CCS811_H
+#define CCS811_H
+
+//CCS811 register define
+#define CCS811_I2C_ADDR                 0xB4
+#define CCS811_REG_STATUS               0x00    //Status.
+#define CCS811_REG_MEAS_MODE            0x01    //Mesurement mode and conditions register.
+#define CCS811_REG_ALG_RESULT_DATA      0x02    //Algorithm result. 2 bytes co2 ppm next 2 bytes ppb VOC level.
+#define CCS811_REG_RAW_DATA             0x03    //Raw ADC data.
+#define CCS811_REG_ENV_DATA             0x05    //Temperature and humidity data can be write to enavle compensation.
+#define CCS811_REG_NTC                  0x06    //Provides the voltage across the reference registor and the voltage across the NTC resistor.
+#define CCS811_REG_THRESHOLDS           0x10    //Thresholds for operation when interrupts are only generated when eCO2 ppm crosses a threshold.
+#define CCS811_REG_BASELINE             0x11    //The encoded current baseline value can be read.A previously saved encoded baseline can be written.
+#define CCS811_REG_HW_ID                0x20    //Hardware ID. The value is 0x81.
+#define CCS811_REG_HW_VERSION           0x21    //Hardware version. The value is 0x1X.
+#define CCS811_REG_BOOT_VERSION         0x23    //Firmware boot version. The First 2 bytes contain the firmware version number for the boot code.
+#define CCS811_REG_APP_VERSION          0x24    //Firmware application version. The first 2 bytes contain the firmware version number for the application code.
+#define CCS811_REG_ERROR_ID             0xE0    //Error ID. When the status register reports and error its source is lcated in this register.
+#define CCS811_REG_APP_START            0xF4    //Application start. Used to transition the CCS811 state from boot to application mode, a write with no data is required. Before performing a write to APP_START the Status register should be accessed to check if there is a valid application present.
+#define CCS811_REG_SW_RESET             0xFF    //If the correct 4 byres (0x11, 0xE5, 0x72, 0x8A)are written to this register in a single sequence the device will reset and return to BOOT bode.
+
+//mode setting
+#define CCS811_MEASUREMENT_MODE0        0x00    //Idle(Measurements are disabled in this mode).
+#define CCS811_MEASUREMENT_MODE1        0x10    //Constant power mode, IAQ measurement every second.
+#define CCS811_MEASUREMENT_MODE2        0x20    //Pulse heating mode IAQ measurement every 10 seconds.
+#define CCS811_MEASUREMENT_MODE3        0x30    //Low power pulse heating mode IAQ measurement every 60 seconds.
+#define CCS811_MEASUREMENT_MODE4        0x40    //Constant power mode, sensor measurement every 250ms. 1xx: Reserved modes (For future use).
+
+//Interrupt control 
+#define CCS811_INT_DATARDY              0x08    //At the end of each measurement cycle (250ms, 1s, 10s, 60s) a flag is set in the STATUS register regardless of the setting of this bit.
+#define CCS811_INT_THRESH               0x04    //0: Interrupt mode (if enabled) operates normally 1: Interrupt mode (if enabled) only asserts the nINT signal (driven low) if the new ALG_RESULT_DATA crosses one of the thresholds set in the THRESHOLDS register by more than the hysteresis value (also in the THRESHOLDS register).
+
+#include "mbed.h"
+
+class CCS811 {
+    public:
+        CCS811(I2C& i2c);
+        void init();
+        int setMeasureMode(char mode);
+        int readData(uint16_t *ECO2, uint16_t *TVOC);
+        bool checkHW();
+        bool softRest();
+    protected:
+        I2C m_i2c;
+        Serial pc;
+    private:
+};
+
+
+#endif
\ No newline at end of file