hello
Dependents: sht31_II ina-hack-test rapidtouchscreenprototype EMBEDDED_SYSTEM_PROJECT_GROUP_9 ... more
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; 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 00072 wait_ms(500); 00073 _i2c.start(); 00074 _i2c.read(_i2caddr, readbuffer, 6); 00075 _i2c.stop(); 00076 for (uint8_t i = 0; i < 6; i++) { 00077 printf("0x%Xd\r\n", readbuffer[i]); 00078 } 00079 uint16_t ST, SRH; 00080 ST = readbuffer[0]; 00081 ST <<= 8; 00082 ST |= readbuffer[1]; 00083 00084 if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) { 00085 return false; 00086 } 00087 00088 SRH = readbuffer[3]; 00089 SRH <<= 8; 00090 SRH |= readbuffer[4]; 00091 00092 if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) { 00093 return false; 00094 } 00095 00096 printf("ST = %d\r\n", ST); 00097 double stemp = ST; 00098 stemp *= 175; 00099 stemp /= 0xffff; 00100 stemp = -45 + stemp; 00101 temp = stemp; 00102 00103 printf("SRH = %d\r\n", SRH); 00104 double shum = SRH; 00105 shum *= 100; 00106 shum /= 0xFFFF; 00107 00108 humidity = shum; 00109 00110 return true; 00111 } 00112 00113 uint8_t Sht31::crc8(const uint8_t *data, int len) { 00114 const uint8_t POLYNOMIAL(0x31); 00115 uint8_t crc(0xFF); 00116 00117 for ( int j = len; j; --j ) { 00118 crc ^= *data++; 00119 00120 for ( int i = 8; i; --i ) { 00121 crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); 00122 } 00123 } 00124 return crc; 00125 }
Generated on Fri Jul 15 2022 03:47:50 by
1.7.2