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