![](/media/cache/profiles/aa7fd480bd75d86948e7f7aac11d2704.jpg.50x50_q85.jpg)
ATT example code
Dependencies: WNCInterface mbed-rtos mbed
HTS221.cpp@0:2bfa06d10e28, 2017-05-22 (annotated)
- Committer:
- surajdagar
- Date:
- Mon May 22 19:50:26 2017 +0000
- Revision:
- 0:2bfa06d10e28
Wireless solution ATT kit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
surajdagar | 0:2bfa06d10e28 | 1 | /* =================================================================== |
surajdagar | 0:2bfa06d10e28 | 2 | Copyright © 2016, AVNET Inc. |
surajdagar | 0:2bfa06d10e28 | 3 | |
surajdagar | 0:2bfa06d10e28 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
surajdagar | 0:2bfa06d10e28 | 5 | you may not use this file except in compliance with the License. |
surajdagar | 0:2bfa06d10e28 | 6 | You may obtain a copy of the License at |
surajdagar | 0:2bfa06d10e28 | 7 | |
surajdagar | 0:2bfa06d10e28 | 8 | http://www.apache.org/licenses/LICENSE-2.0 |
surajdagar | 0:2bfa06d10e28 | 9 | |
surajdagar | 0:2bfa06d10e28 | 10 | Unless required by applicable law or agreed to in writing, |
surajdagar | 0:2bfa06d10e28 | 11 | software distributed under the License is distributed on an |
surajdagar | 0:2bfa06d10e28 | 12 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
surajdagar | 0:2bfa06d10e28 | 13 | either express or implied. See the License for the specific |
surajdagar | 0:2bfa06d10e28 | 14 | language governing permissions and limitations under the License. |
surajdagar | 0:2bfa06d10e28 | 15 | |
surajdagar | 0:2bfa06d10e28 | 16 | ======================================================================== */ |
surajdagar | 0:2bfa06d10e28 | 17 | |
surajdagar | 0:2bfa06d10e28 | 18 | #include "HTS221.h" |
surajdagar | 0:2bfa06d10e28 | 19 | |
surajdagar | 0:2bfa06d10e28 | 20 | |
surajdagar | 0:2bfa06d10e28 | 21 | // ------------------------------------------------------------------------------ |
surajdagar | 0:2bfa06d10e28 | 22 | //jmf -- define I2C pins and functions to read & write to I2C device |
surajdagar | 0:2bfa06d10e28 | 23 | |
surajdagar | 0:2bfa06d10e28 | 24 | #include <string> |
surajdagar | 0:2bfa06d10e28 | 25 | #include "mbed.h" |
surajdagar | 0:2bfa06d10e28 | 26 | #include "hardware.h" |
surajdagar | 0:2bfa06d10e28 | 27 | |
surajdagar | 0:2bfa06d10e28 | 28 | // Read a single unsigned char from addressToRead and return it as a unsigned char |
surajdagar | 0:2bfa06d10e28 | 29 | unsigned char HTS221::readRegister(unsigned char slaveAddress, unsigned char ToRead) |
surajdagar | 0:2bfa06d10e28 | 30 | { |
surajdagar | 0:2bfa06d10e28 | 31 | char data = ToRead; |
surajdagar | 0:2bfa06d10e28 | 32 | |
surajdagar | 0:2bfa06d10e28 | 33 | //i2c.write(slaveAddress, &data, 1, 0); |
surajdagar | 0:2bfa06d10e28 | 34 | i2c.write(slaveAddress, &data, 1, 1); //by Stefan |
surajdagar | 0:2bfa06d10e28 | 35 | i2c.read(slaveAddress, &data, 1, 0); |
surajdagar | 0:2bfa06d10e28 | 36 | return data; |
surajdagar | 0:2bfa06d10e28 | 37 | } |
surajdagar | 0:2bfa06d10e28 | 38 | |
surajdagar | 0:2bfa06d10e28 | 39 | // Writes a single unsigned char (dataToWrite) into regToWrite |
surajdagar | 0:2bfa06d10e28 | 40 | int HTS221::writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite) |
surajdagar | 0:2bfa06d10e28 | 41 | { |
surajdagar | 0:2bfa06d10e28 | 42 | const char data[] = {regToWrite, dataToWrite}; |
surajdagar | 0:2bfa06d10e28 | 43 | |
surajdagar | 0:2bfa06d10e28 | 44 | return i2c.write(slaveAddress,data,2,0); |
surajdagar | 0:2bfa06d10e28 | 45 | } |
surajdagar | 0:2bfa06d10e28 | 46 | |
surajdagar | 0:2bfa06d10e28 | 47 | |
surajdagar | 0:2bfa06d10e28 | 48 | //jmf end |
surajdagar | 0:2bfa06d10e28 | 49 | // ------------------------------------------------------------------------------ |
surajdagar | 0:2bfa06d10e28 | 50 | |
surajdagar | 0:2bfa06d10e28 | 51 | HTS221::HTS221(void) : _address(HTS221_ADDRESS) { |
surajdagar | 0:2bfa06d10e28 | 52 | _temperature = 0; |
surajdagar | 0:2bfa06d10e28 | 53 | _humidity = 0; |
surajdagar | 0:2bfa06d10e28 | 54 | } |
surajdagar | 0:2bfa06d10e28 | 55 | |
surajdagar | 0:2bfa06d10e28 | 56 | |
surajdagar | 0:2bfa06d10e28 | 57 | int HTS221::init(void) { |
surajdagar | 0:2bfa06d10e28 | 58 | uint8_t data; |
surajdagar | 0:2bfa06d10e28 | 59 | |
surajdagar | 0:2bfa06d10e28 | 60 | data = readRegister(_address, WHO_AM_I); |
surajdagar | 0:2bfa06d10e28 | 61 | if (data == WHO_AM_I_RETURN){ |
surajdagar | 0:2bfa06d10e28 | 62 | if (activate()){ |
surajdagar | 0:2bfa06d10e28 | 63 | storeCalibration(); |
surajdagar | 0:2bfa06d10e28 | 64 | return data; |
surajdagar | 0:2bfa06d10e28 | 65 | } |
surajdagar | 0:2bfa06d10e28 | 66 | } |
surajdagar | 0:2bfa06d10e28 | 67 | |
surajdagar | 0:2bfa06d10e28 | 68 | return 0; |
surajdagar | 0:2bfa06d10e28 | 69 | } |
surajdagar | 0:2bfa06d10e28 | 70 | |
surajdagar | 0:2bfa06d10e28 | 71 | int HTS221::storeCalibration(void) { |
surajdagar | 0:2bfa06d10e28 | 72 | uint8_t data; |
surajdagar | 0:2bfa06d10e28 | 73 | uint16_t tmp; |
surajdagar | 0:2bfa06d10e28 | 74 | |
surajdagar | 0:2bfa06d10e28 | 75 | for (int reg=CALIB_START; reg<=CALIB_END; reg++) { |
surajdagar | 0:2bfa06d10e28 | 76 | if ((reg!=CALIB_START+8) && (reg!=CALIB_START+9) && (reg!=CALIB_START+4)) { |
surajdagar | 0:2bfa06d10e28 | 77 | |
surajdagar | 0:2bfa06d10e28 | 78 | data = readRegister(HTS221_ADDRESS, reg); |
surajdagar | 0:2bfa06d10e28 | 79 | |
surajdagar | 0:2bfa06d10e28 | 80 | switch (reg) { |
surajdagar | 0:2bfa06d10e28 | 81 | case CALIB_START: |
surajdagar | 0:2bfa06d10e28 | 82 | _h0_rH = data; |
surajdagar | 0:2bfa06d10e28 | 83 | break; |
surajdagar | 0:2bfa06d10e28 | 84 | case CALIB_START+1: |
surajdagar | 0:2bfa06d10e28 | 85 | _h1_rH = data; |
surajdagar | 0:2bfa06d10e28 | 86 | break; |
surajdagar | 0:2bfa06d10e28 | 87 | case CALIB_START+2: |
surajdagar | 0:2bfa06d10e28 | 88 | _T0_degC = data; |
surajdagar | 0:2bfa06d10e28 | 89 | break; |
surajdagar | 0:2bfa06d10e28 | 90 | case CALIB_START+3: |
surajdagar | 0:2bfa06d10e28 | 91 | _T1_degC = data; |
surajdagar | 0:2bfa06d10e28 | 92 | break; |
surajdagar | 0:2bfa06d10e28 | 93 | |
surajdagar | 0:2bfa06d10e28 | 94 | case CALIB_START+5: |
surajdagar | 0:2bfa06d10e28 | 95 | tmp = _T0_degC; |
surajdagar | 0:2bfa06d10e28 | 96 | _T0_degC = (data&0x3)<<8; |
surajdagar | 0:2bfa06d10e28 | 97 | _T0_degC |= tmp; |
surajdagar | 0:2bfa06d10e28 | 98 | |
surajdagar | 0:2bfa06d10e28 | 99 | tmp = _T1_degC; |
surajdagar | 0:2bfa06d10e28 | 100 | _T1_degC = ((data&0xC)>>2)<<8; |
surajdagar | 0:2bfa06d10e28 | 101 | _T1_degC |= tmp; |
surajdagar | 0:2bfa06d10e28 | 102 | break; |
surajdagar | 0:2bfa06d10e28 | 103 | case CALIB_START+6: |
surajdagar | 0:2bfa06d10e28 | 104 | _H0_T0 = data; |
surajdagar | 0:2bfa06d10e28 | 105 | break; |
surajdagar | 0:2bfa06d10e28 | 106 | case CALIB_START+7: |
surajdagar | 0:2bfa06d10e28 | 107 | _H0_T0 |= data<<8; |
surajdagar | 0:2bfa06d10e28 | 108 | break; |
surajdagar | 0:2bfa06d10e28 | 109 | case CALIB_START+0xA: |
surajdagar | 0:2bfa06d10e28 | 110 | _H1_T0 = data; |
surajdagar | 0:2bfa06d10e28 | 111 | break; |
surajdagar | 0:2bfa06d10e28 | 112 | case CALIB_START+0xB: |
surajdagar | 0:2bfa06d10e28 | 113 | _H1_T0 |= data <<8; |
surajdagar | 0:2bfa06d10e28 | 114 | break; |
surajdagar | 0:2bfa06d10e28 | 115 | case CALIB_START+0xC: |
surajdagar | 0:2bfa06d10e28 | 116 | _T0_OUT = data; |
surajdagar | 0:2bfa06d10e28 | 117 | break; |
surajdagar | 0:2bfa06d10e28 | 118 | case CALIB_START+0xD: |
surajdagar | 0:2bfa06d10e28 | 119 | _T0_OUT |= data << 8; |
surajdagar | 0:2bfa06d10e28 | 120 | break; |
surajdagar | 0:2bfa06d10e28 | 121 | case CALIB_START+0xE: |
surajdagar | 0:2bfa06d10e28 | 122 | _T1_OUT = data; |
surajdagar | 0:2bfa06d10e28 | 123 | break; |
surajdagar | 0:2bfa06d10e28 | 124 | case CALIB_START+0xF: |
surajdagar | 0:2bfa06d10e28 | 125 | _T1_OUT |= data << 8; |
surajdagar | 0:2bfa06d10e28 | 126 | break; |
surajdagar | 0:2bfa06d10e28 | 127 | |
surajdagar | 0:2bfa06d10e28 | 128 | |
surajdagar | 0:2bfa06d10e28 | 129 | case CALIB_START+8: |
surajdagar | 0:2bfa06d10e28 | 130 | case CALIB_START+9: |
surajdagar | 0:2bfa06d10e28 | 131 | case CALIB_START+4: |
surajdagar | 0:2bfa06d10e28 | 132 | //DO NOTHING |
surajdagar | 0:2bfa06d10e28 | 133 | break; |
surajdagar | 0:2bfa06d10e28 | 134 | |
surajdagar | 0:2bfa06d10e28 | 135 | // to cover any possible error |
surajdagar | 0:2bfa06d10e28 | 136 | default: |
surajdagar | 0:2bfa06d10e28 | 137 | return false; |
surajdagar | 0:2bfa06d10e28 | 138 | } /* switch */ |
surajdagar | 0:2bfa06d10e28 | 139 | } /* if */ |
surajdagar | 0:2bfa06d10e28 | 140 | } /* for */ |
surajdagar | 0:2bfa06d10e28 | 141 | return true; |
surajdagar | 0:2bfa06d10e28 | 142 | } |
surajdagar | 0:2bfa06d10e28 | 143 | |
surajdagar | 0:2bfa06d10e28 | 144 | |
surajdagar | 0:2bfa06d10e28 | 145 | int HTS221::activate(void) { |
surajdagar | 0:2bfa06d10e28 | 146 | uint8_t data; |
surajdagar | 0:2bfa06d10e28 | 147 | |
surajdagar | 0:2bfa06d10e28 | 148 | data = readRegister(_address, CTRL_REG1); |
surajdagar | 0:2bfa06d10e28 | 149 | data |= POWER_UP; |
surajdagar | 0:2bfa06d10e28 | 150 | data |= ODR0_SET; |
surajdagar | 0:2bfa06d10e28 | 151 | writeRegister(_address, CTRL_REG1, data); |
surajdagar | 0:2bfa06d10e28 | 152 | |
surajdagar | 0:2bfa06d10e28 | 153 | return true; |
surajdagar | 0:2bfa06d10e28 | 154 | } |
surajdagar | 0:2bfa06d10e28 | 155 | |
surajdagar | 0:2bfa06d10e28 | 156 | |
surajdagar | 0:2bfa06d10e28 | 157 | int HTS221::deactivate(void) { |
surajdagar | 0:2bfa06d10e28 | 158 | uint8_t data; |
surajdagar | 0:2bfa06d10e28 | 159 | |
surajdagar | 0:2bfa06d10e28 | 160 | data = readRegister(_address, CTRL_REG1); |
surajdagar | 0:2bfa06d10e28 | 161 | data &= ~POWER_UP; |
surajdagar | 0:2bfa06d10e28 | 162 | writeRegister(_address, CTRL_REG1, data); |
surajdagar | 0:2bfa06d10e28 | 163 | return true; |
surajdagar | 0:2bfa06d10e28 | 164 | } |
surajdagar | 0:2bfa06d10e28 | 165 | |
surajdagar | 0:2bfa06d10e28 | 166 | |
surajdagar | 0:2bfa06d10e28 | 167 | int HTS221::bduActivate(void) { |
surajdagar | 0:2bfa06d10e28 | 168 | uint8_t data; |
surajdagar | 0:2bfa06d10e28 | 169 | |
surajdagar | 0:2bfa06d10e28 | 170 | data = readRegister(_address, CTRL_REG1); |
surajdagar | 0:2bfa06d10e28 | 171 | data |= BDU_SET; |
surajdagar | 0:2bfa06d10e28 | 172 | writeRegister(_address, CTRL_REG1, data); |
surajdagar | 0:2bfa06d10e28 | 173 | |
surajdagar | 0:2bfa06d10e28 | 174 | return true; |
surajdagar | 0:2bfa06d10e28 | 175 | } |
surajdagar | 0:2bfa06d10e28 | 176 | |
surajdagar | 0:2bfa06d10e28 | 177 | |
surajdagar | 0:2bfa06d10e28 | 178 | int HTS221::bduDeactivate(void) { |
surajdagar | 0:2bfa06d10e28 | 179 | uint8_t data; |
surajdagar | 0:2bfa06d10e28 | 180 | |
surajdagar | 0:2bfa06d10e28 | 181 | data = readRegister(_address, CTRL_REG1); |
surajdagar | 0:2bfa06d10e28 | 182 | data &= ~BDU_SET; |
surajdagar | 0:2bfa06d10e28 | 183 | writeRegister(_address, CTRL_REG1, data); |
surajdagar | 0:2bfa06d10e28 | 184 | return true; |
surajdagar | 0:2bfa06d10e28 | 185 | } |
surajdagar | 0:2bfa06d10e28 | 186 | |
surajdagar | 0:2bfa06d10e28 | 187 | |
surajdagar | 0:2bfa06d10e28 | 188 | int HTS221::readHumidity(void) { |
surajdagar | 0:2bfa06d10e28 | 189 | uint8_t data = 0; |
surajdagar | 0:2bfa06d10e28 | 190 | uint16_t h_out = 0; |
surajdagar | 0:2bfa06d10e28 | 191 | double h_temp = 0.0; |
surajdagar | 0:2bfa06d10e28 | 192 | double hum = 0.0; |
surajdagar | 0:2bfa06d10e28 | 193 | |
surajdagar | 0:2bfa06d10e28 | 194 | data = readRegister(_address, STATUS_REG); |
surajdagar | 0:2bfa06d10e28 | 195 | |
surajdagar | 0:2bfa06d10e28 | 196 | if (data & HUMIDITY_READY) { |
surajdagar | 0:2bfa06d10e28 | 197 | data = readRegister(_address, HUMIDITY_H_REG); |
surajdagar | 0:2bfa06d10e28 | 198 | h_out = data << 8; // MSB |
surajdagar | 0:2bfa06d10e28 | 199 | data = readRegister(_address, HUMIDITY_L_REG); |
surajdagar | 0:2bfa06d10e28 | 200 | h_out |= data; // LSB |
surajdagar | 0:2bfa06d10e28 | 201 | |
surajdagar | 0:2bfa06d10e28 | 202 | // Decode Humidity |
surajdagar | 0:2bfa06d10e28 | 203 | hum = ((int16_t)(_h1_rH) - (int16_t)(_h0_rH))/2.0; // remove x2 multiple |
surajdagar | 0:2bfa06d10e28 | 204 | |
surajdagar | 0:2bfa06d10e28 | 205 | // Calculate humidity in decimal of grade centigrades i.e. 15.0 = 150. |
surajdagar | 0:2bfa06d10e28 | 206 | h_temp = (((int16_t)h_out - (int16_t)_H0_T0) * hum) / ((int16_t)_H1_T0 - (int16_t)_H0_T0); |
surajdagar | 0:2bfa06d10e28 | 207 | hum = ((int16_t)_h0_rH) / 2.0; // remove x2 multiple |
surajdagar | 0:2bfa06d10e28 | 208 | _humidity = (int16_t)((hum + h_temp)); // provide signed % measurement unit |
surajdagar | 0:2bfa06d10e28 | 209 | } |
surajdagar | 0:2bfa06d10e28 | 210 | return _humidity; |
surajdagar | 0:2bfa06d10e28 | 211 | } |
surajdagar | 0:2bfa06d10e28 | 212 | |
surajdagar | 0:2bfa06d10e28 | 213 | |
surajdagar | 0:2bfa06d10e28 | 214 | |
surajdagar | 0:2bfa06d10e28 | 215 | double HTS221::readTemperature(void) { |
surajdagar | 0:2bfa06d10e28 | 216 | uint8_t data = 0; |
surajdagar | 0:2bfa06d10e28 | 217 | uint16_t t_out = 0; |
surajdagar | 0:2bfa06d10e28 | 218 | double t_temp = 0.0; |
surajdagar | 0:2bfa06d10e28 | 219 | double deg = 0.0; |
surajdagar | 0:2bfa06d10e28 | 220 | |
surajdagar | 0:2bfa06d10e28 | 221 | data = readRegister(_address, STATUS_REG); |
surajdagar | 0:2bfa06d10e28 | 222 | |
surajdagar | 0:2bfa06d10e28 | 223 | if (data & TEMPERATURE_READY) { |
surajdagar | 0:2bfa06d10e28 | 224 | |
surajdagar | 0:2bfa06d10e28 | 225 | data= readRegister(_address, TEMP_H_REG); |
surajdagar | 0:2bfa06d10e28 | 226 | t_out = data << 8; // MSB |
surajdagar | 0:2bfa06d10e28 | 227 | data = readRegister(_address, TEMP_L_REG); |
surajdagar | 0:2bfa06d10e28 | 228 | t_out |= data; // LSB |
surajdagar | 0:2bfa06d10e28 | 229 | |
surajdagar | 0:2bfa06d10e28 | 230 | // Decode Temperature |
surajdagar | 0:2bfa06d10e28 | 231 | deg = ((int16_t)(_T1_degC) - (int16_t)(_T0_degC))/8.0; // remove x8 multiple |
surajdagar | 0:2bfa06d10e28 | 232 | |
surajdagar | 0:2bfa06d10e28 | 233 | // Calculate Temperature in decimal of grade centigrades i.e. 15.0 = 150. |
surajdagar | 0:2bfa06d10e28 | 234 | t_temp = (((int16_t)t_out - (int16_t)_T0_OUT) * deg) / ((int16_t)_T1_OUT - (int16_t)_T0_OUT); |
surajdagar | 0:2bfa06d10e28 | 235 | deg = ((int16_t)_T0_degC) / 8.0; // remove x8 multiple |
surajdagar | 0:2bfa06d10e28 | 236 | _temperature = deg + t_temp; // provide signed celsius measurement unit |
surajdagar | 0:2bfa06d10e28 | 237 | } |
surajdagar | 0:2bfa06d10e28 | 238 | |
surajdagar | 0:2bfa06d10e28 | 239 | return _temperature; |
surajdagar | 0:2bfa06d10e28 | 240 | } |