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.
Dependencies: WNCInterface mbed-rtos mbed
HTS221.cpp
00001 /* =================================================================== 00002 Copyright © 2016, AVNET Inc. 00003 00004 Licensed under the Apache License, Version 2.0 (the "License"); 00005 you may not use this file except in compliance with the License. 00006 You may obtain a copy of the License at 00007 00008 http://www.apache.org/licenses/LICENSE-2.0 00009 00010 Unless required by applicable law or agreed to in writing, 00011 software distributed under the License is distributed on an 00012 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 00013 either express or implied. See the License for the specific 00014 language governing permissions and limitations under the License. 00015 00016 ======================================================================== */ 00017 00018 #include "HTS221.h" 00019 00020 00021 // ------------------------------------------------------------------------------ 00022 //jmf -- define I2C pins and functions to read & write to I2C device 00023 00024 #include <string> 00025 #include "mbed.h" 00026 #include "hardware.h" 00027 00028 // Read a single unsigned char from addressToRead and return it as a unsigned char 00029 unsigned char HTS221::readRegister(unsigned char slaveAddress, unsigned char ToRead) 00030 { 00031 char data = ToRead; 00032 00033 //i2c.write(slaveAddress, &data, 1, 0); 00034 i2c.write(slaveAddress, &data, 1, 1); //by Stefan 00035 i2c.read(slaveAddress, &data, 1, 0); 00036 return data; 00037 } 00038 00039 // Writes a single unsigned char (dataToWrite) into regToWrite 00040 int HTS221::writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite) 00041 { 00042 const char data[] = {regToWrite, dataToWrite}; 00043 00044 return i2c.write(slaveAddress,data,2,0); 00045 } 00046 00047 00048 //jmf end 00049 // ------------------------------------------------------------------------------ 00050 00051 HTS221::HTS221(void) : _address(HTS221_ADDRESS) { 00052 _temperature = 0; 00053 _humidity = 0; 00054 } 00055 00056 00057 int HTS221::init(void) { 00058 uint8_t data; 00059 00060 data = readRegister(_address, WHO_AM_I); 00061 if (data == WHO_AM_I_RETURN){ 00062 if (activate()){ 00063 storeCalibration(); 00064 return data; 00065 } 00066 } 00067 00068 return 0; 00069 } 00070 00071 int HTS221::storeCalibration(void) { 00072 uint8_t data; 00073 uint16_t tmp; 00074 00075 for (int reg=CALIB_START; reg<=CALIB_END; reg++) { 00076 if ((reg!=CALIB_START+8) && (reg!=CALIB_START+9) && (reg!=CALIB_START+4)) { 00077 00078 data = readRegister(HTS221_ADDRESS, reg); 00079 00080 switch (reg) { 00081 case CALIB_START: 00082 _h0_rH = data; 00083 break; 00084 case CALIB_START+1: 00085 _h1_rH = data; 00086 break; 00087 case CALIB_START+2: 00088 _T0_degC = data; 00089 break; 00090 case CALIB_START+3: 00091 _T1_degC = data; 00092 break; 00093 00094 case CALIB_START+5: 00095 tmp = _T0_degC; 00096 _T0_degC = (data&0x3)<<8; 00097 _T0_degC |= tmp; 00098 00099 tmp = _T1_degC; 00100 _T1_degC = ((data&0xC)>>2)<<8; 00101 _T1_degC |= tmp; 00102 break; 00103 case CALIB_START+6: 00104 _H0_T0 = data; 00105 break; 00106 case CALIB_START+7: 00107 _H0_T0 |= data<<8; 00108 break; 00109 case CALIB_START+0xA: 00110 _H1_T0 = data; 00111 break; 00112 case CALIB_START+0xB: 00113 _H1_T0 |= data <<8; 00114 break; 00115 case CALIB_START+0xC: 00116 _T0_OUT = data; 00117 break; 00118 case CALIB_START+0xD: 00119 _T0_OUT |= data << 8; 00120 break; 00121 case CALIB_START+0xE: 00122 _T1_OUT = data; 00123 break; 00124 case CALIB_START+0xF: 00125 _T1_OUT |= data << 8; 00126 break; 00127 00128 00129 case CALIB_START+8: 00130 case CALIB_START+9: 00131 case CALIB_START+4: 00132 //DO NOTHING 00133 break; 00134 00135 // to cover any possible error 00136 default: 00137 return false; 00138 } /* switch */ 00139 } /* if */ 00140 } /* for */ 00141 return true; 00142 } 00143 00144 00145 int HTS221::activate(void) { 00146 uint8_t data; 00147 00148 data = readRegister(_address, CTRL_REG1); 00149 data |= POWER_UP; 00150 data |= ODR0_SET; 00151 writeRegister(_address, CTRL_REG1, data); 00152 00153 return true; 00154 } 00155 00156 00157 int HTS221::deactivate(void) { 00158 uint8_t data; 00159 00160 data = readRegister(_address, CTRL_REG1); 00161 data &= ~POWER_UP; 00162 writeRegister(_address, CTRL_REG1, data); 00163 return true; 00164 } 00165 00166 00167 int HTS221::bduActivate(void) { 00168 uint8_t data; 00169 00170 data = readRegister(_address, CTRL_REG1); 00171 data |= BDU_SET; 00172 writeRegister(_address, CTRL_REG1, data); 00173 00174 return true; 00175 } 00176 00177 00178 int HTS221::bduDeactivate(void) { 00179 uint8_t data; 00180 00181 data = readRegister(_address, CTRL_REG1); 00182 data &= ~BDU_SET; 00183 writeRegister(_address, CTRL_REG1, data); 00184 return true; 00185 } 00186 00187 00188 int HTS221::readHumidity(void) { 00189 uint8_t data = 0; 00190 uint16_t h_out = 0; 00191 double h_temp = 0.0; 00192 double hum = 0.0; 00193 00194 data = readRegister(_address, STATUS_REG); 00195 00196 if (data & HUMIDITY_READY) { 00197 data = readRegister(_address, HUMIDITY_H_REG); 00198 h_out = data << 8; // MSB 00199 data = readRegister(_address, HUMIDITY_L_REG); 00200 h_out |= data; // LSB 00201 00202 // Decode Humidity 00203 hum = ((int16_t)(_h1_rH) - (int16_t)(_h0_rH))/2.0; // remove x2 multiple 00204 00205 // Calculate humidity in decimal of grade centigrades i.e. 15.0 = 150. 00206 h_temp = (((int16_t)h_out - (int16_t)_H0_T0) * hum) / ((int16_t)_H1_T0 - (int16_t)_H0_T0); 00207 hum = ((int16_t)_h0_rH) / 2.0; // remove x2 multiple 00208 _humidity = (int16_t)((hum + h_temp)); // provide signed % measurement unit 00209 } 00210 return _humidity; 00211 } 00212 00213 00214 00215 double HTS221::readTemperature(void) { 00216 uint8_t data = 0; 00217 uint16_t t_out = 0; 00218 double t_temp = 0.0; 00219 double deg = 0.0; 00220 00221 data = readRegister(_address, STATUS_REG); 00222 00223 if (data & TEMPERATURE_READY) { 00224 00225 data= readRegister(_address, TEMP_H_REG); 00226 t_out = data << 8; // MSB 00227 data = readRegister(_address, TEMP_L_REG); 00228 t_out |= data; // LSB 00229 00230 // Decode Temperature 00231 deg = ((int16_t)(_T1_degC) - (int16_t)(_T0_degC))/8.0; // remove x8 multiple 00232 00233 // Calculate Temperature in decimal of grade centigrades i.e. 15.0 = 150. 00234 t_temp = (((int16_t)t_out - (int16_t)_T0_OUT) * deg) / ((int16_t)_T1_OUT - (int16_t)_T0_OUT); 00235 deg = ((int16_t)_T0_degC) / 8.0; // remove x8 multiple 00236 _temperature = deg + t_temp; // provide signed celsius measurement unit 00237 } 00238 00239 return _temperature; 00240 }
Generated on Fri Jul 15 2022 21:54:07 by
1.7.2