corrected TH02 sample program
Dependencies: mbed
Fork of TH02_humidity_temp by
th02.cpp@1:8326614f3037, 2019-10-21 (annotated)
- Committer:
- superphil06
- Date:
- Mon Oct 21 09:10:15 2019 +0000
- Revision:
- 1:8326614f3037
- Parent:
- 0:f1951c6c9187
th02 sensor application program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
superphil06 | 0:f1951c6c9187 | 1 | // ********************************************************************************** |
superphil06 | 0:f1951c6c9187 | 2 | // Driver definition for HopeRF TH02 temperature and humidity sensor |
superphil06 | 0:f1951c6c9187 | 3 | // ********************************************************************************** |
superphil06 | 0:f1951c6c9187 | 4 | // Creative Commons Attrib Share-Alike License |
superphil06 | 0:f1951c6c9187 | 5 | // You are free to use/extend this library but please abide with the CC-BY-SA license: |
superphil06 | 0:f1951c6c9187 | 6 | // http://creativecommons.org/licenses/by-sa/4.0/ |
superphil06 | 0:f1951c6c9187 | 7 | // |
superphil06 | 0:f1951c6c9187 | 8 | // For any explanation see TH02 sensor information at |
superphil06 | 0:f1951c6c9187 | 9 | // http://www.hoperf.com/sensor/app/TH02.htm |
superphil06 | 0:f1951c6c9187 | 10 | // |
superphil06 | 0:f1951c6c9187 | 11 | // Code based on following datasheet |
superphil06 | 1:8326614f3037 | 12 | // http://www.hoperf.com/upload/sensor/TH02_V1.1.pdf |
superphil06 | 0:f1951c6c9187 | 13 | // |
superphil06 | 0:f1951c6c9187 | 14 | // Written by Charles-Henri Hallard (http://hallard.me) |
superphil06 | 0:f1951c6c9187 | 15 | // |
superphil06 | 0:f1951c6c9187 | 16 | // History : V1.00 2014-07-14 - First release |
superphil06 | 0:f1951c6c9187 | 17 | // V1.10 2015-04-13 - changed to Wire library instead of m_I2C |
superphil06 | 0:f1951c6c9187 | 18 | // |
superphil06 | 0:f1951c6c9187 | 19 | // All text above must be included in any redistribution. |
superphil06 | 0:f1951c6c9187 | 20 | // |
superphil06 | 0:f1951c6c9187 | 21 | // ********************************************************************************** |
superphil06 | 0:f1951c6c9187 | 22 | #include "th02.h" |
superphil06 | 0:f1951c6c9187 | 23 | #include "mbed.h" |
superphil06 | 0:f1951c6c9187 | 24 | #include "math.h" |
superphil06 | 0:f1951c6c9187 | 25 | |
superphil06 | 0:f1951c6c9187 | 26 | // Class Constructor |
superphil06 | 0:f1951c6c9187 | 27 | |
superphil06 | 0:f1951c6c9187 | 28 | TH02::TH02(PinName sda,PinName scl,uint8_t address): m_I2C(sda, scl) |
superphil06 | 1:8326614f3037 | 29 | { |
superphil06 | 1:8326614f3037 | 30 | _address = address; // m_I2C Module Address |
superphil06 | 1:8326614f3037 | 31 | _last_temp = TH02_UNINITIALIZED_TEMP; // Last measured temperature (for linearization) |
superphil06 | 1:8326614f3037 | 32 | _last_rh = TH02_UNINITIALIZED_RH; // Last measured RH |
superphil06 | 1:8326614f3037 | 33 | //m_I2C.frequency(10000); //set 10khz i2c frequency |
superphil06 | 1:8326614f3037 | 34 | |
superphil06 | 0:f1951c6c9187 | 35 | } |
superphil06 | 0:f1951c6c9187 | 36 | |
superphil06 | 0:f1951c6c9187 | 37 | TH02::~TH02() |
superphil06 | 0:f1951c6c9187 | 38 | { |
superphil06 | 0:f1951c6c9187 | 39 | |
superphil06 | 0:f1951c6c9187 | 40 | } |
superphil06 | 0:f1951c6c9187 | 41 | |
superphil06 | 0:f1951c6c9187 | 42 | |
superphil06 | 0:f1951c6c9187 | 43 | |
superphil06 | 0:f1951c6c9187 | 44 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 45 | Function: writeCommand |
superphil06 | 1:8326614f3037 | 46 | Purpose : write the "register address" value on m_I2C bus |
superphil06 | 0:f1951c6c9187 | 47 | Input : register address |
superphil06 | 0:f1951c6c9187 | 48 | true if we need to release the bus after (default yes) |
superphil06 | 0:f1951c6c9187 | 49 | Output : Arduino Wire library return code (0 if ok) |
superphil06 | 1:8326614f3037 | 50 | Comments: |
superphil06 | 0:f1951c6c9187 | 51 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 52 | uint8_t TH02::writeCommand(uint8_t command, bool release) |
superphil06 | 1:8326614f3037 | 53 | { |
superphil06 | 1:8326614f3037 | 54 | int iError; |
superphil06 | 1:8326614f3037 | 55 | (void) m_I2C.start(); |
superphil06 | 1:8326614f3037 | 56 | //Wire.beginTransmission(_address); |
superphil06 | 1:8326614f3037 | 57 | iError=m_I2C.write(_address);// send adress of i2c slave |
superphil06 | 1:8326614f3037 | 58 | |
superphil06 | 1:8326614f3037 | 59 | if (iError==1) { // ack received |
superphil06 | 1:8326614f3037 | 60 | // Wire.write(command) ; |
superphil06 | 1:8326614f3037 | 61 | iError= m_I2C.write(command); |
superphil06 | 1:8326614f3037 | 62 | |
superphil06 | 1:8326614f3037 | 63 | |
superphil06 | 1:8326614f3037 | 64 | if (release==true) { |
superphil06 | 1:8326614f3037 | 65 | m_I2C.stop();// return stop error code |
superphil06 | 1:8326614f3037 | 66 | } |
superphil06 | 1:8326614f3037 | 67 | } |
superphil06 | 1:8326614f3037 | 68 | |
superphil06 | 1:8326614f3037 | 69 | if (iError==1) iError=0;// ack received |
superphil06 | 1:8326614f3037 | 70 | else iError=1;// no ack |
superphil06 | 1:8326614f3037 | 71 | return iError; |
superphil06 | 0:f1951c6c9187 | 72 | } |
superphil06 | 0:f1951c6c9187 | 73 | |
superphil06 | 0:f1951c6c9187 | 74 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 75 | Function: writeRegister |
superphil06 | 1:8326614f3037 | 76 | Purpose : write a value on the designed register address on m_I2C bus |
superphil06 | 0:f1951c6c9187 | 77 | Input : register address |
superphil06 | 0:f1951c6c9187 | 78 | value to write |
superphil06 | 0:f1951c6c9187 | 79 | Output : Arduino Wire library return code (0 if ok) |
superphil06 | 1:8326614f3037 | 80 | Comments: |
superphil06 | 0:f1951c6c9187 | 81 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 82 | uint8_t TH02::writeRegister(uint8_t reg, uint8_t value) |
superphil06 | 1:8326614f3037 | 83 | { |
superphil06 | 1:8326614f3037 | 84 | int iError; |
superphil06 | 1:8326614f3037 | 85 | |
superphil06 | 1:8326614f3037 | 86 | bool ret = false; |
superphil06 | 1:8326614f3037 | 87 | |
superphil06 | 1:8326614f3037 | 88 | //Wire.beginTransmission(_address); |
superphil06 | 1:8326614f3037 | 89 | (void) m_I2C.start(); |
superphil06 | 1:8326614f3037 | 90 | iError=m_I2C.write(_address);// send adress of i2c slave |
superphil06 | 1:8326614f3037 | 91 | // Wire.write(reg); |
superphil06 | 1:8326614f3037 | 92 | if (iError==1) { |
superphil06 | 0:f1951c6c9187 | 93 | |
superphil06 | 1:8326614f3037 | 94 | iError= m_I2C.write(reg); |
superphil06 | 1:8326614f3037 | 95 | |
superphil06 | 1:8326614f3037 | 96 | // Wire.write(value); |
superphil06 | 1:8326614f3037 | 97 | (void) m_I2C.write(value); |
superphil06 | 1:8326614f3037 | 98 | } |
superphil06 | 1:8326614f3037 | 99 | // return Wire.endTransmission(); |
superphil06 | 0:f1951c6c9187 | 100 | m_I2C.stop();// return stop error code |
superphil06 | 1:8326614f3037 | 101 | if (iError==1) iError=0;// ack received |
superphil06 | 1:8326614f3037 | 102 | else iError=1;// no ack |
superphil06 | 1:8326614f3037 | 103 | wait_ms(1); |
superphil06 | 1:8326614f3037 | 104 | return iError; |
superphil06 | 0:f1951c6c9187 | 105 | } |
superphil06 | 0:f1951c6c9187 | 106 | |
superphil06 | 0:f1951c6c9187 | 107 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 108 | Function: readRegister |
superphil06 | 1:8326614f3037 | 109 | Purpose : read a register address value on m_I2C bus |
superphil06 | 0:f1951c6c9187 | 110 | Input : register address |
superphil06 | 0:f1951c6c9187 | 111 | pointer where the return value will be filled |
superphil06 | 0:f1951c6c9187 | 112 | Output : Arduino Wire library return code (0 if ok) |
superphil06 | 1:8326614f3037 | 113 | Comments: |
superphil06 | 0:f1951c6c9187 | 114 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 115 | uint8_t TH02::readRegister(uint8_t reg, uint8_t * value) |
superphil06 | 0:f1951c6c9187 | 116 | { |
superphil06 | 1:8326614f3037 | 117 | uint8_t ret ; |
superphil06 | 1:8326614f3037 | 118 | int iAck,iRedVal,iError; |
superphil06 | 1:8326614f3037 | 119 | // Send a register reading command |
superphil06 | 1:8326614f3037 | 120 | // but DO NOT release the m_I2C bus |
superphil06 | 1:8326614f3037 | 121 | // (void) m_I2C.start(); |
superphil06 | 1:8326614f3037 | 122 | //iError=m_I2C.write(_address);// send adress of i2c slave |
superphil06 | 1:8326614f3037 | 123 | |
superphil06 | 1:8326614f3037 | 124 | //if (iError==1) // ack received |
superphil06 | 1:8326614f3037 | 125 | //{ |
superphil06 | 1:8326614f3037 | 126 | ret = writeCommand(reg, false);// no stop |
superphil06 | 0:f1951c6c9187 | 127 | |
superphil06 | 1:8326614f3037 | 128 | if ( ret == 0) { //if command ok |
superphil06 | 1:8326614f3037 | 129 | // Wire.requestFrom( (uint8_t) _address, (uint8_t) 1); |
superphil06 | 1:8326614f3037 | 130 | (void) m_I2C.start(); |
superphil06 | 1:8326614f3037 | 131 | iError=m_I2C.write(_address+0x01);// send adress of i2c slave in read mode |
superphil06 | 1:8326614f3037 | 132 | *value =m_I2C.read(0);//send non ack |
superphil06 | 1:8326614f3037 | 133 | // if (Wire.available() != 1) |
superphil06 | 1:8326614f3037 | 134 | /*if (iAck != 1) |
superphil06 | 1:8326614f3037 | 135 | |
superphil06 | 1:8326614f3037 | 136 | // Other error as Wire library |
superphil06 | 1:8326614f3037 | 137 | ret = 4; |
superphil06 | 1:8326614f3037 | 138 | else |
superphil06 | 1:8326614f3037 | 139 | // grab the value*/ |
superphil06 | 1:8326614f3037 | 140 | // *value = iRedVal; // return Red value by ref |
superphil06 | 1:8326614f3037 | 141 | |
superphil06 | 1:8326614f3037 | 142 | //} |
superphil06 | 1:8326614f3037 | 143 | |
superphil06 | 1:8326614f3037 | 144 | // Ok now we have finished |
superphil06 | 1:8326614f3037 | 145 | // Wire.endTransmission(); |
superphil06 | 1:8326614f3037 | 146 | |
superphil06 | 1:8326614f3037 | 147 | } |
superphil06 | 1:8326614f3037 | 148 | (void) m_I2C.stop();// return stop error code |
superphil06 | 1:8326614f3037 | 149 | wait_ms(1); |
superphil06 | 1:8326614f3037 | 150 | return ret; |
superphil06 | 1:8326614f3037 | 151 | } |
superphil06 | 0:f1951c6c9187 | 152 | |
superphil06 | 0:f1951c6c9187 | 153 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 154 | Function: getId |
superphil06 | 1:8326614f3037 | 155 | Purpose : Get device ID register |
superphil06 | 0:f1951c6c9187 | 156 | Input : pointer where the return value will be filled |
superphil06 | 0:f1951c6c9187 | 157 | Output : Arduino Wire library return code (0 if ok) |
superphil06 | 0:f1951c6c9187 | 158 | Comments: - |
superphil06 | 0:f1951c6c9187 | 159 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 160 | uint8_t TH02::getId(uint8_t * pvalue) |
superphil06 | 0:f1951c6c9187 | 161 | { |
superphil06 | 1:8326614f3037 | 162 | return (readRegister(TH02_ID, pvalue)); |
superphil06 | 0:f1951c6c9187 | 163 | } |
superphil06 | 0:f1951c6c9187 | 164 | |
superphil06 | 0:f1951c6c9187 | 165 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 166 | Function: getStatus |
superphil06 | 1:8326614f3037 | 167 | Purpose : Get device status register |
superphil06 | 0:f1951c6c9187 | 168 | Input : pointer where the return value will be filled |
superphil06 | 0:f1951c6c9187 | 169 | Output : Arduino Wire library return code (0 if ok) |
superphil06 | 1:8326614f3037 | 170 | Comments: |
superphil06 | 0:f1951c6c9187 | 171 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 172 | uint8_t TH02::getStatus(uint8_t * pvalue) |
superphil06 | 0:f1951c6c9187 | 173 | { |
superphil06 | 1:8326614f3037 | 174 | return (readRegister(TH02_STATUS, pvalue)); |
superphil06 | 0:f1951c6c9187 | 175 | } |
superphil06 | 0:f1951c6c9187 | 176 | |
superphil06 | 0:f1951c6c9187 | 177 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 178 | Function: isConverting |
superphil06 | 1:8326614f3037 | 179 | Purpose : Indicate if a temperature or humidity conversion is in progress |
superphil06 | 0:f1951c6c9187 | 180 | Input : - |
superphil06 | 0:f1951c6c9187 | 181 | Output : true if conversion in progress false otherwise |
superphil06 | 1:8326614f3037 | 182 | Comments: |
superphil06 | 0:f1951c6c9187 | 183 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 184 | bool TH02::isConverting(void) |
superphil06 | 0:f1951c6c9187 | 185 | { |
superphil06 | 1:8326614f3037 | 186 | uint8_t status; |
superphil06 | 1:8326614f3037 | 187 | // Get status and check RDY bit |
superphil06 | 1:8326614f3037 | 188 | if ( getStatus(&status) == 0) |
superphil06 | 1:8326614f3037 | 189 | |
superphil06 | 1:8326614f3037 | 190 | { |
superphil06 | 1:8326614f3037 | 191 | // printf("\n lecture status %x",status); |
superphil06 | 1:8326614f3037 | 192 | if ( (status & TH02_STATUS_RDY) ==1 ) |
superphil06 | 1:8326614f3037 | 193 | return true; |
superphil06 | 1:8326614f3037 | 194 | } |
superphil06 | 1:8326614f3037 | 195 | return false; |
superphil06 | 0:f1951c6c9187 | 196 | } |
superphil06 | 0:f1951c6c9187 | 197 | |
superphil06 | 0:f1951c6c9187 | 198 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 199 | Function: getConfig |
superphil06 | 1:8326614f3037 | 200 | Purpose : Get device configuration register |
superphil06 | 0:f1951c6c9187 | 201 | Input : pointer where the return value will be filled |
superphil06 | 0:f1951c6c9187 | 202 | Output : Arduino Wire library return code (0 if ok) |
superphil06 | 1:8326614f3037 | 203 | Comments: |
superphil06 | 0:f1951c6c9187 | 204 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 205 | uint8_t TH02::getConfig(uint8_t * pvalue) |
superphil06 | 0:f1951c6c9187 | 206 | { |
superphil06 | 1:8326614f3037 | 207 | return (readRegister(TH02_CONFIG, pvalue)); |
superphil06 | 0:f1951c6c9187 | 208 | } |
superphil06 | 0:f1951c6c9187 | 209 | |
superphil06 | 0:f1951c6c9187 | 210 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 211 | Function: setConfig |
superphil06 | 1:8326614f3037 | 212 | Purpose : Set device configuration register |
superphil06 | 0:f1951c6c9187 | 213 | Input : value to set |
superphil06 | 0:f1951c6c9187 | 214 | Output : true if succeded, false otherwise |
superphil06 | 1:8326614f3037 | 215 | Comments: |
superphil06 | 0:f1951c6c9187 | 216 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 217 | uint8_t TH02::setConfig(uint8_t config) |
superphil06 | 0:f1951c6c9187 | 218 | { |
superphil06 | 1:8326614f3037 | 219 | return (writeRegister(TH02_CONFIG, config)); |
superphil06 | 0:f1951c6c9187 | 220 | } |
superphil06 | 0:f1951c6c9187 | 221 | |
superphil06 | 0:f1951c6c9187 | 222 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 223 | Function: startTempConv |
superphil06 | 1:8326614f3037 | 224 | Purpose : Start a temperature conversion |
superphil06 | 0:f1951c6c9187 | 225 | Input : - fastmode true to enable fast conversion |
superphil06 | 0:f1951c6c9187 | 226 | - heater true to enable heater |
superphil06 | 0:f1951c6c9187 | 227 | Output : true if succeded, false otherwise |
superphil06 | 0:f1951c6c9187 | 228 | Comments: if heater enabled, it will not be auto disabled |
superphil06 | 0:f1951c6c9187 | 229 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 230 | uint8_t TH02::startTempConv(bool fastmode, bool heater) |
superphil06 | 0:f1951c6c9187 | 231 | { |
superphil06 | 1:8326614f3037 | 232 | // init configuration register to start and temperature |
superphil06 | 1:8326614f3037 | 233 | uint8_t config = TH02_CONFIG_START | TH02_CONFIG_TEMP; |
superphil06 | 1:8326614f3037 | 234 | |
superphil06 | 1:8326614f3037 | 235 | // set fast mode and heater if asked |
superphil06 | 1:8326614f3037 | 236 | if (fastmode) config |= TH02_CONFIG_FAST; |
superphil06 | 1:8326614f3037 | 237 | if (heater) config |= TH02_CONFIG_HEAT; |
superphil06 | 1:8326614f3037 | 238 | |
superphil06 | 1:8326614f3037 | 239 | // write to configuration register |
superphil06 | 1:8326614f3037 | 240 | return ( setConfig( config ) ); |
superphil06 | 0:f1951c6c9187 | 241 | } |
superphil06 | 0:f1951c6c9187 | 242 | |
superphil06 | 0:f1951c6c9187 | 243 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 244 | Function: startRHConv |
superphil06 | 1:8326614f3037 | 245 | Purpose : Start a relative humidity conversion |
superphil06 | 0:f1951c6c9187 | 246 | Input : - fastmode true to enable fast conversion |
superphil06 | 0:f1951c6c9187 | 247 | - heater true to enable heater |
superphil06 | 0:f1951c6c9187 | 248 | Output : true if succeded, false otherwise |
superphil06 | 0:f1951c6c9187 | 249 | Comments: if heater enabled, it will not be auto disabled |
superphil06 | 0:f1951c6c9187 | 250 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 251 | uint8_t TH02::startRHConv(bool fastmode, bool heater) |
superphil06 | 0:f1951c6c9187 | 252 | { |
superphil06 | 1:8326614f3037 | 253 | // init configuration register to start and no temperature (so RH) |
superphil06 | 1:8326614f3037 | 254 | uint8_t config = TH02_CONFIG_START; |
superphil06 | 1:8326614f3037 | 255 | |
superphil06 | 1:8326614f3037 | 256 | // set fast mode and heater if asked |
superphil06 | 1:8326614f3037 | 257 | if (fastmode) config |= TH02_CONFIG_FAST; |
superphil06 | 1:8326614f3037 | 258 | if (heater) config |= TH02_CONFIG_HEAT; |
superphil06 | 1:8326614f3037 | 259 | |
superphil06 | 1:8326614f3037 | 260 | // write to configuration register |
superphil06 | 1:8326614f3037 | 261 | return ( setConfig( config ) ); |
superphil06 | 0:f1951c6c9187 | 262 | } |
superphil06 | 0:f1951c6c9187 | 263 | |
superphil06 | 0:f1951c6c9187 | 264 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 265 | Function: waitEndConversion |
superphil06 | 1:8326614f3037 | 266 | Purpose : wait for a temperature or RH conversion is done |
superphil06 | 1:8326614f3037 | 267 | Input : |
superphil06 | 1:8326614f3037 | 268 | Output : delay in ms the process took. |
superphil06 | 0:f1951c6c9187 | 269 | Comments: if return >= TH02_CONVERSION_TIME_OUT, time out occured |
superphil06 | 0:f1951c6c9187 | 270 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 271 | uint8_t TH02::waitEndConversion(void) |
superphil06 | 0:f1951c6c9187 | 272 | { |
superphil06 | 1:8326614f3037 | 273 | // okay this is basic approach not so accurate |
superphil06 | 1:8326614f3037 | 274 | // but avoid using long and millis() |
superphil06 | 1:8326614f3037 | 275 | uint8_t time_out = 0; |
superphil06 | 1:8326614f3037 | 276 | |
superphil06 | 1:8326614f3037 | 277 | // loop until conversion done or duration >= time out |
superphil06 | 1:8326614f3037 | 278 | while (isConverting() && (time_out <= TH02_CONVERSION_TIME_OUT) ) { |
superphil06 | 1:8326614f3037 | 279 | ++time_out; |
superphil06 | 1:8326614f3037 | 280 | wait_ms(2); |
superphil06 | 1:8326614f3037 | 281 | } |
superphil06 | 1:8326614f3037 | 282 | |
superphil06 | 1:8326614f3037 | 283 | // return approx time of conversion |
superphil06 | 1:8326614f3037 | 284 | return (time_out); |
superphil06 | 0:f1951c6c9187 | 285 | } |
superphil06 | 0:f1951c6c9187 | 286 | |
superphil06 | 0:f1951c6c9187 | 287 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 288 | Function: roundInt |
superphil06 | 1:8326614f3037 | 289 | Purpose : round a float value to int |
superphil06 | 0:f1951c6c9187 | 290 | Input : float value |
superphil06 | 1:8326614f3037 | 291 | Output : int value rounded |
superphil06 | 1:8326614f3037 | 292 | Comments: |
superphil06 | 0:f1951c6c9187 | 293 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 294 | int16_t TH02::roundInt(float value) |
superphil06 | 1:8326614f3037 | 295 | { |
superphil06 | 0:f1951c6c9187 | 296 | |
superphil06 | 1:8326614f3037 | 297 | // check positive number and do round |
superphil06 | 1:8326614f3037 | 298 | if (value >= 0.0f) |
superphil06 | 1:8326614f3037 | 299 | value = floor(value + 0.5f); |
superphil06 | 1:8326614f3037 | 300 | else |
superphil06 | 1:8326614f3037 | 301 | value = ceil(value - 0.5f); |
superphil06 | 1:8326614f3037 | 302 | |
superphil06 | 1:8326614f3037 | 303 | // return int value |
superphil06 | 1:8326614f3037 | 304 | return (static_cast<int16_t>(value)); |
superphil06 | 0:f1951c6c9187 | 305 | } |
superphil06 | 0:f1951c6c9187 | 306 | |
superphil06 | 0:f1951c6c9187 | 307 | /* to avoid math library may I need to test something |
superphil06 | 0:f1951c6c9187 | 308 | like that |
superphil06 | 1:8326614f3037 | 309 | float TH02::showDecimals(float x, int numDecimals) |
superphil06 | 0:f1951c6c9187 | 310 | { |
superphil06 | 0:f1951c6c9187 | 311 | int y=x; |
superphil06 | 0:f1951c6c9187 | 312 | double z=x-y; |
superphil06 | 0:f1951c6c9187 | 313 | double m=pow(10,numDecimals); |
superphil06 | 0:f1951c6c9187 | 314 | double q=z*m; |
superphil06 | 0:f1951c6c9187 | 315 | double r=round(q); |
superphil06 | 0:f1951c6c9187 | 316 | return static_cast<double>(y)+(1.0/m)*r; |
superphil06 | 0:f1951c6c9187 | 317 | } |
superphil06 | 0:f1951c6c9187 | 318 | */ |
superphil06 | 0:f1951c6c9187 | 319 | |
superphil06 | 1:8326614f3037 | 320 | |
superphil06 | 0:f1951c6c9187 | 321 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 322 | Function: getConversionValue |
superphil06 | 1:8326614f3037 | 323 | Purpose : return the last converted value to int * 10 to have 1 digit prec. |
superphil06 | 0:f1951c6c9187 | 324 | Input : float value |
superphil06 | 0:f1951c6c9187 | 325 | Output : value rounded but multiplied per 10 or TH02_UNDEFINED_VALUE on err |
superphil06 | 0:f1951c6c9187 | 326 | Comments: - temperature and rh raw values (*100) are stored for raw purpose |
superphil06 | 0:f1951c6c9187 | 327 | - the configuration register is checked to see if last conv was |
superphil06 | 0:f1951c6c9187 | 328 | a temperature or humidity conversion |
superphil06 | 0:f1951c6c9187 | 329 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 330 | int16_t TH02::getConversionValue(void) |
superphil06 | 1:8326614f3037 | 331 | { |
superphil06 | 1:8326614f3037 | 332 | char cMaChaine[4]; |
superphil06 | 1:8326614f3037 | 333 | int iError; |
superphil06 | 1:8326614f3037 | 334 | int32_t result=0 ; |
superphil06 | 1:8326614f3037 | 335 | uint8_t config; |
superphil06 | 1:8326614f3037 | 336 | int16_t ret = TH02_UNDEFINED_VALUE; |
superphil06 | 1:8326614f3037 | 337 | |
superphil06 | 1:8326614f3037 | 338 | // Prepare reading address of ADC data result |
superphil06 | 1:8326614f3037 | 339 | /*if ( writeCommand(TH02_DATAh, false) == 0 ) // no stop |
superphil06 | 1:8326614f3037 | 340 | {*/ |
superphil06 | 0:f1951c6c9187 | 341 | // Read 2 bytes adc data result MSB and LSB from TH02 |
superphil06 | 0:f1951c6c9187 | 342 | //Wire.requestFrom( (uint8_t) _address, (uint8_t) 2); |
superphil06 | 1:8326614f3037 | 343 | writeCommand(TH02_DATAh, false); |
superphil06 | 0:f1951c6c9187 | 344 | |
superphil06 | 1:8326614f3037 | 345 | // read of two register |
superphil06 | 1:8326614f3037 | 346 | (void) m_I2C.start(); |
superphil06 | 1:8326614f3037 | 347 | iError=m_I2C.write(_address+1);// send adress of i2c slave read mode |
superphil06 | 1:8326614f3037 | 348 | if (iError==1) { |
superphil06 | 1:8326614f3037 | 349 | cMaChaine[0]= m_I2C.read(1);// read first byte with ack |
superphil06 | 1:8326614f3037 | 350 | cMaChaine[1]=m_I2C.read(0);// read first byte without ack |
superphil06 | 1:8326614f3037 | 351 | |
superphil06 | 1:8326614f3037 | 352 | m_I2C.stop();// rperform stop |
superphil06 | 1:8326614f3037 | 353 | |
superphil06 | 0:f1951c6c9187 | 354 | |
superphil06 | 0:f1951c6c9187 | 355 | |
superphil06 | 1:8326614f3037 | 356 | //iError= m_I2C.read (_address,cMaChaine,4,false);// send stop at end |
superphil06 | 1:8326614f3037 | 357 | // printf (" \n\r lecture I2C: %02x %02x",cMaChaine[0],cMaChaine[1]); |
superphil06 | 1:8326614f3037 | 358 | //} |
superphil06 | 1:8326614f3037 | 359 | result=(cMaChaine[0]<<8 )|cMaChaine[1]; |
superphil06 | 1:8326614f3037 | 360 | // Get configuration to know what was asked last time |
superphil06 | 1:8326614f3037 | 361 | |
superphil06 | 1:8326614f3037 | 362 | if (getConfig(&config)==0) { |
superphil06 | 1:8326614f3037 | 363 | // last conversion was temperature ? |
superphil06 | 1:8326614f3037 | 364 | if( config & TH02_CONFIG_TEMP) { |
superphil06 | 1:8326614f3037 | 365 | result >>= 2; // remove 2 unused LSB bits |
superphil06 | 1:8326614f3037 | 366 | result *= 100; // multiply per 100 to have int value with 2 decimal |
superphil06 | 1:8326614f3037 | 367 | result /= 32; // now apply datasheet formula |
superphil06 | 1:8326614f3037 | 368 | if(result >= 5000) { |
superphil06 | 1:8326614f3037 | 369 | result -= 5000; |
superphil06 | 1:8326614f3037 | 370 | } else { |
superphil06 | 1:8326614f3037 | 371 | result -= 5000; |
superphil06 | 1:8326614f3037 | 372 | result = -result; |
superphil06 | 1:8326614f3037 | 373 | } |
superphil06 | 1:8326614f3037 | 374 | |
superphil06 | 1:8326614f3037 | 375 | // now result contain temperature * 100 |
superphil06 | 1:8326614f3037 | 376 | // so 2134 is 21.34 C |
superphil06 | 1:8326614f3037 | 377 | |
superphil06 | 1:8326614f3037 | 378 | // Save raw value |
superphil06 | 1:8326614f3037 | 379 | _last_temp = result; |
superphil06 | 1:8326614f3037 | 380 | } |
superphil06 | 1:8326614f3037 | 381 | // it was RH conversion |
superphil06 | 1:8326614f3037 | 382 | else { |
superphil06 | 1:8326614f3037 | 383 | result >>= 4; // remove 4 unused LSB bits |
superphil06 | 1:8326614f3037 | 384 | result *= 100; // multiply per 100 to have int value with 2 decimal |
superphil06 | 1:8326614f3037 | 385 | result /= 16; // now apply datasheet formula |
superphil06 | 1:8326614f3037 | 386 | result -= 2400; |
superphil06 | 1:8326614f3037 | 387 | |
superphil06 | 1:8326614f3037 | 388 | // now result contain humidity * 100 |
superphil06 | 1:8326614f3037 | 389 | // so 4567 is 45.67 % RH |
superphil06 | 1:8326614f3037 | 390 | _last_rh = result; |
superphil06 | 1:8326614f3037 | 391 | } |
superphil06 | 1:8326614f3037 | 392 | |
superphil06 | 1:8326614f3037 | 393 | // remember result value is multiplied by 10 to avoid float calculation later |
superphil06 | 1:8326614f3037 | 394 | // so humidity of 45.6% is 456 and temp of 21.3 C is 213 |
superphil06 | 1:8326614f3037 | 395 | ret = roundInt(result/10.0f); |
superphil06 | 0:f1951c6c9187 | 396 | } |
superphil06 | 1:8326614f3037 | 397 | } |
superphil06 | 1:8326614f3037 | 398 | |
superphil06 | 1:8326614f3037 | 399 | else{ |
superphil06 | 1:8326614f3037 | 400 | |
superphil06 | 1:8326614f3037 | 401 | m_I2C.stop();// rperform stop |
superphil06 | 1:8326614f3037 | 402 | } |
superphil06 | 1:8326614f3037 | 403 | return ret; |
superphil06 | 1:8326614f3037 | 404 | } |
superphil06 | 0:f1951c6c9187 | 405 | |
superphil06 | 1:8326614f3037 | 406 | |
superphil06 | 0:f1951c6c9187 | 407 | |
superphil06 | 0:f1951c6c9187 | 408 | |
superphil06 | 0:f1951c6c9187 | 409 | |
superphil06 | 0:f1951c6c9187 | 410 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 411 | Function: getConpensatedRH |
superphil06 | 1:8326614f3037 | 412 | Purpose : return the compensated calulated humidity |
superphil06 | 0:f1951c6c9187 | 413 | Input : true if we want to round value to 1 digit precision, else 2 |
superphil06 | 0:f1951c6c9187 | 414 | Output : the compensed RH value (rounded or not) |
superphil06 | 1:8326614f3037 | 415 | Comments: |
superphil06 | 0:f1951c6c9187 | 416 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 417 | int16_t TH02::getConpensatedRH(bool round) |
superphil06 | 0:f1951c6c9187 | 418 | { |
superphil06 | 1:8326614f3037 | 419 | float rhvalue ; |
superphil06 | 1:8326614f3037 | 420 | float rhlinear ; |
superphil06 | 1:8326614f3037 | 421 | int16_t ret = TH02_UNDEFINED_VALUE; |
superphil06 | 0:f1951c6c9187 | 422 | |
superphil06 | 1:8326614f3037 | 423 | // did we had a previous measure RH |
superphil06 | 1:8326614f3037 | 424 | if (_last_rh != TH02_UNINITIALIZED_RH) { |
superphil06 | 1:8326614f3037 | 425 | // now we're float restore real value RH value |
superphil06 | 1:8326614f3037 | 426 | rhvalue = (float) _last_rh / 100.0 ; |
superphil06 | 1:8326614f3037 | 427 | |
superphil06 | 1:8326614f3037 | 428 | // apply linear compensation |
superphil06 | 1:8326614f3037 | 429 | rhlinear = rhvalue - ((rhvalue*rhvalue) * TH02_A2 + rhvalue * TH02_A1 + TH02_A0); |
superphil06 | 1:8326614f3037 | 430 | |
superphil06 | 1:8326614f3037 | 431 | // correct value |
superphil06 | 1:8326614f3037 | 432 | rhvalue = rhlinear; |
superphil06 | 0:f1951c6c9187 | 433 | |
superphil06 | 1:8326614f3037 | 434 | // do we have a initialized temperature value ? |
superphil06 | 1:8326614f3037 | 435 | if (_last_temp != TH02_UNINITIALIZED_TEMP ) { |
superphil06 | 1:8326614f3037 | 436 | // Apply Temperature compensation |
superphil06 | 1:8326614f3037 | 437 | // remember last temp was stored * 100 |
superphil06 | 1:8326614f3037 | 438 | rhvalue += ((_last_temp/100.0) - 30.0) * (rhlinear * TH02_Q1 + TH02_Q0); |
superphil06 | 1:8326614f3037 | 439 | } |
superphil06 | 1:8326614f3037 | 440 | |
superphil06 | 1:8326614f3037 | 441 | // now get back * 100 to have int with 2 digit precision |
superphil06 | 1:8326614f3037 | 442 | rhvalue *= 100; |
superphil06 | 1:8326614f3037 | 443 | |
superphil06 | 1:8326614f3037 | 444 | // do we need to round to 1 digit ? |
superphil06 | 1:8326614f3037 | 445 | if (round) { |
superphil06 | 1:8326614f3037 | 446 | // remember result value is multiplied by 10 to avoid float calculation later |
superphil06 | 1:8326614f3037 | 447 | // so humidity of 45.6% is 456 |
superphil06 | 1:8326614f3037 | 448 | ret = roundInt(rhvalue/10.0f); |
superphil06 | 1:8326614f3037 | 449 | } else { |
superphil06 | 1:8326614f3037 | 450 | ret = (int16_t) rhvalue; |
superphil06 | 1:8326614f3037 | 451 | } |
superphil06 | 0:f1951c6c9187 | 452 | } |
superphil06 | 1:8326614f3037 | 453 | |
superphil06 | 1:8326614f3037 | 454 | return ret; |
superphil06 | 0:f1951c6c9187 | 455 | } |
superphil06 | 0:f1951c6c9187 | 456 | |
superphil06 | 0:f1951c6c9187 | 457 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 458 | Function: getLastRawRH |
superphil06 | 1:8326614f3037 | 459 | Purpose : return the raw humidity * 100 |
superphil06 | 1:8326614f3037 | 460 | Input : |
superphil06 | 0:f1951c6c9187 | 461 | Output : int value (ie 4123 for 41.23%) |
superphil06 | 1:8326614f3037 | 462 | Comments: |
superphil06 | 0:f1951c6c9187 | 463 | ====================================================================== */ |
superphil06 | 0:f1951c6c9187 | 464 | int32_t TH02::getLastRawRH(void) |
superphil06 | 0:f1951c6c9187 | 465 | { |
superphil06 | 1:8326614f3037 | 466 | return _last_rh; |
superphil06 | 0:f1951c6c9187 | 467 | } |
superphil06 | 0:f1951c6c9187 | 468 | |
superphil06 | 0:f1951c6c9187 | 469 | /* ====================================================================== |
superphil06 | 0:f1951c6c9187 | 470 | Function: getLastRawTemp |
superphil06 | 1:8326614f3037 | 471 | Purpose : return the raw temperature value * 100 |
superphil06 | 1:8326614f3037 | 472 | Input : |
superphil06 | 0:f1951c6c9187 | 473 | Output : int value (ie 2124 for 21.24 C) |
superphil06 | 1:8326614f3037 | 474 | Comments: |
superphil06 | 0:f1951c6c9187 | 475 | ====================================================================== */ |
superphil06 | 1:8326614f3037 | 476 | int32_t TH02::getLastRawTemp(void) |
superphil06 | 0:f1951c6c9187 | 477 | { |
superphil06 | 1:8326614f3037 | 478 | return _last_temp; |
superphil06 | 0:f1951c6c9187 | 479 | } |