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
00001 /*************************************************** 00002 This is a library for the SHT31 Digital Humidity & Temp Sht31 00003 00004 Designed specifically to work with the SHT31 Digital Sht31 from Adafruit 00005 ----> https://www.adafruit.com/products/2857 00006 00007 These displays use I2C to communicate, 2 pins are required to 00008 interface 00009 Adafruit invests time and resources providing this open source code, 00010 please support Adafruit and open-source hardware by purchasing 00011 products from Adafruit! 00012 00013 Written by Limor Fried/Ladyada for Adafruit Industries. 00014 BSD license, all text above must be included in any redistribution 00015 ****************************************************/ 00016 00017 #include "Sht31.h" 00018 #include "mbed.h" 00019 00020 Sht31::Sht31(PinName sda, PinName scl) : _i2c(sda, scl) { 00021 _i2caddr = 0x44*2; 00022 printf("\r\n"); 00023 reset(); 00024 readStatus(); 00025 } 00026 00027 float Sht31::readTemperature(void) { 00028 if (! readTempHum()) return NAN; 00029 return temp; 00030 } 00031 00032 float Sht31::readHumidity(void) { 00033 if (! readTempHum()) return NAN; 00034 return humidity; 00035 } 00036 00037 void Sht31::reset(void) { 00038 writeCommand(SHT31_SOFTRESET); 00039 wait_ms(10); 00040 } 00041 00042 uint16_t Sht31::readStatus(void) { 00043 writeCommand(SHT31_READSTATUS); 00044 char val[1]; 00045 _i2c.start(); 00046 _i2c.read(_i2caddr, val, 1); 00047 _i2c.stop(); 00048 uint16_t stat = val[0]; 00049 stat <<= 8; 00050 _i2c.start(); 00051 _i2c.read(_i2caddr, val, 1); 00052 _i2c.stop(); 00053 stat |= val[0]; 00054 printf("0x%X\r\n", stat); 00055 return stat; 00056 } 00057 00058 void Sht31::writeCommand(uint16_t cmd) { 00059 char buf[2]; 00060 _i2c.start(); 00061 buf[0] = (cmd >> 8); 00062 buf[1] = (cmd & 0xFF); 00063 _i2c.write(_i2caddr, buf, 2); 00064 _i2c.stop(); 00065 } 00066 00067 bool Sht31::readTempHum(void) { 00068 char readbuffer[6]; 00069 00070 // writeCommand(SHT31_MEAS_HIGHREP); 00071 writeCommand(0x2721); 00072 00073 wait_ms(500); 00074 _i2c.start(); 00075 _i2c.read(_i2caddr, readbuffer, 6); 00076 _i2c.stop(); 00077 for (uint8_t i = 0; i < 6; i++) { 00078 printf("0x%Xd\r\n", readbuffer[i]); 00079 } 00080 uint16_t ST, SRH; 00081 ST = readbuffer[0]; 00082 ST <<= 8; 00083 ST |= readbuffer[1]; 00084 00085 if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) { 00086 return false; 00087 } 00088 00089 SRH = readbuffer[3]; 00090 SRH <<= 8; 00091 SRH |= readbuffer[4]; 00092 00093 if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) { 00094 return false; 00095 } 00096 00097 printf("ST = %d\r\n", ST); 00098 double stemp = ST; 00099 stemp *= 175; 00100 stemp /= 0xffff; 00101 stemp = -45 + stemp; 00102 temp = stemp; 00103 00104 printf("SRH = %d\r\n", SRH); 00105 double shum = SRH; 00106 shum *= 100; 00107 shum /= 0xFFFF; 00108 00109 humidity = shum; 00110 00111 return true; 00112 } 00113 00114 uint8_t Sht31::crc8(const uint8_t *data, int len) { 00115 const uint8_t POLYNOMIAL(0x31); 00116 uint8_t crc(0xFF); 00117 00118 for ( int j = len; j; --j ) { 00119 crc ^= *data++; 00120 00121 for ( int i = 8; i; --i ) { 00122 crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); 00123 } 00124 } 00125 return crc; 00126 }
Generated on Fri Jul 22 2022 18:19:28 by
1.7.2
