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