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.
Fork of Sht31 by
Sht31.cpp@3:c4bd8cd5d566, 2018-10-03 (annotated)
- Committer:
- osilvam
- Date:
- Wed Oct 03 13:51:56 2018 +0000
- Revision:
- 3:c4bd8cd5d566
- Parent:
- 2:c84a60326ecf
first commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| GeofferyOmlette | 0:c90aa4f69539 | 1 | /*************************************************** |
| GeofferyOmlette | 0:c90aa4f69539 | 2 | This is a library for the SHT31 Digital Humidity & Temp Sht31 |
| GeofferyOmlette | 0:c90aa4f69539 | 3 | |
| GeofferyOmlette | 0:c90aa4f69539 | 4 | Designed specifically to work with the SHT31 Digital Sht31 from Adafruit |
| GeofferyOmlette | 0:c90aa4f69539 | 5 | ----> https://www.adafruit.com/products/2857 |
| GeofferyOmlette | 0:c90aa4f69539 | 6 | |
| GeofferyOmlette | 0:c90aa4f69539 | 7 | These displays use I2C to communicate, 2 pins are required to |
| GeofferyOmlette | 0:c90aa4f69539 | 8 | interface |
| GeofferyOmlette | 0:c90aa4f69539 | 9 | Adafruit invests time and resources providing this open source code, |
| GeofferyOmlette | 0:c90aa4f69539 | 10 | please support Adafruit and open-source hardware by purchasing |
| GeofferyOmlette | 0:c90aa4f69539 | 11 | products from Adafruit! |
| GeofferyOmlette | 0:c90aa4f69539 | 12 | |
| GeofferyOmlette | 0:c90aa4f69539 | 13 | Written by Limor Fried/Ladyada for Adafruit Industries. |
| GeofferyOmlette | 0:c90aa4f69539 | 14 | BSD license, all text above must be included in any redistribution |
| GeofferyOmlette | 0:c90aa4f69539 | 15 | ****************************************************/ |
| GeofferyOmlette | 0:c90aa4f69539 | 16 | |
| GeofferyOmlette | 0:c90aa4f69539 | 17 | #include "Sht31.h" |
| GeofferyOmlette | 0:c90aa4f69539 | 18 | #include "mbed.h" |
| GeofferyOmlette | 0:c90aa4f69539 | 19 | |
| osilvam | 3:c4bd8cd5d566 | 20 | Sht31::Sht31(PinName sda, PinName scl, bool i2caddr) : _i2c(sda, scl) { |
| osilvam | 3:c4bd8cd5d566 | 21 | _i2caddr = !i2caddr ? (0x44 << 1) : (0x45 << 1); |
| GeofferyOmlette | 0:c90aa4f69539 | 22 | reset(); |
| GeofferyOmlette | 0:c90aa4f69539 | 23 | readStatus(); |
| GeofferyOmlette | 0:c90aa4f69539 | 24 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 25 | |
| GeofferyOmlette | 0:c90aa4f69539 | 26 | float Sht31::readTemperature(void) { |
| GeofferyOmlette | 0:c90aa4f69539 | 27 | if (! readTempHum()) return NAN; |
| GeofferyOmlette | 0:c90aa4f69539 | 28 | return temp; |
| GeofferyOmlette | 0:c90aa4f69539 | 29 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 30 | |
| GeofferyOmlette | 0:c90aa4f69539 | 31 | float Sht31::readHumidity(void) { |
| GeofferyOmlette | 0:c90aa4f69539 | 32 | if (! readTempHum()) return NAN; |
| GeofferyOmlette | 0:c90aa4f69539 | 33 | return humidity; |
| GeofferyOmlette | 0:c90aa4f69539 | 34 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 35 | |
| GeofferyOmlette | 0:c90aa4f69539 | 36 | void Sht31::reset(void) { |
| GeofferyOmlette | 1:756e26f0b067 | 37 | writeCommand(SHT31_SOFTRESET); |
| GeofferyOmlette | 0:c90aa4f69539 | 38 | wait_ms(10); |
| GeofferyOmlette | 0:c90aa4f69539 | 39 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 40 | |
| GeofferyOmlette | 0:c90aa4f69539 | 41 | uint16_t Sht31::readStatus(void) { |
| GeofferyOmlette | 0:c90aa4f69539 | 42 | writeCommand(SHT31_READSTATUS); |
| GeofferyOmlette | 0:c90aa4f69539 | 43 | char val[1]; |
| GeofferyOmlette | 0:c90aa4f69539 | 44 | _i2c.read(_i2caddr, val, 1); |
| GeofferyOmlette | 0:c90aa4f69539 | 45 | uint16_t stat = val[0]; |
| GeofferyOmlette | 0:c90aa4f69539 | 46 | stat <<= 8; |
| GeofferyOmlette | 0:c90aa4f69539 | 47 | _i2c.read(_i2caddr, val, 1); |
| GeofferyOmlette | 0:c90aa4f69539 | 48 | stat |= val[0]; |
| andcor02 | 2:c84a60326ecf | 49 | // printf("0x%X\r\n", stat); |
| GeofferyOmlette | 0:c90aa4f69539 | 50 | return stat; |
| GeofferyOmlette | 0:c90aa4f69539 | 51 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 52 | |
| GeofferyOmlette | 0:c90aa4f69539 | 53 | void Sht31::writeCommand(uint16_t cmd) { |
| GeofferyOmlette | 0:c90aa4f69539 | 54 | char buf[2]; |
| GeofferyOmlette | 0:c90aa4f69539 | 55 | buf[0] = (cmd >> 8); |
| GeofferyOmlette | 0:c90aa4f69539 | 56 | buf[1] = (cmd & 0xFF); |
| GeofferyOmlette | 0:c90aa4f69539 | 57 | _i2c.write(_i2caddr, buf, 2); |
| GeofferyOmlette | 0:c90aa4f69539 | 58 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 59 | |
| GeofferyOmlette | 0:c90aa4f69539 | 60 | bool Sht31::readTempHum(void) { |
| GeofferyOmlette | 0:c90aa4f69539 | 61 | char readbuffer[6]; |
| GeofferyOmlette | 0:c90aa4f69539 | 62 | |
| GeofferyOmlette | 0:c90aa4f69539 | 63 | writeCommand(SHT31_MEAS_HIGHREP); |
| GeofferyOmlette | 0:c90aa4f69539 | 64 | |
| GeofferyOmlette | 0:c90aa4f69539 | 65 | wait_ms(500); |
| GeofferyOmlette | 0:c90aa4f69539 | 66 | _i2c.read(_i2caddr, readbuffer, 6); |
| andcor02 | 2:c84a60326ecf | 67 | |
| GeofferyOmlette | 0:c90aa4f69539 | 68 | uint16_t ST, SRH; |
| GeofferyOmlette | 0:c90aa4f69539 | 69 | ST = readbuffer[0]; |
| GeofferyOmlette | 0:c90aa4f69539 | 70 | ST <<= 8; |
| GeofferyOmlette | 0:c90aa4f69539 | 71 | ST |= readbuffer[1]; |
| GeofferyOmlette | 0:c90aa4f69539 | 72 | |
| GeofferyOmlette | 0:c90aa4f69539 | 73 | if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) { |
| GeofferyOmlette | 0:c90aa4f69539 | 74 | return false; |
| GeofferyOmlette | 0:c90aa4f69539 | 75 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 76 | |
| GeofferyOmlette | 0:c90aa4f69539 | 77 | SRH = readbuffer[3]; |
| GeofferyOmlette | 0:c90aa4f69539 | 78 | SRH <<= 8; |
| GeofferyOmlette | 0:c90aa4f69539 | 79 | SRH |= readbuffer[4]; |
| GeofferyOmlette | 0:c90aa4f69539 | 80 | |
| GeofferyOmlette | 0:c90aa4f69539 | 81 | if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) { |
| GeofferyOmlette | 0:c90aa4f69539 | 82 | return false; |
| GeofferyOmlette | 0:c90aa4f69539 | 83 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 84 | |
| GeofferyOmlette | 0:c90aa4f69539 | 85 | double stemp = ST; |
| GeofferyOmlette | 0:c90aa4f69539 | 86 | stemp *= 175; |
| GeofferyOmlette | 0:c90aa4f69539 | 87 | stemp /= 0xffff; |
| GeofferyOmlette | 0:c90aa4f69539 | 88 | stemp = -45 + stemp; |
| GeofferyOmlette | 0:c90aa4f69539 | 89 | temp = stemp; |
| GeofferyOmlette | 0:c90aa4f69539 | 90 | |
| GeofferyOmlette | 0:c90aa4f69539 | 91 | double shum = SRH; |
| GeofferyOmlette | 0:c90aa4f69539 | 92 | shum *= 100; |
| GeofferyOmlette | 0:c90aa4f69539 | 93 | shum /= 0xFFFF; |
| GeofferyOmlette | 0:c90aa4f69539 | 94 | |
| GeofferyOmlette | 0:c90aa4f69539 | 95 | humidity = shum; |
| GeofferyOmlette | 0:c90aa4f69539 | 96 | |
| GeofferyOmlette | 0:c90aa4f69539 | 97 | return true; |
| GeofferyOmlette | 0:c90aa4f69539 | 98 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 99 | |
| osilvam | 3:c4bd8cd5d566 | 100 | |
| osilvam | 3:c4bd8cd5d566 | 101 | bool Sht31::readTempHum(float &_temp, float &_humidity) { |
| osilvam | 3:c4bd8cd5d566 | 102 | char readbuffer[6]; |
| osilvam | 3:c4bd8cd5d566 | 103 | |
| osilvam | 3:c4bd8cd5d566 | 104 | writeCommand(SHT31_MEAS_HIGHREP); |
| osilvam | 3:c4bd8cd5d566 | 105 | |
| osilvam | 3:c4bd8cd5d566 | 106 | wait_ms(500); |
| osilvam | 3:c4bd8cd5d566 | 107 | _i2c.read(_i2caddr, readbuffer, 6); |
| osilvam | 3:c4bd8cd5d566 | 108 | |
| osilvam | 3:c4bd8cd5d566 | 109 | uint16_t ST, SRH; |
| osilvam | 3:c4bd8cd5d566 | 110 | ST = readbuffer[0]; |
| osilvam | 3:c4bd8cd5d566 | 111 | ST <<= 8; |
| osilvam | 3:c4bd8cd5d566 | 112 | ST |= readbuffer[1]; |
| osilvam | 3:c4bd8cd5d566 | 113 | |
| osilvam | 3:c4bd8cd5d566 | 114 | if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) { |
| osilvam | 3:c4bd8cd5d566 | 115 | return false; |
| osilvam | 3:c4bd8cd5d566 | 116 | } |
| osilvam | 3:c4bd8cd5d566 | 117 | |
| osilvam | 3:c4bd8cd5d566 | 118 | SRH = readbuffer[3]; |
| osilvam | 3:c4bd8cd5d566 | 119 | SRH <<= 8; |
| osilvam | 3:c4bd8cd5d566 | 120 | SRH |= readbuffer[4]; |
| osilvam | 3:c4bd8cd5d566 | 121 | |
| osilvam | 3:c4bd8cd5d566 | 122 | if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) { |
| osilvam | 3:c4bd8cd5d566 | 123 | return false; |
| osilvam | 3:c4bd8cd5d566 | 124 | } |
| osilvam | 3:c4bd8cd5d566 | 125 | |
| osilvam | 3:c4bd8cd5d566 | 126 | double stemp = ST; |
| osilvam | 3:c4bd8cd5d566 | 127 | stemp *= 175; |
| osilvam | 3:c4bd8cd5d566 | 128 | stemp /= 0xffff; |
| osilvam | 3:c4bd8cd5d566 | 129 | stemp = -45 + stemp; |
| osilvam | 3:c4bd8cd5d566 | 130 | temp = stemp; |
| osilvam | 3:c4bd8cd5d566 | 131 | _temp = temp; |
| osilvam | 3:c4bd8cd5d566 | 132 | |
| osilvam | 3:c4bd8cd5d566 | 133 | double shum = SRH; |
| osilvam | 3:c4bd8cd5d566 | 134 | shum *= 100; |
| osilvam | 3:c4bd8cd5d566 | 135 | shum /= 0xFFFF; |
| osilvam | 3:c4bd8cd5d566 | 136 | |
| osilvam | 3:c4bd8cd5d566 | 137 | humidity = shum; |
| osilvam | 3:c4bd8cd5d566 | 138 | _humidity = humidity; |
| osilvam | 3:c4bd8cd5d566 | 139 | |
| osilvam | 3:c4bd8cd5d566 | 140 | return true; |
| osilvam | 3:c4bd8cd5d566 | 141 | } |
| osilvam | 3:c4bd8cd5d566 | 142 | |
| osilvam | 3:c4bd8cd5d566 | 143 | |
| GeofferyOmlette | 0:c90aa4f69539 | 144 | uint8_t Sht31::crc8(const uint8_t *data, int len) { |
| GeofferyOmlette | 0:c90aa4f69539 | 145 | const uint8_t POLYNOMIAL(0x31); |
| GeofferyOmlette | 0:c90aa4f69539 | 146 | uint8_t crc(0xFF); |
| GeofferyOmlette | 0:c90aa4f69539 | 147 | |
| GeofferyOmlette | 0:c90aa4f69539 | 148 | for ( int j = len; j; --j ) { |
| GeofferyOmlette | 0:c90aa4f69539 | 149 | crc ^= *data++; |
| GeofferyOmlette | 0:c90aa4f69539 | 150 | |
| GeofferyOmlette | 0:c90aa4f69539 | 151 | for ( int i = 8; i; --i ) { |
| GeofferyOmlette | 0:c90aa4f69539 | 152 | crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); |
| GeofferyOmlette | 0:c90aa4f69539 | 153 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 154 | } |
| GeofferyOmlette | 0:c90aa4f69539 | 155 | return crc; |
| GeofferyOmlette | 0:c90aa4f69539 | 156 | } |
