working with mbed os 5.5
Dependents: Mbed-Connect-Cloud-Demo Mbed-Connect-Cloud-Demo mbed-cloud-connect-sensor-laser-distance2 mbed-cloud-connect-sensor-laser-distance2 ... more
Fork of Sht31 by
Sht31.cpp@2:c84a60326ecf, 2017-09-11 (annotated)
- Committer:
- andcor02
- Date:
- Mon Sep 11 13:51:03 2017 +0000
- Revision:
- 2:c84a60326ecf
- Parent:
- 1:756e26f0b067
Working with mbed os 5.5
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) { |
andcor02 | 2:c84a60326ecf | 21 | _i2caddr = (0x44 << 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 | |
GeofferyOmlette | 0:c90aa4f69539 | 100 | uint8_t Sht31::crc8(const uint8_t *data, int len) { |
GeofferyOmlette | 0:c90aa4f69539 | 101 | const uint8_t POLYNOMIAL(0x31); |
GeofferyOmlette | 0:c90aa4f69539 | 102 | uint8_t crc(0xFF); |
GeofferyOmlette | 0:c90aa4f69539 | 103 | |
GeofferyOmlette | 0:c90aa4f69539 | 104 | for ( int j = len; j; --j ) { |
GeofferyOmlette | 0:c90aa4f69539 | 105 | crc ^= *data++; |
GeofferyOmlette | 0:c90aa4f69539 | 106 | |
GeofferyOmlette | 0:c90aa4f69539 | 107 | for ( int i = 8; i; --i ) { |
GeofferyOmlette | 0:c90aa4f69539 | 108 | crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); |
GeofferyOmlette | 0:c90aa4f69539 | 109 | } |
GeofferyOmlette | 0:c90aa4f69539 | 110 | } |
GeofferyOmlette | 0:c90aa4f69539 | 111 | return crc; |
GeofferyOmlette | 0:c90aa4f69539 | 112 | } |