Sht31 LIB for TY51822r3
Fork of Sht31 by
Sht31.cpp@3:97593ea1eff5, 2017-08-22 (annotated)
- Committer:
- sonson
- Date:
- Tue Aug 22 02:44:41 2017 +0000
- Revision:
- 3:97593ea1eff5
- Parent:
- 1:756e26f0b067
TY51822r3????
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 | |
GeofferyOmlette | 0:c90aa4f69539 | 20 | Sht31::Sht31(PinName sda, PinName scl) : _i2c(sda, scl) { |
sonson | 3:97593ea1eff5 | 21 | _i2caddr = 0x44*2; |
GeofferyOmlette | 1:756e26f0b067 | 22 | printf("\r\n"); |
GeofferyOmlette | 0:c90aa4f69539 | 23 | reset(); |
GeofferyOmlette | 0:c90aa4f69539 | 24 | readStatus(); |
GeofferyOmlette | 0:c90aa4f69539 | 25 | } |
GeofferyOmlette | 0:c90aa4f69539 | 26 | |
GeofferyOmlette | 0:c90aa4f69539 | 27 | float Sht31::readTemperature(void) { |
GeofferyOmlette | 0:c90aa4f69539 | 28 | if (! readTempHum()) return NAN; |
GeofferyOmlette | 0:c90aa4f69539 | 29 | return temp; |
GeofferyOmlette | 0:c90aa4f69539 | 30 | } |
GeofferyOmlette | 0:c90aa4f69539 | 31 | |
GeofferyOmlette | 0:c90aa4f69539 | 32 | float Sht31::readHumidity(void) { |
GeofferyOmlette | 0:c90aa4f69539 | 33 | if (! readTempHum()) return NAN; |
GeofferyOmlette | 0:c90aa4f69539 | 34 | return humidity; |
GeofferyOmlette | 0:c90aa4f69539 | 35 | } |
GeofferyOmlette | 0:c90aa4f69539 | 36 | |
GeofferyOmlette | 0:c90aa4f69539 | 37 | void Sht31::reset(void) { |
GeofferyOmlette | 1:756e26f0b067 | 38 | writeCommand(SHT31_SOFTRESET); |
GeofferyOmlette | 0:c90aa4f69539 | 39 | wait_ms(10); |
GeofferyOmlette | 0:c90aa4f69539 | 40 | } |
GeofferyOmlette | 0:c90aa4f69539 | 41 | |
GeofferyOmlette | 0:c90aa4f69539 | 42 | uint16_t Sht31::readStatus(void) { |
GeofferyOmlette | 0:c90aa4f69539 | 43 | writeCommand(SHT31_READSTATUS); |
GeofferyOmlette | 0:c90aa4f69539 | 44 | char val[1]; |
GeofferyOmlette | 0:c90aa4f69539 | 45 | _i2c.start(); |
GeofferyOmlette | 0:c90aa4f69539 | 46 | _i2c.read(_i2caddr, val, 1); |
GeofferyOmlette | 0:c90aa4f69539 | 47 | _i2c.stop(); |
GeofferyOmlette | 0:c90aa4f69539 | 48 | uint16_t stat = val[0]; |
GeofferyOmlette | 0:c90aa4f69539 | 49 | stat <<= 8; |
GeofferyOmlette | 0:c90aa4f69539 | 50 | _i2c.start(); |
GeofferyOmlette | 0:c90aa4f69539 | 51 | _i2c.read(_i2caddr, val, 1); |
GeofferyOmlette | 0:c90aa4f69539 | 52 | _i2c.stop(); |
GeofferyOmlette | 0:c90aa4f69539 | 53 | stat |= val[0]; |
GeofferyOmlette | 0:c90aa4f69539 | 54 | printf("0x%X\r\n", stat); |
GeofferyOmlette | 0:c90aa4f69539 | 55 | return stat; |
GeofferyOmlette | 0:c90aa4f69539 | 56 | } |
GeofferyOmlette | 0:c90aa4f69539 | 57 | |
GeofferyOmlette | 0:c90aa4f69539 | 58 | void Sht31::writeCommand(uint16_t cmd) { |
GeofferyOmlette | 0:c90aa4f69539 | 59 | char buf[2]; |
GeofferyOmlette | 0:c90aa4f69539 | 60 | _i2c.start(); |
GeofferyOmlette | 0:c90aa4f69539 | 61 | buf[0] = (cmd >> 8); |
GeofferyOmlette | 0:c90aa4f69539 | 62 | buf[1] = (cmd & 0xFF); |
GeofferyOmlette | 0:c90aa4f69539 | 63 | _i2c.write(_i2caddr, buf, 2); |
GeofferyOmlette | 0:c90aa4f69539 | 64 | _i2c.stop(); |
GeofferyOmlette | 0:c90aa4f69539 | 65 | } |
GeofferyOmlette | 0:c90aa4f69539 | 66 | |
GeofferyOmlette | 0:c90aa4f69539 | 67 | bool Sht31::readTempHum(void) { |
GeofferyOmlette | 0:c90aa4f69539 | 68 | char readbuffer[6]; |
GeofferyOmlette | 0:c90aa4f69539 | 69 | |
sonson | 3:97593ea1eff5 | 70 | // writeCommand(SHT31_MEAS_HIGHREP); |
sonson | 3:97593ea1eff5 | 71 | writeCommand(0x2721); |
GeofferyOmlette | 0:c90aa4f69539 | 72 | |
GeofferyOmlette | 0:c90aa4f69539 | 73 | wait_ms(500); |
GeofferyOmlette | 0:c90aa4f69539 | 74 | _i2c.start(); |
GeofferyOmlette | 0:c90aa4f69539 | 75 | _i2c.read(_i2caddr, readbuffer, 6); |
GeofferyOmlette | 0:c90aa4f69539 | 76 | _i2c.stop(); |
GeofferyOmlette | 1:756e26f0b067 | 77 | for (uint8_t i = 0; i < 6; i++) { |
GeofferyOmlette | 1:756e26f0b067 | 78 | printf("0x%Xd\r\n", readbuffer[i]); |
GeofferyOmlette | 1:756e26f0b067 | 79 | } |
GeofferyOmlette | 0:c90aa4f69539 | 80 | uint16_t ST, SRH; |
GeofferyOmlette | 0:c90aa4f69539 | 81 | ST = readbuffer[0]; |
GeofferyOmlette | 0:c90aa4f69539 | 82 | ST <<= 8; |
GeofferyOmlette | 0:c90aa4f69539 | 83 | ST |= readbuffer[1]; |
GeofferyOmlette | 0:c90aa4f69539 | 84 | |
GeofferyOmlette | 0:c90aa4f69539 | 85 | if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) { |
GeofferyOmlette | 0:c90aa4f69539 | 86 | return false; |
GeofferyOmlette | 0:c90aa4f69539 | 87 | } |
GeofferyOmlette | 0:c90aa4f69539 | 88 | |
GeofferyOmlette | 0:c90aa4f69539 | 89 | SRH = readbuffer[3]; |
GeofferyOmlette | 0:c90aa4f69539 | 90 | SRH <<= 8; |
GeofferyOmlette | 0:c90aa4f69539 | 91 | SRH |= readbuffer[4]; |
GeofferyOmlette | 0:c90aa4f69539 | 92 | |
GeofferyOmlette | 0:c90aa4f69539 | 93 | if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) { |
GeofferyOmlette | 0:c90aa4f69539 | 94 | return false; |
GeofferyOmlette | 0:c90aa4f69539 | 95 | } |
GeofferyOmlette | 0:c90aa4f69539 | 96 | |
GeofferyOmlette | 0:c90aa4f69539 | 97 | printf("ST = %d\r\n", ST); |
GeofferyOmlette | 0:c90aa4f69539 | 98 | double stemp = ST; |
GeofferyOmlette | 0:c90aa4f69539 | 99 | stemp *= 175; |
GeofferyOmlette | 0:c90aa4f69539 | 100 | stemp /= 0xffff; |
GeofferyOmlette | 0:c90aa4f69539 | 101 | stemp = -45 + stemp; |
GeofferyOmlette | 0:c90aa4f69539 | 102 | temp = stemp; |
GeofferyOmlette | 0:c90aa4f69539 | 103 | |
GeofferyOmlette | 0:c90aa4f69539 | 104 | printf("SRH = %d\r\n", SRH); |
GeofferyOmlette | 0:c90aa4f69539 | 105 | double shum = SRH; |
GeofferyOmlette | 0:c90aa4f69539 | 106 | shum *= 100; |
GeofferyOmlette | 0:c90aa4f69539 | 107 | shum /= 0xFFFF; |
GeofferyOmlette | 0:c90aa4f69539 | 108 | |
GeofferyOmlette | 0:c90aa4f69539 | 109 | humidity = shum; |
GeofferyOmlette | 0:c90aa4f69539 | 110 | |
GeofferyOmlette | 0:c90aa4f69539 | 111 | return true; |
GeofferyOmlette | 0:c90aa4f69539 | 112 | } |
GeofferyOmlette | 0:c90aa4f69539 | 113 | |
GeofferyOmlette | 0:c90aa4f69539 | 114 | uint8_t Sht31::crc8(const uint8_t *data, int len) { |
GeofferyOmlette | 0:c90aa4f69539 | 115 | const uint8_t POLYNOMIAL(0x31); |
GeofferyOmlette | 0:c90aa4f69539 | 116 | uint8_t crc(0xFF); |
GeofferyOmlette | 0:c90aa4f69539 | 117 | |
GeofferyOmlette | 0:c90aa4f69539 | 118 | for ( int j = len; j; --j ) { |
GeofferyOmlette | 0:c90aa4f69539 | 119 | crc ^= *data++; |
GeofferyOmlette | 0:c90aa4f69539 | 120 | |
GeofferyOmlette | 0:c90aa4f69539 | 121 | for ( int i = 8; i; --i ) { |
GeofferyOmlette | 0:c90aa4f69539 | 122 | crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); |
GeofferyOmlette | 0:c90aa4f69539 | 123 | } |
GeofferyOmlette | 0:c90aa4f69539 | 124 | } |
GeofferyOmlette | 0:c90aa4f69539 | 125 | return crc; |
GeofferyOmlette | 0:c90aa4f69539 | 126 | } |