Adafruit SHT40 basic driver
Basic library for Adafruit SHT40 Working on PSoC6 Pioneer dev board CY8CKIT-062-WIFI-BT
Revision 1:c6381a3f2d9a, committed 2022-02-14
- Comitter:
- reedas
- Date:
- Mon Feb 14 23:53:34 2022 +0000
- Parent:
- 0:c46b3e0a8c2b
- Commit message:
- added floating point return values;
Changed in this revision
SHT40.cpp | Show annotated file Show diff for this revision Revisions of this file |
SHT40.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r c46b3e0a8c2b -r c6381a3f2d9a SHT40.cpp --- a/SHT40.cpp Mon Feb 07 15:08:07 2022 +0000 +++ b/SHT40.cpp Mon Feb 14 23:53:34 2022 +0000 @@ -1,4 +1,4 @@ -/** + /** * @author Andrew Reed * * Freeware -- @@ -82,4 +82,56 @@ return (int)rh; } +float SHT40::tempCF(void) { + char txBuff[2]; + char rxBuff[6]; + + txBuff[0] = SHT4x_NOHEAT_HIGHPRECISION; // Triggers a temperature measure by feeding correct opcode. + txBuff[1] = SHT4x_NOHEAT_HIGHPRECISION >> 8; + i2c_->write((SHT40_I2C_ADDRESS << 1) & 0xFE, txBuff, 2); + thread_sleep_for(50); // Per datasheet, wait long enough for device to sample temperature + + // Reads triggered measure + i2c_->read((SHT40_I2C_ADDRESS << 1) | 0x01, rxBuff, 6); + thread_sleep_for(1); + + // Algorithm from arduino sht4x.h to compute temperature. + unsigned int rawTemperature = ((unsigned int) rxBuff[0] << 8) | (unsigned int) rxBuff[1]; + rawTemperature &= 0xFFFC; + + float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536 + float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14 + + return (float)realTemperature; + +} +float SHT40::relHumidF(void) { + + char txBuff[2]; + char rxBuff[6]; + + + txBuff[0] = SHT4x_NOHEAT_HIGHPRECISION; // send command. + txBuff[1] = SHT4x_NOHEAT_HIGHPRECISION >> 8; + i2c_->write((SHT40_I2C_ADDRESS << 1) & 0xFE, txBuff, 2); + thread_sleep_for(16); // delay to sample humidity + + // Reads measurement + i2c_->read((SHT40_I2C_ADDRESS << 1) | 0x01, rxBuff, 6); + thread_sleep_for(1); + + //Algorithm from sht4x.h. + unsigned int rawHumidity = ((unsigned int) rxBuff[3] << 8) | (unsigned int) rxBuff[4]; + + rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place + + //Convert to relative humidity + float RH = rawHumidity / (float)65536; // two bytes worth to float + float rh = -6 + (125 * RH); // adjustment values as per sht4x.h + rh = min(max(rh, (float)0.0), (float)100.0); + + + return (float)rh; + +} \ No newline at end of file
diff -r c46b3e0a8c2b -r c6381a3f2d9a SHT40.h --- a/SHT40.h Mon Feb 07 15:08:07 2022 +0000 +++ b/SHT40.h Mon Feb 14 23:53:34 2022 +0000 @@ -94,6 +94,11 @@ //Reads the relative humidity, input void, outputs and int. int relHumid(void); + //Reads the temperature, input void, outputs an int in celcius. + float tempCF(void); + + //Reads the relative humidity, input void, outputs and int. + float relHumidF(void); /* @ToDo populate a struct that returns temp and humidity in one call * struct tempHumid{