![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
V.06 11/3
Dependencies: FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts
Fork of ATT_AWS_IoT_demo by
Sensors/Temperature/hts221_driver.cpp@29:f71a0be59b99, 2017-11-03 (annotated)
- Committer:
- jilee
- Date:
- Fri Nov 03 20:28:02 2017 +0000
- Revision:
- 29:f71a0be59b99
- Parent:
- 20:ee34856ae510
v.06 11/03/2016
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ampembeng | 20:ee34856ae510 | 1 | |
ampembeng | 20:ee34856ae510 | 2 | #include "HTS221.h" |
ampembeng | 20:ee34856ae510 | 3 | |
ampembeng | 20:ee34856ae510 | 4 | |
ampembeng | 20:ee34856ae510 | 5 | // ------------------------------------------------------------------------------ |
ampembeng | 20:ee34856ae510 | 6 | //jmf -- define I2C pins and functions to read & write to I2C device |
ampembeng | 20:ee34856ae510 | 7 | |
ampembeng | 20:ee34856ae510 | 8 | #include <string> |
ampembeng | 20:ee34856ae510 | 9 | #include "mbed.h" |
ampembeng | 20:ee34856ae510 | 10 | |
ampembeng | 20:ee34856ae510 | 11 | #include "hardware.h" |
ampembeng | 20:ee34856ae510 | 12 | //I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used. Defined in a |
ampembeng | 20:ee34856ae510 | 13 | //common locatioin since sensors also use I2C |
ampembeng | 20:ee34856ae510 | 14 | |
ampembeng | 20:ee34856ae510 | 15 | // Read a single unsigned char from addressToRead and return it as a unsigned char |
ampembeng | 20:ee34856ae510 | 16 | unsigned char HTS221::readRegister(unsigned char slaveAddress, unsigned char ToRead) |
ampembeng | 20:ee34856ae510 | 17 | { |
ampembeng | 20:ee34856ae510 | 18 | char data = ToRead; |
ampembeng | 20:ee34856ae510 | 19 | |
ampembeng | 20:ee34856ae510 | 20 | i2c.write(slaveAddress, &data, 1, 1); |
ampembeng | 20:ee34856ae510 | 21 | i2c.read(slaveAddress, &data, 1, 0); |
ampembeng | 20:ee34856ae510 | 22 | return data; |
ampembeng | 20:ee34856ae510 | 23 | } |
ampembeng | 20:ee34856ae510 | 24 | |
ampembeng | 20:ee34856ae510 | 25 | // Writes a single unsigned char (dataToWrite) into regToWrite |
ampembeng | 20:ee34856ae510 | 26 | int HTS221::writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite) |
ampembeng | 20:ee34856ae510 | 27 | { |
ampembeng | 20:ee34856ae510 | 28 | const char data[] = {regToWrite, dataToWrite}; |
ampembeng | 20:ee34856ae510 | 29 | |
ampembeng | 20:ee34856ae510 | 30 | return i2c.write(slaveAddress,data,2,0); |
ampembeng | 20:ee34856ae510 | 31 | } |
ampembeng | 20:ee34856ae510 | 32 | |
ampembeng | 20:ee34856ae510 | 33 | |
ampembeng | 20:ee34856ae510 | 34 | //jmf end |
ampembeng | 20:ee34856ae510 | 35 | // ------------------------------------------------------------------------------ |
ampembeng | 20:ee34856ae510 | 36 | |
ampembeng | 20:ee34856ae510 | 37 | //static inline int humidityReady(uint8_t data) { |
ampembeng | 20:ee34856ae510 | 38 | // return (data & 0x02); |
ampembeng | 20:ee34856ae510 | 39 | //} |
ampembeng | 20:ee34856ae510 | 40 | //static inline int temperatureReady(uint8_t data) { |
ampembeng | 20:ee34856ae510 | 41 | // return (data & 0x01); |
ampembeng | 20:ee34856ae510 | 42 | //} |
ampembeng | 20:ee34856ae510 | 43 | |
ampembeng | 20:ee34856ae510 | 44 | |
ampembeng | 20:ee34856ae510 | 45 | HTS221::HTS221(void) : _address(HTS221_ADDRESS) |
ampembeng | 20:ee34856ae510 | 46 | { |
ampembeng | 20:ee34856ae510 | 47 | _temperature = 0; |
ampembeng | 20:ee34856ae510 | 48 | _humidity = 0; |
ampembeng | 20:ee34856ae510 | 49 | } |
ampembeng | 20:ee34856ae510 | 50 | |
ampembeng | 20:ee34856ae510 | 51 | |
ampembeng | 20:ee34856ae510 | 52 | int HTS221::begin(void) |
ampembeng | 20:ee34856ae510 | 53 | { |
ampembeng | 20:ee34856ae510 | 54 | uint8_t data; |
ampembeng | 20:ee34856ae510 | 55 | |
ampembeng | 20:ee34856ae510 | 56 | data = readRegister(_address, WHO_AM_I); |
ampembeng | 20:ee34856ae510 | 57 | if (data == WHO_AM_I_RETURN){ |
ampembeng | 20:ee34856ae510 | 58 | if (activate()){ |
ampembeng | 20:ee34856ae510 | 59 | storeCalibration(); |
ampembeng | 20:ee34856ae510 | 60 | return data; |
ampembeng | 20:ee34856ae510 | 61 | } |
ampembeng | 20:ee34856ae510 | 62 | } |
ampembeng | 20:ee34856ae510 | 63 | |
ampembeng | 20:ee34856ae510 | 64 | return 0; |
ampembeng | 20:ee34856ae510 | 65 | } |
ampembeng | 20:ee34856ae510 | 66 | |
ampembeng | 20:ee34856ae510 | 67 | int |
ampembeng | 20:ee34856ae510 | 68 | HTS221::storeCalibration(void) |
ampembeng | 20:ee34856ae510 | 69 | { |
ampembeng | 20:ee34856ae510 | 70 | uint8_t data; |
ampembeng | 20:ee34856ae510 | 71 | uint16_t tmp; |
ampembeng | 20:ee34856ae510 | 72 | |
ampembeng | 20:ee34856ae510 | 73 | for (int reg=CALIB_START; reg<=CALIB_END; reg++) { |
ampembeng | 20:ee34856ae510 | 74 | if ((reg!=CALIB_START+8) && (reg!=CALIB_START+9) && (reg!=CALIB_START+4)) { |
ampembeng | 20:ee34856ae510 | 75 | |
ampembeng | 20:ee34856ae510 | 76 | data = readRegister(HTS221_ADDRESS, reg); |
ampembeng | 20:ee34856ae510 | 77 | |
ampembeng | 20:ee34856ae510 | 78 | switch (reg) { |
ampembeng | 20:ee34856ae510 | 79 | case CALIB_START: |
ampembeng | 20:ee34856ae510 | 80 | _h0_rH = data; |
ampembeng | 20:ee34856ae510 | 81 | break; |
ampembeng | 20:ee34856ae510 | 82 | case CALIB_START+1: |
ampembeng | 20:ee34856ae510 | 83 | _h1_rH = data; |
ampembeng | 20:ee34856ae510 | 84 | break; |
ampembeng | 20:ee34856ae510 | 85 | case CALIB_START+2: |
ampembeng | 20:ee34856ae510 | 86 | _T0_degC = data; |
ampembeng | 20:ee34856ae510 | 87 | break; |
ampembeng | 20:ee34856ae510 | 88 | case CALIB_START+3: |
ampembeng | 20:ee34856ae510 | 89 | _T1_degC = data; |
ampembeng | 20:ee34856ae510 | 90 | break; |
ampembeng | 20:ee34856ae510 | 91 | |
ampembeng | 20:ee34856ae510 | 92 | case CALIB_START+5: |
ampembeng | 20:ee34856ae510 | 93 | tmp = _T0_degC; |
ampembeng | 20:ee34856ae510 | 94 | _T0_degC = (data&0x3)<<8; |
ampembeng | 20:ee34856ae510 | 95 | _T0_degC |= tmp; |
ampembeng | 20:ee34856ae510 | 96 | |
ampembeng | 20:ee34856ae510 | 97 | tmp = _T1_degC; |
ampembeng | 20:ee34856ae510 | 98 | _T1_degC = ((data&0xC)>>2)<<8; |
ampembeng | 20:ee34856ae510 | 99 | _T1_degC |= tmp; |
ampembeng | 20:ee34856ae510 | 100 | break; |
ampembeng | 20:ee34856ae510 | 101 | case CALIB_START+6: |
ampembeng | 20:ee34856ae510 | 102 | _H0_T0 = data; |
ampembeng | 20:ee34856ae510 | 103 | break; |
ampembeng | 20:ee34856ae510 | 104 | case CALIB_START+7: |
ampembeng | 20:ee34856ae510 | 105 | _H0_T0 |= data<<8; |
ampembeng | 20:ee34856ae510 | 106 | break; |
ampembeng | 20:ee34856ae510 | 107 | case CALIB_START+0xA: |
ampembeng | 20:ee34856ae510 | 108 | _H1_T0 = data; |
ampembeng | 20:ee34856ae510 | 109 | break; |
ampembeng | 20:ee34856ae510 | 110 | case CALIB_START+0xB: |
ampembeng | 20:ee34856ae510 | 111 | _H1_T0 |= data <<8; |
ampembeng | 20:ee34856ae510 | 112 | break; |
ampembeng | 20:ee34856ae510 | 113 | case CALIB_START+0xC: |
ampembeng | 20:ee34856ae510 | 114 | _T0_OUT = data; |
ampembeng | 20:ee34856ae510 | 115 | break; |
ampembeng | 20:ee34856ae510 | 116 | case CALIB_START+0xD: |
ampembeng | 20:ee34856ae510 | 117 | _T0_OUT |= data << 8; |
ampembeng | 20:ee34856ae510 | 118 | break; |
ampembeng | 20:ee34856ae510 | 119 | case CALIB_START+0xE: |
ampembeng | 20:ee34856ae510 | 120 | _T1_OUT = data; |
ampembeng | 20:ee34856ae510 | 121 | break; |
ampembeng | 20:ee34856ae510 | 122 | case CALIB_START+0xF: |
ampembeng | 20:ee34856ae510 | 123 | _T1_OUT |= data << 8; |
ampembeng | 20:ee34856ae510 | 124 | break; |
ampembeng | 20:ee34856ae510 | 125 | |
ampembeng | 20:ee34856ae510 | 126 | |
ampembeng | 20:ee34856ae510 | 127 | case CALIB_START+8: |
ampembeng | 20:ee34856ae510 | 128 | case CALIB_START+9: |
ampembeng | 20:ee34856ae510 | 129 | case CALIB_START+4: |
ampembeng | 20:ee34856ae510 | 130 | //DO NOTHING |
ampembeng | 20:ee34856ae510 | 131 | break; |
ampembeng | 20:ee34856ae510 | 132 | |
ampembeng | 20:ee34856ae510 | 133 | // to cover any possible error |
ampembeng | 20:ee34856ae510 | 134 | default: |
ampembeng | 20:ee34856ae510 | 135 | return false; |
ampembeng | 20:ee34856ae510 | 136 | } /* switch */ |
ampembeng | 20:ee34856ae510 | 137 | } /* if */ |
ampembeng | 20:ee34856ae510 | 138 | } /* for */ |
ampembeng | 20:ee34856ae510 | 139 | return true; |
ampembeng | 20:ee34856ae510 | 140 | } |
ampembeng | 20:ee34856ae510 | 141 | |
ampembeng | 20:ee34856ae510 | 142 | |
ampembeng | 20:ee34856ae510 | 143 | int |
ampembeng | 20:ee34856ae510 | 144 | HTS221::activate(void) |
ampembeng | 20:ee34856ae510 | 145 | { |
ampembeng | 20:ee34856ae510 | 146 | uint8_t data; |
ampembeng | 20:ee34856ae510 | 147 | |
ampembeng | 20:ee34856ae510 | 148 | data = readRegister(_address, CTRL_REG1); |
ampembeng | 20:ee34856ae510 | 149 | data |= POWER_UP; |
ampembeng | 20:ee34856ae510 | 150 | data |= ODR0_SET; |
ampembeng | 20:ee34856ae510 | 151 | writeRegister(_address, CTRL_REG1, data); |
ampembeng | 20:ee34856ae510 | 152 | |
ampembeng | 20:ee34856ae510 | 153 | return true; |
ampembeng | 20:ee34856ae510 | 154 | } |
ampembeng | 20:ee34856ae510 | 155 | |
ampembeng | 20:ee34856ae510 | 156 | |
ampembeng | 20:ee34856ae510 | 157 | int HTS221::deactivate(void) |
ampembeng | 20:ee34856ae510 | 158 | { |
ampembeng | 20:ee34856ae510 | 159 | uint8_t data; |
ampembeng | 20:ee34856ae510 | 160 | |
ampembeng | 20:ee34856ae510 | 161 | data = readRegister(_address, CTRL_REG1); |
ampembeng | 20:ee34856ae510 | 162 | data &= ~POWER_UP; |
ampembeng | 20:ee34856ae510 | 163 | writeRegister(_address, CTRL_REG1, data); |
ampembeng | 20:ee34856ae510 | 164 | return true; |
ampembeng | 20:ee34856ae510 | 165 | } |
ampembeng | 20:ee34856ae510 | 166 | |
ampembeng | 20:ee34856ae510 | 167 | |
ampembeng | 20:ee34856ae510 | 168 | int |
ampembeng | 20:ee34856ae510 | 169 | HTS221::bduActivate(void) |
ampembeng | 20:ee34856ae510 | 170 | { |
ampembeng | 20:ee34856ae510 | 171 | uint8_t data; |
ampembeng | 20:ee34856ae510 | 172 | |
ampembeng | 20:ee34856ae510 | 173 | data = readRegister(_address, CTRL_REG1); |
ampembeng | 20:ee34856ae510 | 174 | data |= BDU_SET; |
ampembeng | 20:ee34856ae510 | 175 | writeRegister(_address, CTRL_REG1, data); |
ampembeng | 20:ee34856ae510 | 176 | |
ampembeng | 20:ee34856ae510 | 177 | return true; |
ampembeng | 20:ee34856ae510 | 178 | } |
ampembeng | 20:ee34856ae510 | 179 | |
ampembeng | 20:ee34856ae510 | 180 | |
ampembeng | 20:ee34856ae510 | 181 | int |
ampembeng | 20:ee34856ae510 | 182 | HTS221::bduDeactivate(void) |
ampembeng | 20:ee34856ae510 | 183 | { |
ampembeng | 20:ee34856ae510 | 184 | uint8_t data; |
ampembeng | 20:ee34856ae510 | 185 | |
ampembeng | 20:ee34856ae510 | 186 | data = readRegister(_address, CTRL_REG1); |
ampembeng | 20:ee34856ae510 | 187 | data &= ~BDU_SET; |
ampembeng | 20:ee34856ae510 | 188 | writeRegister(_address, CTRL_REG1, data); |
ampembeng | 20:ee34856ae510 | 189 | return true; |
ampembeng | 20:ee34856ae510 | 190 | } |
ampembeng | 20:ee34856ae510 | 191 | |
ampembeng | 20:ee34856ae510 | 192 | |
ampembeng | 20:ee34856ae510 | 193 | int |
ampembeng | 20:ee34856ae510 | 194 | HTS221::readHumidity(void) |
ampembeng | 20:ee34856ae510 | 195 | { |
ampembeng | 20:ee34856ae510 | 196 | uint8_t data = 0; |
ampembeng | 20:ee34856ae510 | 197 | uint16_t h_out = 0; |
ampembeng | 20:ee34856ae510 | 198 | double h_temp = 0.0; |
ampembeng | 20:ee34856ae510 | 199 | double hum = 0.0; |
ampembeng | 20:ee34856ae510 | 200 | |
ampembeng | 20:ee34856ae510 | 201 | data = readRegister(_address, STATUS_REG); |
ampembeng | 20:ee34856ae510 | 202 | |
ampembeng | 20:ee34856ae510 | 203 | if (data & HUMIDITY_READY) { |
ampembeng | 20:ee34856ae510 | 204 | data = readRegister(_address, HUMIDITY_H_REG); |
ampembeng | 20:ee34856ae510 | 205 | h_out = data << 8; // MSB |
ampembeng | 20:ee34856ae510 | 206 | data = readRegister(_address, HUMIDITY_L_REG); |
ampembeng | 20:ee34856ae510 | 207 | h_out |= data; // LSB |
ampembeng | 20:ee34856ae510 | 208 | |
ampembeng | 20:ee34856ae510 | 209 | // Decode Humidity |
ampembeng | 20:ee34856ae510 | 210 | hum = ((int16_t)(_h1_rH) - (int16_t)(_h0_rH))/2.0; // remove x2 multiple |
ampembeng | 20:ee34856ae510 | 211 | |
ampembeng | 20:ee34856ae510 | 212 | // Calculate humidity in decimal of grade centigrades i.e. 15.0 = 150. |
ampembeng | 20:ee34856ae510 | 213 | h_temp = (((int16_t)h_out - (int16_t)_H0_T0) * hum) / ((int16_t)_H1_T0 - (int16_t)_H0_T0); |
ampembeng | 20:ee34856ae510 | 214 | hum = ((int16_t)_h0_rH) / 2.0; // remove x2 multiple |
ampembeng | 20:ee34856ae510 | 215 | _humidity = (int16_t)((hum + h_temp)); // provide signed % measurement unit |
ampembeng | 20:ee34856ae510 | 216 | } |
ampembeng | 20:ee34856ae510 | 217 | return _humidity; |
ampembeng | 20:ee34856ae510 | 218 | } |
ampembeng | 20:ee34856ae510 | 219 | |
ampembeng | 20:ee34856ae510 | 220 | |
ampembeng | 20:ee34856ae510 | 221 | |
ampembeng | 20:ee34856ae510 | 222 | double |
ampembeng | 20:ee34856ae510 | 223 | HTS221::readTemperature(void) |
ampembeng | 20:ee34856ae510 | 224 | { |
ampembeng | 20:ee34856ae510 | 225 | uint8_t data = 0; |
ampembeng | 20:ee34856ae510 | 226 | uint16_t t_out = 0; |
ampembeng | 20:ee34856ae510 | 227 | double t_temp = 0.0; |
ampembeng | 20:ee34856ae510 | 228 | double deg = 0.0; |
ampembeng | 20:ee34856ae510 | 229 | |
ampembeng | 20:ee34856ae510 | 230 | data = readRegister(_address, STATUS_REG); |
ampembeng | 20:ee34856ae510 | 231 | |
ampembeng | 20:ee34856ae510 | 232 | if (data & TEMPERATURE_READY) { |
ampembeng | 20:ee34856ae510 | 233 | |
ampembeng | 20:ee34856ae510 | 234 | data= readRegister(_address, TEMP_H_REG); |
ampembeng | 20:ee34856ae510 | 235 | t_out = data << 8; // MSB |
ampembeng | 20:ee34856ae510 | 236 | data = readRegister(_address, TEMP_L_REG); |
ampembeng | 20:ee34856ae510 | 237 | t_out |= data; // LSB |
ampembeng | 20:ee34856ae510 | 238 | |
ampembeng | 20:ee34856ae510 | 239 | // Decode Temperature |
ampembeng | 20:ee34856ae510 | 240 | deg = ((int16_t)(_T1_degC) - (int16_t)(_T0_degC))/8.0; // remove x8 multiple |
ampembeng | 20:ee34856ae510 | 241 | |
ampembeng | 20:ee34856ae510 | 242 | // Calculate Temperature in decimal of grade centigrades i.e. 15.0 = 150. |
ampembeng | 20:ee34856ae510 | 243 | t_temp = (((int16_t)t_out - (int16_t)_T0_OUT) * deg) / ((int16_t)_T1_OUT - (int16_t)_T0_OUT); |
ampembeng | 20:ee34856ae510 | 244 | deg = ((int16_t)_T0_degC) / 8.0; // remove x8 multiple |
ampembeng | 20:ee34856ae510 | 245 | _temperature = deg + t_temp; // provide signed celsius measurement unit |
ampembeng | 20:ee34856ae510 | 246 | } |
ampembeng | 20:ee34856ae510 | 247 | |
ampembeng | 20:ee34856ae510 | 248 | return _temperature; |
ampembeng | 20:ee34856ae510 | 249 | } |
ampembeng | 20:ee34856ae510 | 250 |