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.
CCS811.cpp@1:d0abe05bc21e, 2021-09-06 (annotated)
- Committer:
- louatayehh
- Date:
- Mon Sep 06 11:32:22 2021 +0000
- Revision:
- 1:d0abe05bc21e
- Parent:
- 0:2c1dce3543ae
Capteur 2 : CSS811;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| louatayehh | 1:d0abe05bc21e | 1 | //*****************Capteur CCS811 - qualité de l'air ***************************// |
| louatayehh | 1:d0abe05bc21e | 2 | |
| louatayehh | 0:2c1dce3543ae | 3 | #include "CCS811.h" |
| louatayehh | 0:2c1dce3543ae | 4 | |
| louatayehh | 1:d0abe05bc21e | 5 | CCS811::CCS811(PinName sda, PinName scl) : _i2c(sda, scl) {} // Constructeur |
| louatayehh | 0:2c1dce3543ae | 6 | |
| louatayehh | 0:2c1dce3543ae | 7 | /** |
| louatayehh | 0:2c1dce3543ae | 8 | ** Initial CCS811 need write MODE register and should Write APP START register to begin measurement. |
| louatayehh | 0:2c1dce3543ae | 9 | **/ |
| louatayehh | 0:2c1dce3543ae | 10 | |
| louatayehh | 0:2c1dce3543ae | 11 | void CCS811::init() |
| louatayehh | 0:2c1dce3543ae | 12 | { |
| louatayehh | 0:2c1dce3543ae | 13 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 14 | |
| louatayehh | 0:2c1dce3543ae | 15 | char send[2]; |
| louatayehh | 0:2c1dce3543ae | 16 | char read[8]; |
| louatayehh | 0:2c1dce3543ae | 17 | char hwv[8]; |
| louatayehh | 0:2c1dce3543ae | 18 | char hwd[8]; |
| louatayehh | 0:2c1dce3543ae | 19 | |
| louatayehh | 0:2c1dce3543ae | 20 | read[0] = CCS811_REG_STATUS; //0x00 |
| louatayehh | 0:2c1dce3543ae | 21 | |
| louatayehh | 0:2c1dce3543ae | 22 | _i2c.write(CCS811_I2C_ADDR, read, 1); |
| louatayehh | 0:2c1dce3543ae | 23 | _i2c.read(CCS811_I2C_ADDR, hwv, 1); |
| louatayehh | 0:2c1dce3543ae | 24 | |
| louatayehh | 0:2c1dce3543ae | 25 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 26 | |
| louatayehh | 0:2c1dce3543ae | 27 | send[0] = CCS811_REG_APP_START; //0xF4 |
| louatayehh | 0:2c1dce3543ae | 28 | |
| louatayehh | 0:2c1dce3543ae | 29 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 30 | |
| louatayehh | 0:2c1dce3543ae | 31 | _i2c.write(CCS811_I2C_ADDR, send, 1); |
| louatayehh | 0:2c1dce3543ae | 32 | |
| louatayehh | 0:2c1dce3543ae | 33 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 34 | |
| louatayehh | 0:2c1dce3543ae | 35 | read[0] = CCS811_REG_STATUS; //0x00 |
| louatayehh | 0:2c1dce3543ae | 36 | |
| louatayehh | 0:2c1dce3543ae | 37 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 38 | |
| louatayehh | 0:2c1dce3543ae | 39 | _i2c.write(CCS811_I2C_ADDR, read, 1); |
| louatayehh | 0:2c1dce3543ae | 40 | _i2c.read(CCS811_I2C_ADDR, hwd, 1); |
| louatayehh | 0:2c1dce3543ae | 41 | |
| louatayehh | 0:2c1dce3543ae | 42 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 43 | |
| louatayehh | 0:2c1dce3543ae | 44 | |
| louatayehh | 0:2c1dce3543ae | 45 | send[0] = CCS811_REG_MEAS_MODE; //0x01 |
| louatayehh | 0:2c1dce3543ae | 46 | send[1] = CCS811_MEASUREMENT_MODE1; //0x10 |
| louatayehh | 0:2c1dce3543ae | 47 | |
| louatayehh | 0:2c1dce3543ae | 48 | _i2c.write(CCS811_I2C_ADDR, send, 2); |
| louatayehh | 0:2c1dce3543ae | 49 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 50 | |
| louatayehh | 0:2c1dce3543ae | 51 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 52 | |
| louatayehh | 0:2c1dce3543ae | 53 | |
| louatayehh | 0:2c1dce3543ae | 54 | read[0] = CCS811_REG_STATUS; //0x00 |
| louatayehh | 0:2c1dce3543ae | 55 | |
| louatayehh | 0:2c1dce3543ae | 56 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 57 | |
| louatayehh | 0:2c1dce3543ae | 58 | |
| louatayehh | 0:2c1dce3543ae | 59 | _i2c.write(CCS811_I2C_ADDR, read, 1); |
| louatayehh | 0:2c1dce3543ae | 60 | _i2c.read(CCS811_I2C_ADDR, hwd, 1); |
| louatayehh | 0:2c1dce3543ae | 61 | |
| louatayehh | 0:2c1dce3543ae | 62 | wait_us(50); |
| louatayehh | 0:2c1dce3543ae | 63 | } |
| louatayehh | 0:2c1dce3543ae | 64 | |
| louatayehh | 0:2c1dce3543ae | 65 | int CCS811::setMeasureMode(char mode) |
| louatayehh | 0:2c1dce3543ae | 66 | { |
| louatayehh | 0:2c1dce3543ae | 67 | |
| louatayehh | 0:2c1dce3543ae | 68 | char send[2]; |
| louatayehh | 0:2c1dce3543ae | 69 | |
| louatayehh | 0:2c1dce3543ae | 70 | send[0] = CCS811_REG_MEAS_MODE; |
| louatayehh | 0:2c1dce3543ae | 71 | send[1] = mode; |
| louatayehh | 0:2c1dce3543ae | 72 | |
| louatayehh | 0:2c1dce3543ae | 73 | _i2c.write(CCS811_I2C_ADDR, send, 2); |
| louatayehh | 0:2c1dce3543ae | 74 | |
| louatayehh | 0:2c1dce3543ae | 75 | // send[0] = CCS811_REG_APP_START; |
| louatayehh | 0:2c1dce3543ae | 76 | // send[1] = 0x00; |
| louatayehh | 0:2c1dce3543ae | 77 | |
| louatayehh | 0:2c1dce3543ae | 78 | // _i2c.write(CCS811_I2C_ADDR, send, 2); |
| louatayehh | 0:2c1dce3543ae | 79 | |
| louatayehh | 0:2c1dce3543ae | 80 | return 0; |
| louatayehh | 0:2c1dce3543ae | 81 | } |
| louatayehh | 0:2c1dce3543ae | 82 | |
| louatayehh | 0:2c1dce3543ae | 83 | bool CCS811::readstatus() |
| louatayehh | 0:2c1dce3543ae | 84 | { |
| louatayehh | 0:2c1dce3543ae | 85 | |
| louatayehh | 0:2c1dce3543ae | 86 | char read[8]; |
| louatayehh | 0:2c1dce3543ae | 87 | char hwd[8]; |
| louatayehh | 0:2c1dce3543ae | 88 | |
| louatayehh | 0:2c1dce3543ae | 89 | read[0] = CCS811_REG_STATUS; //0x00 |
| louatayehh | 0:2c1dce3543ae | 90 | |
| louatayehh | 0:2c1dce3543ae | 91 | _i2c.write(CCS811_I2C_ADDR, read, 1); |
| louatayehh | 0:2c1dce3543ae | 92 | _i2c.read(CCS811_I2C_ADDR, hwd, 1); |
| louatayehh | 0:2c1dce3543ae | 93 | |
| louatayehh | 0:2c1dce3543ae | 94 | printf("STATUS 0x%X\r\n", hwd[0]); |
| louatayehh | 0:2c1dce3543ae | 95 | |
| louatayehh | 0:2c1dce3543ae | 96 | return 0; |
| louatayehh | 0:2c1dce3543ae | 97 | } |
| louatayehh | 0:2c1dce3543ae | 98 | |
| louatayehh | 0:2c1dce3543ae | 99 | bool CCS811::readmeas() |
| louatayehh | 0:2c1dce3543ae | 100 | { |
| louatayehh | 0:2c1dce3543ae | 101 | |
| louatayehh | 0:2c1dce3543ae | 102 | char read[8]; |
| louatayehh | 0:2c1dce3543ae | 103 | char hwd[8]; |
| louatayehh | 0:2c1dce3543ae | 104 | |
| louatayehh | 0:2c1dce3543ae | 105 | read[0] = CCS811_REG_MEAS_MODE; //0x01 |
| louatayehh | 0:2c1dce3543ae | 106 | |
| louatayehh | 0:2c1dce3543ae | 107 | _i2c.write(CCS811_I2C_ADDR, read, 1); |
| louatayehh | 0:2c1dce3543ae | 108 | _i2c.read(CCS811_I2C_ADDR, hwd, 1); |
| louatayehh | 0:2c1dce3543ae | 109 | |
| louatayehh | 0:2c1dce3543ae | 110 | printf("meas 0x%X\r\n", hwd[0]); |
| louatayehh | 0:2c1dce3543ae | 111 | |
| louatayehh | 0:2c1dce3543ae | 112 | return 0; |
| louatayehh | 0:2c1dce3543ae | 113 | } |
| louatayehh | 0:2c1dce3543ae | 114 | |
| louatayehh | 0:2c1dce3543ae | 115 | bool CCS811::readerror() |
| louatayehh | 0:2c1dce3543ae | 116 | { |
| louatayehh | 0:2c1dce3543ae | 117 | |
| louatayehh | 0:2c1dce3543ae | 118 | char read[8]; |
| louatayehh | 0:2c1dce3543ae | 119 | char hwv[8]; |
| louatayehh | 0:2c1dce3543ae | 120 | |
| louatayehh | 0:2c1dce3543ae | 121 | read[0] = CCS811_REG_ERROR_ID; //0xE0 |
| louatayehh | 0:2c1dce3543ae | 122 | |
| louatayehh | 0:2c1dce3543ae | 123 | _i2c.write(CCS811_I2C_ADDR, read, 1); |
| louatayehh | 0:2c1dce3543ae | 124 | _i2c.read(CCS811_I2C_ADDR, hwv, 1); |
| louatayehh | 0:2c1dce3543ae | 125 | |
| louatayehh | 0:2c1dce3543ae | 126 | printf("error 0x%X \r\n", hwv[0]); |
| louatayehh | 0:2c1dce3543ae | 127 | |
| louatayehh | 0:2c1dce3543ae | 128 | return 0; |
| louatayehh | 0:2c1dce3543ae | 129 | } |
| louatayehh | 0:2c1dce3543ae | 130 | |
| louatayehh | 0:2c1dce3543ae | 131 | //Lecture de CCS811 : taux de CO2 en ppm et taux de tvos en bbm (unsigned) |
| louatayehh | 0:2c1dce3543ae | 132 | |
| louatayehh | 0:2c1dce3543ae | 133 | int CCS811::readData(uint16_t *ECO2, uint16_t *TVOC) |
| louatayehh | 0:2c1dce3543ae | 134 | { |
| louatayehh | 0:2c1dce3543ae | 135 | char recv[8]; |
| louatayehh | 0:2c1dce3543ae | 136 | char send[1]; |
| louatayehh | 0:2c1dce3543ae | 137 | |
| louatayehh | 0:2c1dce3543ae | 138 | send[0] = CCS811_REG_ALG_RESULT_DATA; |
| louatayehh | 0:2c1dce3543ae | 139 | _i2c.write(CCS811_I2C_ADDR, send, 1, true); |
| louatayehh | 0:2c1dce3543ae | 140 | _i2c.read(CCS811_I2C_ADDR, recv, 8, false); |
| louatayehh | 0:2c1dce3543ae | 141 | wait_us(1); |
| louatayehh | 0:2c1dce3543ae | 142 | |
| louatayehh | 0:2c1dce3543ae | 143 | // pc.printf("%X %X\r\n", recv[0], recv[1]); |
| louatayehh | 0:2c1dce3543ae | 144 | // pc.printf("%X %X\r\n", recv[2], recv[3]); |
| louatayehh | 0:2c1dce3543ae | 145 | // pc.printf("%X %X\r\n", recv[4], recv[5]); |
| louatayehh | 0:2c1dce3543ae | 146 | // pc.printf("%X %X\r\n", recv[6], recv[7]); |
| louatayehh | 0:2c1dce3543ae | 147 | |
| louatayehh | 0:2c1dce3543ae | 148 | *ECO2 = (uint16_t) (recv[0] <<8) + recv[1]; |
| louatayehh | 0:2c1dce3543ae | 149 | *TVOC = (uint16_t) (recv[2] <<8) + recv[3]; |
| louatayehh | 0:2c1dce3543ae | 150 | |
| louatayehh | 0:2c1dce3543ae | 151 | return 0; |
| louatayehh | 0:2c1dce3543ae | 152 | |
| louatayehh | 0:2c1dce3543ae | 153 | } |
| louatayehh | 0:2c1dce3543ae | 154 | |
| louatayehh | 0:2c1dce3543ae | 155 | |
| louatayehh | 0:2c1dce3543ae | 156 | //vérifier CCS811 du bus i2c |
| louatayehh | 0:2c1dce3543ae | 157 | |
| louatayehh | 0:2c1dce3543ae | 158 | bool CCS811::checkHW() |
| louatayehh | 0:2c1dce3543ae | 159 | { |
| louatayehh | 0:2c1dce3543ae | 160 | char read[1]; |
| louatayehh | 0:2c1dce3543ae | 161 | char hid[1]; |
| louatayehh | 0:2c1dce3543ae | 162 | |
| louatayehh | 0:2c1dce3543ae | 163 | read[0] = CCS811_REG_HW_ID; |
| louatayehh | 0:2c1dce3543ae | 164 | |
| louatayehh | 0:2c1dce3543ae | 165 | _i2c.write(CCS811_I2C_ADDR, read, 1, false); |
| louatayehh | 0:2c1dce3543ae | 166 | _i2c.read(CCS811_I2C_ADDR, hid, 1, false); |
| louatayehh | 0:2c1dce3543ae | 167 | |
| louatayehh | 0:2c1dce3543ae | 168 | // pc.printf("%X\r\n", hid[0]); |
| louatayehh | 0:2c1dce3543ae | 169 | |
| louatayehh | 0:2c1dce3543ae | 170 | if (hid[0] == 0x81) { |
| louatayehh | 0:2c1dce3543ae | 171 | return true; |
| louatayehh | 0:2c1dce3543ae | 172 | } else { |
| louatayehh | 0:2c1dce3543ae | 173 | return false; |
| louatayehh | 0:2c1dce3543ae | 174 | } |
| louatayehh | 0:2c1dce3543ae | 175 | |
| louatayehh | 0:2c1dce3543ae | 176 | } |
| louatayehh | 0:2c1dce3543ae | 177 | |
| louatayehh | 0:2c1dce3543ae | 178 | |
| louatayehh | 0:2c1dce3543ae | 179 | bool CCS811::softRest() |
| louatayehh | 0:2c1dce3543ae | 180 | { |
| louatayehh | 0:2c1dce3543ae | 181 | |
| louatayehh | 0:2c1dce3543ae | 182 | char rstCMD[5] = {CCS811_REG_SW_RESET, 0x11,0xE5,0x72,0x8A}; |
| louatayehh | 0:2c1dce3543ae | 183 | |
| louatayehh | 0:2c1dce3543ae | 184 | _i2c.write(CCS811_I2C_ADDR, rstCMD, 5); |
| louatayehh | 0:2c1dce3543ae | 185 | |
| louatayehh | 0:2c1dce3543ae | 186 | return false; |
| louatayehh | 0:2c1dce3543ae | 187 | |
| louatayehh | 0:2c1dce3543ae | 188 | } |