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.
Dependents: test_ncleee WeatherStation Temp_hum PROJ ... more
SHT21_ncleee.h
00001 /* Copyright (c) 2012 Graeme Coapes, Newcastle University, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 00020 #include "mbed.h" 00021 00022 00023 #ifndef SHT21_ncleee 00024 #define SHT21_ncleee 00025 00026 /* 00027 * Constants used in communication 00028 * 00029 * Refer to datasheet for full explanation 00030 */ 00031 00032 //Sensor I2C address 00033 #define SHT_I2C_ADDR 0x80 00034 00035 //Commands... 00036 //Trigger Temp with hold master 00037 #define SHT_TRIG_TEMP_HOLD 0xE3 00038 //Trigger RH with hold master 00039 #define SHT_TRIG_RH_HOLD 0xE5 00040 //Trigger Temp with no hold master 00041 #define SHT_TRIG_TEMP 0xF3 00042 //Trigger RH with no hold master 00043 #define SHT_TRIG_RH 0xF5 00044 //Write to user register 00045 #define SHT_WRITE_REG 0xE6 00046 //Read from user register 00047 #define SHT_READ_REG 0xE7 00048 //Soft reset the sensor 00049 #define SHT_SOFT_RESET 0xFE 00050 00051 //User Register information 00052 00053 //Data precision settings 00054 //RH 12 T 14 - default 00055 #define SHT_PREC_1214 0x00 00056 //RH 8 T 10 00057 #define SHT_PREC_0812 0x01 00058 //RH 10 T 13 00059 #define SHT_PREC_1013 0x80 00060 //RH 11 T 11 00061 #define SHT_PREC_1111 0x81 00062 00063 //Battery status 00064 #define SHT_BATTERY_STAT 0x40 00065 //Enable on chip heater 00066 #define SHT_HEATER 0x04 00067 //Disable OTP reload 00068 #define SHT_DISABLE_OTP 0x02 00069 00070 00071 //Fail conditions on the I2C bus 00072 #define SHT_FAIL 1 00073 #define SHT_SUCCESS 0 00074 00075 //Author fail conditions 00076 //1, 2, 3 can be used because these are status bits 00077 //in the received measurement value 00078 #define SHT_GOOD 0xFFFC 00079 #define SHT_TRIG_FAIL 1 00080 #define SHT_READ_FAIL 2 00081 00082 /** SHT21 Connection class, utilizing a I2C interface 00083 * 00084 * Example: 00085 * @code 00086 * #include "mbed.h" 00087 * #include "SHT21_ncleee.h" 00088 * 00089 * 00090 * DigitalOut myled(LED1); 00091 * Serial pc(USBTX, USBRX); 00092 * I2C i2c(p28,p27); 00093 * SHT21 sht(&i2c); 00094 * 00095 * int main() 00096 * { 00097 * 00098 * pc.printf("Hello World...\n\tTesting temperature Sensor\n"); 00099 * 00100 * int temperature = sht.readTemp(); 00101 * 00102 * pc.printf("Temperature is: %d \n", temperature); 00103 * 00104 * pc.printf("Experiment complete...\n"); 00105 * 00106 * } 00107 * 00108 * @endcode 00109 * 00110 * 00111 */ 00112 class SHT21 00113 { 00114 private: 00115 I2C *_i2c; 00116 // Serial *_pc; 00117 int triggerTemp(); 00118 int requestTemp(); 00119 unsigned short temperature; 00120 int triggerRH(); 00121 int requestRH(); 00122 unsigned short humidity; 00123 int wr(int cmd); 00124 00125 public: 00126 00127 /** Constructor - create a connection to a SHT21 temperature and humidity sensor 00128 * through an I2C interface 00129 * 00130 * @param *i2c a pointer to the i2c interface that is used for communication 00131 */ 00132 SHT21(I2C *i2c); 00133 00134 /** Read the temperature value from the sensor \n 00135 * 00136 * Involves triggering the measuring unit then 00137 * waiting for 100ms for the measuring to complete 00138 * before reading the temperature 00139 * 00140 * @param returns a value representing the temperature in degrees centigrade 00141 */ 00142 float readTemp(); 00143 00144 /** Read the humidity value from the sensor \n 00145 * 00146 * Involves triggering the measuring unit then 00147 * waiting for 100ms for the measuring to complete 00148 * before reading the humidity 00149 * 00150 * @param returns the percentage humidity 00151 */ 00152 float readHumidity(); 00153 00154 /** 00155 * Perform a soft-reset of the sensor unit. 00156 */ 00157 int reset(); 00158 00159 /** 00160 * Set the precision of the measuring 00161 * 00162 * //Data precision settings \n 00163 * //RH 12 T 14 - default \n 00164 * #define SHT_PREC_1214 0x00 \n 00165 * //RH 8 T 10 \n 00166 * #define SHT_PREC_0812 0x01 \n 00167 * //RH 10 T 13 \n 00168 * #define SHT_PREC_1013 0x80 \n 00169 * //RH 11 T 11 \n 00170 * #define SHT_PREC_1111 0x81 \n 00171 * 00172 * @param precision - the precision, refer to above or datasheet. 00173 * 00174 */ 00175 int setPrecision(char precision); 00176 00177 }; 00178 00179 00180 #endif
Generated on Tue Jul 12 2022 21:35:21 by
1.7.2