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: SPARKFUN_WEATHER_SHIELD
Fork of htu21d by
htu21d.h
00001 /** 00002 HTU21D / HPP828E031 driver for mbed. 00003 - Includes RTOS hooks if RTOS is detected during compile. 00004 Author: Kevin Braun 00005 **/ 00006 00007 #ifndef HTU21D_H 00008 #define HTU21D_H 00009 00010 #include "mbed.h" 00011 00012 #if(defined(TARGET_KL25Z) )//|| defined(TARGET_K64F)) 00013 00014 #define HTU21Di2cLOWLEVEL 1 //if the use of low-level I2C routines is needed 00015 #warning "HTU21D using low level I2C routines" 00016 00017 #endif 00018 00019 //Defines for HTU21D 00020 #define HTU21Di2cWRITE 0x80 00021 #define HTU21Di2cREAD 0x81 00022 00023 #define HTU21DWRITEUSER 0xE6 00024 #define HTU21DREADUSER 0xE7 00025 #define HTU21DtempNOHOLD 0xF3 00026 #define HTU21DhumNOHOLD 0xF5 00027 #define HTU21DRESET 0xFE 00028 00029 #define HTU21SNAC1 0xFC 00030 #define HTU21SNAC2 0xC9 00031 #define HTU21SNB1 0xFA 00032 #define HTU21SNB2 0x0F 00033 00034 #define HTU21DHEATER 0x04 00035 00036 00037 /** 00038 * measurement specialties / Honeywell HTU21D digital humidity and temperature sensor. 00039 * - Web site: http://www.meas-spec.com 00040 * - Part Number: HPP828E031 00041 * - HTU21D = +-3% rh error at 55% 00042 * - HTU20D = +-5% rh error at 55% 00043 * - Main code generated from datasheet dated October 2013 00044 * - Serial number code generated from App Note "HTU2X Serial Number Reading", dated Februrary 2014 00045 * - No checksum checking is performed in this code 00046 * 00047 * @code 00048 * //Tested on FRDM-K64F 00049 * 00050 * #include "mbed.h" 00051 * #include "htu21d.h" 00052 * 00053 * #define SDA PTE25 00054 * #define SCL PTE24 00055 * 00056 * Serial pc(USBTX, USBRX); //local terminal 00057 * htu21d htu(SDA, SCL); //Temp Hum || sda, scl 00058 * 00059 * float H21Temp = 0.0; //Temperture from HTU21D 00060 * float H21Hum = 0.0; //Humidity from HTU21D 00061 * float H21Dew = 0.0; //Dew Point from HTU21D 00062 * 00063 * //Note: If RTOS is used, Mutex for I2C must be initialized 00064 * #ifdef RTOS_H 00065 * Mutex MutexI2cWait; 00066 * #endif 00067 * 00068 * int main() { 00069 * pc.baud(230400); //local terminal baud 00070 * pc.printf("\r\n\r\nK64F_HTU21D basic operation\r\n"); 00071 * 00072 * //initialize the HTU21D 00073 * int htu21 = htu.softReset(); 00074 * if(htu21 == 0) { 00075 * pc.printf(" - HTU21D broken...\r\n"); 00076 * } else { 00077 * uint8_t HTU21DuserReg = htu.getUserReg(); 00078 * pc.printf("HTU21D UserReg: 0x%02x SN: 0x%04x %08x %04x\r\n", 00079 * HTU21DuserReg, htu.HTU21sn.HTU21D_sna, htu.HTU21sn.HTU21D_snb, htu.HTU21sn.HTU21D_snc); 00080 * } 00081 * 00082 * while(true) { 00083 * //get humidity, temperature and dew point from HTU21D 00084 * if(htu21 == 1) { //if HTU21D didn't initialize, don't access HTU21D anymore 00085 * H21Hum = htu.getHum(); 00086 * if((double)H21Hum == 255.0) pc.printf("\r\n*** HTU21D Hum error!!\r\n"); 00087 * H21Temp = htu.getTemp(); 00088 * if((double)H21Temp == 255.0) pc.printf("\r\n*** HTU21D Temp error!!\r\n"); 00089 * H21Dew = htu.getDewPtFast(); 00090 * } 00091 * pc.printf("Temp: %7.2f C %7.2f F Hum: %4.1f %% DewPt: %7.2f C\r\n", H21Temp, H21Hum, H21Dew); 00092 * wait(1.0); 00093 * } 00094 * } 00095 * @endcode 00096 **/ 00097 class htu21d { 00098 00099 public: 00100 /** 00101 * Constructor 00102 * - Fixed at I2C address 0x80 00103 * - I2C speed set to 400000 00104 * 00105 * @param PinName sda and scl, mbed I2C interface pins 00106 */ 00107 htu21d(I2C i2c); 00108 /** 00109 * Constructor 00110 * - Fixed at I2C address 0x80 00111 * - I2C speed set by user 00112 * 00113 * @param PinName sda and scl, mbed I2C interface pins 00114 * @param int I2C frequency 00115 */ 00116 /** 00117 * Destructor 00118 * 00119 * @param --none-- 00120 */ 00121 ~htu21d(); 00122 /** 00123 * Reset the HTU21D chip 00124 * - Waits 15mS before exiting, allowing the chip reset to finish 00125 * - Executes getSNReg() which loads up HTU21D serial number structure 00126 * 00127 * @param --none-- NOTE: run softReset() once at initialization time 00128 * 00129 * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error 00130 */ 00131 int softReset(); 00132 /** 00133 * Get HTU21D user register 00134 * 00135 * @param --none-- 00136 * 00137 * @return 8 bit user register value 00138 */ 00139 uint8_t getUserReg(); 00140 /** 00141 * Turn ON the heater on the HTU21D 00142 * 00143 * @param --none-- 00144 * 00145 * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error 00146 */ 00147 int heaterOn(); 00148 /** 00149 * Turn OFF the heater on the HTU21D 00150 * 00151 * @param --none-- 00152 * 00153 * @return success / failure of HTU21D i2c access. 1 = ok, 0 = error 00154 */ 00155 int heaterOff(); 00156 /** 00157 * Get heater on/off status of the HTU21D 00158 * 00159 * @param --none-- 00160 * 00161 * @return 0x04 = on, 0 = off 00162 */ 00163 uint8_t getHeater(); 00164 /** 00165 * Get HTU21D Temperature 00166 * 00167 * @param --none-- 00168 * 00169 * @return float of Temperature in degrees C. 255.0 if error 00170 */ 00171 float getTemp(); 00172 /** 00173 * Get HTU21D Humidity 00174 * 00175 * @param --none-- 00176 * 00177 * @return float of Humidity in percentage. 255.0 if error 00178 */ 00179 float getHum(); 00180 /** 00181 * Calculate the Dew Point 00182 * 00183 * @param --none-- NOTE: You MUST run getTemp() and getHum() first!! 00184 * 00185 * @return float of Dew Point 00186 */ 00187 float getDewPt(); 00188 /** 00189 * Calculate the Dew Point fast 00190 * - 5x faster than getDewPt() 00191 * - slightly less accurate than getDewPt() 00192 * 00193 * @param --none-- NOTE: You MUST run getTemp() and getHum() first!! 00194 * 00195 * @return float of Dew Point 00196 */ 00197 float getDewPtFast(); 00198 /** 00199 * Structure to access HTU21D's serial number 00200 * - HTU21D_sna is the hi 16 bit word of the s/n, always is 0x4854 00201 * - HTU21D_snb is the mid 32 bit word of the s/n, 0x00-------- 00202 * - HTU21D_snc is the low 16 bit word of the s/n, 0x32-- 00203 * - The complete 64 bit s/n value is: 0x48 54 00 -- -- -- 32 -- 00204 * - The numbers shown are fixed fields 00205 * - The '-' numbers are variable 00206 * - For reference, the CRC values for the s/n are included 00207 */ 00208 struct HTU21snStruct { 00209 uint16_t HTU21D_sna; /**< Highest order 16 bit word of SN 00210 - Value always = 0x4854 00211 */ 00212 uint32_t HTU21D_snb; /**< Middle order 32 bit word of SN 00213 - Value = 0x00-------- 00214 - Highest byte always = 0x00 00215 - Lower 3 bytes are variable 00216 */ 00217 uint16_t HTU21D_snc; /**< Lowest order 16 bit word of SN 00218 - Value = 0x32-- 00219 - Highest byte always = 0x32 00220 - Lowest byte is variable 00221 */ 00222 uint8_t HTU21D_crca; /**< Single byte checksum from HTU21D_sna 00223 */ 00224 uint32_t HTU21D_crcb; /**< Four byte checksum from HTU21D_snb 00225 */ 00226 uint8_t HTU21D_crcc; /**< Single byte checksum from HTU21D_snc 00227 */ 00228 HTU21snStruct() { 00229 HTU21D_sna = 0; 00230 HTU21D_snb = 0; 00231 HTU21D_snc = 0; 00232 HTU21D_crca = 0; 00233 HTU21D_crcb = 0; 00234 HTU21D_crcc = 0; 00235 } 00236 } HTU21sn; 00237 00238 private: 00239 I2C _i2c; 00240 /** 00241 * I2C access for getting raw Temperature and Humidity data 00242 * 00243 * @param 8 bit HTU21D register to get data from. Must use non-blocking regs 00244 * 00245 * @return 16 bit raw i2c data, ANDed to 14 bits 0xFFFC. 0000 if error 00246 */ 00247 uint16_t getData(uint8_t reg); 00248 /** 00249 * Get the HTU21D's serial number. 00250 * - Number returned is 0x4854 00-- ---- 32-- 00251 * - The numbers shown are fixed fields 00252 * - The '-' numbers are variable 00253 * 00254 * @param --none-- 00255 * 00256 * @return --none-- 00257 */ 00258 #if not defined HTU21Di2cLOWLEVEL 00259 char htuBuffer[8]; 00260 #endif 00261 void getSNReg(); 00262 double theTempIs; 00263 double theHumIs; 00264 }; 00265 00266 #endif
Generated on Tue Jul 26 2022 23:31:55 by
 1.7.2
 1.7.2 
    