summaryrefslogtreecommitdiff
path: root/CCS811.h
diff options
context:
space:
mode:
Diffstat (limited to 'CCS811.h')
-rw-r--r--CCS811.h224
1 files changed, 224 insertions, 0 deletions
diff --git a/CCS811.h b/CCS811.h
new file mode 100644
index 0000000..2640535
--- /dev/null
+++ b/CCS811.h
@@ -0,0 +1,224 @@
1#ifndef _CCS811_H
2#define _CCS811_H
3
4#if ARDUINO >= 100
5#include "Arduino.h"
6#else
7#include "WProgram.h"
8#endif
9#include <Wire.h>
10
11
12/*I2C ADDRESS*/
13#define CCS811_I2C_ADDRESS1 0x5A
14#define CCS811_I2C_ADDRESS2 0x5B
15
16#define CCS811_REG_STATUS 0x00
17#define CCS811_REG_MEAS_MODE 0x01
18#define CCS811_REG_ALG_RESULT_DATA 0x02
19#define CCS811_REG_RAW_DATA 0x03
20#define CCS811_REG_ENV_DATA 0x05
21#define CCS811_REG_NTC 0x06
22#define CCS811_REG_THRESHOLDS 0x10
23#define CCS811_REG_BASELINE 0x11
24#define CCS811_REG_HW_ID 0x20
25#define CCS811_REG_HW_VERSION 0x21
26#define CCS811_REG_FW_BOOT_VERSION 0x23
27#define CCS811_REG_FW_APP_VERSION 0x24
28#define CCS811_REG_INTERNAL_STATE 0xA0
29#define CCS811_REG_ERROR_ID 0xE0
30#define CCS811_REG_SW_RESET 0xFF
31
32#define CCS811_BOOTLOADER_APP_ERASE 0xF1
33#define CCS811_BOOTLOADER_APP_DATA 0xF2
34#define CCS811_BOOTLOADER_APP_VERIFY 0xF3
35#define CCS811_BOOTLOADER_APP_START 0xF4
36
37#define CCS811_HW_ID 0x81
38//Open the macro to see the detailed program execution process.
39//#define ENABLE_DBG
40
41#ifdef ENABLE_DBG
42#define DBG(...) {Serial.print("[");Serial.print(__FUNCTION__); Serial.print("(): "); Serial.print(__LINE__); Serial.print(" ] "); Serial.println(__VA_ARGS__);}
43#else
44#define DBG(...)
45#endif
46
47class CCS811
48{
49public:
50 #define ERR_OK 0 //OK
51 #define ERR_DATA_BUS -1 //error in data bus
52 #define ERR_IC_VERSION -2 //chip version mismatch
53
54 uint8_t _deviceAddr;
55 typedef enum{
56 eMode0, //Idle (Measurements are disabled in this mode)
57 eMode1, //Constant power mode, IAQ measurement every second
58 eMode2, //Pulse heating mode IAQ measurement every 10 seconds
59 eMode3, //Low power pulse heating mode IAQ measurement every 60 seconds
60 eMode4 //Constant power mode, sensor measurement every 250ms 1xx: Reserved modes (For future use)
61 }eDRIVE_MODE_t;
62
63 typedef enum{
64 eClosed, //Idle (Measurements are disabled in this mode)
65 eCycle_1s, //Constant power mode, IAQ measurement every second
66 eCycle_10s, //Pulse heating mode IAQ measurement every 10 seconds
67 eCycle_60s, //Low power pulse heating mode IAQ measurement every 60 seconds
68 eCycle_250ms //Constant power mode, sensor measurement every 250ms 1xx: Reserved modes (For future use)
69 }eCycle_t;
70 /**
71 * @brief Constructor
72 * @param Input in Wire address
73 */
74 CCS811(TwoWire *pWire = &Wire, uint8_t deviceAddr = 0x5A){_pWire = pWire; _deviceAddr = deviceAddr;};
75
76 /**
77 * @brief Constructor
78 * @return Return 0 if initialization succeeds, otherwise return non-zero.
79 */
80 int begin();
81 /**
82 * @brief Judge if there is data to read
83 * @return Return 1 if there is, otherwise return 0.
84 */
85 bool checkDataReady();
86 /**
87 * @brief Reset sensor, clear all configured data.
88 */
89 void softReset(),
90 /**
91 * @brief Set environment parameter
92 * @param temperature Set temperature value, unit: centigrade, range (-40~85℃)
93 * @param humidity Set humidity value, unit: RH, range (0~100)
94 */
95 setInTempHum(float temperature, float humidity),
96 /**
97 * @brief Measurement parameter configuration
98 * @param thresh:0 for Interrupt mode operates normally; 1 for interrupt mode only asserts the nINT signal (driven low) if the new
99 * @param interrupt:0 for Interrupt generation is disabled; 1 for the nINT signal is asserted (driven low) when a new sample is ready in
100 * @param mode:in typedef enum eDRIVE_MODE_t
101 */
102 setMeasurementMode(uint8_t thresh, uint8_t interrupt, eDRIVE_MODE_t mode),
103 /**
104 * @brief Measurement parameter configuration
105 * @param mode:in typedef enum eDRIVE_MODE_t
106 */
107 setMeasCycle(eCycle_t cycle),
108 /**
109 * @brief Set interrupt thresholds
110 * @param lowToMed: interrupt triggered value in range low to middle
111 * @param medToHigh: interrupt triggered value in range middle to high
112 */
113 setThresholds(uint16_t lowToMed, uint16_t medToHigh);
114 /**
115 * @brief Get current configured parameter
116 * @return configuration code, needs to be converted into binary code to analyze
117 * The 2nd: Interrupt mode (if enabled) operates normally,1: Interrupt mode (if enabled) only asserts the nINT signal (driven low) if the new
118 * The 3rd: Interrupt generation is disabled,1: The nINT signal is asserted (driven low) when a new sample is ready in
119 * The 4th: 6th: in typedef enum eDRIVE_MODE_t
120 */
121 uint8_t getMeasurementMode();
122
123 /**
124 * @brief Get the current carbon dioxide concentration
125 * @return current carbon dioxide concentration, unit:ppm
126 */
127 uint16_t getCO2PPM(),
128 /**
129 * @brief Get current TVOC concentration
130 * @return Return current TVOC concentration, unit: ppb
131 */
132 getTVOCPPB();
133 uint16_t readBaseLine();
134 void writeBaseLine(uint16_t baseLine);
135
136protected:
137
138 typedef struct{
139 /*
140 * The CCS811 received an I²C write request addressed to this station but with invalid register address ID
141 */
142 uint8_t sWRITE_REG_INVALID: 1;
143 /*
144 * The CCS811 received an I²C read request to a mailbox ID that is invalid
145 */
146 uint8_t sREAD_REG_INVALID: 1;
147 /*
148 * The CCS811 received an I²C request to write an unsupported mode to MEAS_MODE
149 */
150 uint8_t sMEASMODE_INVALID: 1;
151 /*
152 * The sensor resistance measurement has reached or exceeded the maximum range
153 */
154 uint8_t sMAX_RESISTANCE: 1;
155 /*
156 * The The Heater current in the CCS811 is not in range
157 */
158 uint8_t sHEATER_FAULT: 1;
159 /*
160 * The Heater voltage is not being applied correctly
161 */
162 uint8_t sHEATER_SUPPLY: 1;
163 } __attribute__ ((packed))sError_id;
164
165 typedef struct{
166 /*
167 * ALG_RESULT_DATA crosses one of the thresholds set in the THRESHOLDS register
168 * by more than the hysteresis value (also in the THRESHOLDS register)
169 */
170 uint8_t sINT_THRESH: 1;
171 /*
172 * At the end of each measurement cycle (250ms, 1s, 10s, 60s) a flag is set in the
173 * STATUS register regardless of the setting of this bit.
174 */
175 uint8_t sINT_DATARDY: 1;
176 /*
177 * A new sample is placed in ALG_RESULT_DATA and RAW_DATA registers and the
178 * DATA_READY bit in the STATUS register is set at the defined measurement interval.
179 */
180 uint8_t sDRIVE_MODE: 3;
181 } __attribute__ ((packed))sMeas_mode;
182
183 typedef struct{
184 /*
185 * This bit is cleared by reading ERROR_ID
186 * It is not sufficient to read the ERROR field of ALG_RESULT_DATA and STATUS
187 */
188 uint8_t sERROR: 1;
189 /*
190 * ALG_RESULT_DATA is read on the I²C interface
191 */
192 uint8_t sDATA_READY: 1;
193 uint8_t sAPP_VALID: 1;
194 /*
195 * After issuing a VERIFY command the application software must wait 70ms before
196 * issuing any transactions to CCS811 over the I²C interface
197 */
198 uint8_t sAPP_VERIFY: 1;
199 /*
200 * After issuing the ERASE command the application software must wait 500ms
201 * before issuing any transactions to the CCS811 over the I2C interface.
202 */
203 uint8_t sAPP_ERASE: 1;
204 uint8_t sFW_MODE: 1;
205 } __attribute__ ((packed))sStatus;
206
207
208 void getData(void);
209
210 void writeConfig();
211
212 virtual void writeReg(uint8_t reg, const void* pBuf, size_t size);
213 virtual uint8_t readReg(uint8_t reg, const void* pBuf, size_t size);
214
215
216
217private:
218 TwoWire *_pWire;
219
220 uint16_t eCO2;
221 uint16_t eTVOC;
222};
223
224#endif