TAQOZ 2.8 Reloaded - Reading data from the CCS811 Air Quality Sensor
Following @bob_g4bby's I2C driver for the QMC5883 magnetic sensor, here is another I2C demo driver. This time for the CCS811 Air Quality sensor. The TAQOZ default I2C pins are 56 and 57 ( also onboard LEDs but works fine) but can be changed with I2CPINS if the LEDs are needed. e.g. 16 18 I2CPINS
Type GO1 to start the demo and press any key to stop it. NB update of the BASELINE and compensation for humidity and temperature are not implemented (yet )
TAQOZ \ TAQOZ Reloaded 2.8 demo driver for CCS811 Air Quality sensor \ CCS811B_driver.fth Peter Foden plus ideas from Bob Edwards \ November 2021 \ Default I2C pins: SDA=56,SCL=57, alter with I2CPINS ( SCL SDA ) \ Type "GO1" to start demo, any key to stop ALIAS I2C.START <I2C --- Matches the stop alias, I2C> --- I2C slave address of the CCS811 NB 2 x manufacturer's address # $5A --- I2C address as reported by Taqoz $B4 := I2Cadd --- Read byte from register at 'addr' pub I2CREAD ( addr -- byte ) <I2C I2Cadd I2C! I2C! <I2C I2Cadd 1+ I2C! nakI2C@ I2C> ; --- Write byte to register 'addr' NB not used, useful for reference pub I2CWRITE ( byte addr -- ) <I2C I2Cadd I2C! I2C! I2C! I2C> ; --- returns Hardware ID # pub HWID@ ( -- HWID# ) CRLF $20 I2CREAD ." Hardware ID# = " .BYTE CRLF ; --- returns Hardware VER # pub HWV@ ( -- VER# ) CRLF $21 I2CREAD ." Hardware Ver# = " .BYTE CRLF ; --- returns CCS811B status, bit 4 set = APP_VALID pub CCS@ ( -- status ) $0 I2CREAD CRLF ." STATUS = " .BIN CRLF ; --- initialise application mode pub APPSTART <I2C I2Cadd I2C! $F4 I2C! I2C> ; --- returns MEAS_MODE status pub MMS@ ( -- status ) $01 I2CREAD ." MEAS_MODE status = " .BIN CRLF ; --- returns cause of errors pub ERROR ( -- ) CRLF $E0 I2CREAD ." ERROR status = " .BIN CRLF ; pub SETUP ( -- ) --- setup measurement mode 001:mode1 every 1 Sec <I2C $B4 I2C! 1 I2C! %00010000 I2C! I2C> ; --- read RAW_DATA pub RAW@ ( -- raw_data ) <I2C I2Cadd I2C! $03 I2C! <I2C I2Cadd 1+ I2C! I2C@ 8<< nakI2C@ I2C> OR ; --- read CO2 pub CO2@ ( -- co2 ) <I2C I2Cadd I2C! $02 I2C! <I2C I2Cadd 1+ I2C! I2C@ 8<< nakI2C@ I2C> OR ; --- read CO2 and TVOC pub COTV@ ( -- co2 tvoc ) <I2C I2Cadd I2C! $02 I2C! <I2C I2Cadd 1+ I2C! I2C@ 8<< I2C@ OR I2C@ 8<< nakI2C@ I2C> OR SWAP ; --- read baseline pub RDB <I2C I2Cadd I2C! $11 I2C! <I2C I2Cadd 1+ I2C! I2C@ nakI2C@ I2C> . . ; --- reset the chip NB puts into boot loader mode! pub RSTS ( -- ) <I2C I2Cadd I2C! $FF I2C! $11 I2C! $E5 I2C! $72 I2C! $8A I2C! I2C> ; --- test if data is available - i.e. reg 0, bit 3 set pub DATA? ( -- flag ) \ --- 1 for data available $0 I2CREAD 3 >> 1 AND ; --- run hardware tests and start measurement routine pub GO1 ( -- demo ) \ run CCS811 demo - any key to stop CRLF --- 16 18 I2CPINS \ uncomment to use LEDs on P2 EDGE/EVAL boards !I2C \ Needed for reliable operation - defaults to 400kHz CRLF ." *** TAQOZ CCS811 Driver Demo ***" CRLF APPSTART ." (1) App mode set " CRLF \ initialise application mode SETUP ." (2) Measurement mode set " CRLF \ set MEAS_MODE ." (3) Measurement mode: " MMS@ \ read MEAS_MODE ." (4) Waiting 5 secs for sensor warm up..." CRLF CRLF 5000 ms BEGIN APPSTART DATA? IF COTV@ 20 ms ." Co2 = " . ." TVOC = " . CRLF THEN KEY UNTIL ; END