SP1 vers 3
Dependents: SoufflerieSP1-vers2
Sht31.cpp@0:86f8e66e04fc, 2021-06-08 (annotated)
- Committer:
- petit
- Date:
- Tue Jun 08 10:37:36 2021 +0000
- Revision:
- 0:86f8e66e04fc
SP1 vers 3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
petit | 0:86f8e66e04fc | 1 | /*************************************************** |
petit | 0:86f8e66e04fc | 2 | This is a library for the SHT31 Digital Humidity & Temp Sht31 |
petit | 0:86f8e66e04fc | 3 | |
petit | 0:86f8e66e04fc | 4 | Designed specifically to work with the SHT31 Digital Sht31 from Adafruit |
petit | 0:86f8e66e04fc | 5 | ----> https://www.adafruit.com/products/2857 |
petit | 0:86f8e66e04fc | 6 | |
petit | 0:86f8e66e04fc | 7 | These displays use I2C to communicate, 2 pins are required to |
petit | 0:86f8e66e04fc | 8 | interface |
petit | 0:86f8e66e04fc | 9 | Adafruit invests time and resources providing this open source code, |
petit | 0:86f8e66e04fc | 10 | please support Adafruit and open-source hardware by purchasing |
petit | 0:86f8e66e04fc | 11 | products from Adafruit! |
petit | 0:86f8e66e04fc | 12 | |
petit | 0:86f8e66e04fc | 13 | Written by Limor Fried/Ladyada for Adafruit Industries. |
petit | 0:86f8e66e04fc | 14 | BSD license, all text above must be included in any redistribution |
petit | 0:86f8e66e04fc | 15 | ****************************************************/ |
petit | 0:86f8e66e04fc | 16 | |
petit | 0:86f8e66e04fc | 17 | #include "Sht31.h" |
petit | 0:86f8e66e04fc | 18 | #include "mbed.h" |
petit | 0:86f8e66e04fc | 19 | |
petit | 0:86f8e66e04fc | 20 | Sht31::Sht31(PinName sda, PinName scl) : _i2c(sda, scl) { |
petit | 0:86f8e66e04fc | 21 | _i2caddr = (0x44 << 1); |
petit | 0:86f8e66e04fc | 22 | reset(); |
petit | 0:86f8e66e04fc | 23 | readStatus(); |
petit | 0:86f8e66e04fc | 24 | } |
petit | 0:86f8e66e04fc | 25 | |
petit | 0:86f8e66e04fc | 26 | int Sht31::readTemperature(void) { |
petit | 0:86f8e66e04fc | 27 | if (! readTempHum()) return NAN; |
petit | 0:86f8e66e04fc | 28 | return temp; |
petit | 0:86f8e66e04fc | 29 | } |
petit | 0:86f8e66e04fc | 30 | |
petit | 0:86f8e66e04fc | 31 | int Sht31::readHumidity(void) { |
petit | 0:86f8e66e04fc | 32 | if (! readTempHum()) return NAN; |
petit | 0:86f8e66e04fc | 33 | return humidity; |
petit | 0:86f8e66e04fc | 34 | } |
petit | 0:86f8e66e04fc | 35 | |
petit | 0:86f8e66e04fc | 36 | void Sht31::reset(void) { |
petit | 0:86f8e66e04fc | 37 | writeCommand(SHT31_SOFTRESET); |
petit | 0:86f8e66e04fc | 38 | wait_ms(10); |
petit | 0:86f8e66e04fc | 39 | } |
petit | 0:86f8e66e04fc | 40 | |
petit | 0:86f8e66e04fc | 41 | uint16_t Sht31::readStatus(void) { |
petit | 0:86f8e66e04fc | 42 | writeCommand(SHT31_READSTATUS); |
petit | 0:86f8e66e04fc | 43 | char val[1]; |
petit | 0:86f8e66e04fc | 44 | _i2c.read(_i2caddr, val, 1); |
petit | 0:86f8e66e04fc | 45 | uint16_t stat = val[0]; |
petit | 0:86f8e66e04fc | 46 | stat <<= 8; |
petit | 0:86f8e66e04fc | 47 | _i2c.read(_i2caddr, val, 1); |
petit | 0:86f8e66e04fc | 48 | stat |= val[0]; |
petit | 0:86f8e66e04fc | 49 | // printf("0x%X\r\n", stat); |
petit | 0:86f8e66e04fc | 50 | return stat; |
petit | 0:86f8e66e04fc | 51 | } |
petit | 0:86f8e66e04fc | 52 | |
petit | 0:86f8e66e04fc | 53 | void Sht31::writeCommand(uint16_t cmd) { |
petit | 0:86f8e66e04fc | 54 | char buf[2]; |
petit | 0:86f8e66e04fc | 55 | buf[0] = (cmd >> 8); |
petit | 0:86f8e66e04fc | 56 | buf[1] = (cmd & 0xFF); |
petit | 0:86f8e66e04fc | 57 | _i2c.write(_i2caddr, buf, 2); |
petit | 0:86f8e66e04fc | 58 | } |
petit | 0:86f8e66e04fc | 59 | |
petit | 0:86f8e66e04fc | 60 | bool Sht31::readTempHum(void) { |
petit | 0:86f8e66e04fc | 61 | char readbuffer[6]; |
petit | 0:86f8e66e04fc | 62 | |
petit | 0:86f8e66e04fc | 63 | writeCommand(SHT31_MEAS_HIGHREP); |
petit | 0:86f8e66e04fc | 64 | |
petit | 0:86f8e66e04fc | 65 | wait_ms(500); |
petit | 0:86f8e66e04fc | 66 | _i2c.read(_i2caddr, readbuffer, 6); |
petit | 0:86f8e66e04fc | 67 | |
petit | 0:86f8e66e04fc | 68 | uint16_t ST, SRH; |
petit | 0:86f8e66e04fc | 69 | ST = readbuffer[0]; |
petit | 0:86f8e66e04fc | 70 | ST <<= 8; |
petit | 0:86f8e66e04fc | 71 | ST |= readbuffer[1]; |
petit | 0:86f8e66e04fc | 72 | |
petit | 0:86f8e66e04fc | 73 | if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) { |
petit | 0:86f8e66e04fc | 74 | return false; |
petit | 0:86f8e66e04fc | 75 | } |
petit | 0:86f8e66e04fc | 76 | |
petit | 0:86f8e66e04fc | 77 | SRH = readbuffer[3]; |
petit | 0:86f8e66e04fc | 78 | SRH <<= 8; |
petit | 0:86f8e66e04fc | 79 | SRH |= readbuffer[4]; |
petit | 0:86f8e66e04fc | 80 | |
petit | 0:86f8e66e04fc | 81 | if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) { |
petit | 0:86f8e66e04fc | 82 | return false; |
petit | 0:86f8e66e04fc | 83 | } |
petit | 0:86f8e66e04fc | 84 | |
petit | 0:86f8e66e04fc | 85 | double stemp = ST; |
petit | 0:86f8e66e04fc | 86 | stemp *= 175; |
petit | 0:86f8e66e04fc | 87 | stemp /= 0xffff; |
petit | 0:86f8e66e04fc | 88 | stemp = -45 + stemp; |
petit | 0:86f8e66e04fc | 89 | temp = stemp; |
petit | 0:86f8e66e04fc | 90 | |
petit | 0:86f8e66e04fc | 91 | double shum = SRH; |
petit | 0:86f8e66e04fc | 92 | shum *= 100; |
petit | 0:86f8e66e04fc | 93 | shum /= 0xFFFF; |
petit | 0:86f8e66e04fc | 94 | |
petit | 0:86f8e66e04fc | 95 | humidity = shum; |
petit | 0:86f8e66e04fc | 96 | |
petit | 0:86f8e66e04fc | 97 | return true; |
petit | 0:86f8e66e04fc | 98 | } |
petit | 0:86f8e66e04fc | 99 | |
petit | 0:86f8e66e04fc | 100 | uint8_t Sht31::crc8(const uint8_t *data, int len) { |
petit | 0:86f8e66e04fc | 101 | const uint8_t POLYNOMIAL(0x31); |
petit | 0:86f8e66e04fc | 102 | uint8_t crc(0xFF); |
petit | 0:86f8e66e04fc | 103 | |
petit | 0:86f8e66e04fc | 104 | for ( int j = len; j; --j ) { |
petit | 0:86f8e66e04fc | 105 | crc ^= *data++; |
petit | 0:86f8e66e04fc | 106 | |
petit | 0:86f8e66e04fc | 107 | for ( int i = 8; i; --i ) { |
petit | 0:86f8e66e04fc | 108 | crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); |
petit | 0:86f8e66e04fc | 109 | } |
petit | 0:86f8e66e04fc | 110 | } |
petit | 0:86f8e66e04fc | 111 | return crc; |
petit | 0:86f8e66e04fc | 112 | } |