![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
corrected TH02 sample program
Dependencies: mbed
Fork of TH02_humidity_temp by
th02.h@0:f1951c6c9187, 2017-03-17 (annotated)
- Committer:
- superphil06
- Date:
- Fri Mar 17 10:13:49 2017 +0000
- Revision:
- 0:f1951c6c9187
- Child:
- 1:8326614f3037
TH02_sample_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 | 0:f1951c6c9187 | 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 | //ported to mbed env by Philippe LAURENT (IUT de NICE France) |
superphil06 | 0:f1951c6c9187 | 16 | // |
superphil06 | 0:f1951c6c9187 | 17 | // History : V1.00 2014-07-14 - First release |
superphil06 | 0:f1951c6c9187 | 18 | // V1.10 2015-04-13 - changed to Wire library instead of I2C |
superphil06 | 0:f1951c6c9187 | 19 | // |
superphil06 | 0:f1951c6c9187 | 20 | // All text above must be included in any redistribution. |
superphil06 | 0:f1951c6c9187 | 21 | // |
superphil06 | 0:f1951c6c9187 | 22 | // ********************************************************************************** |
superphil06 | 0:f1951c6c9187 | 23 | #ifndef TH02_H |
superphil06 | 0:f1951c6c9187 | 24 | #define TH02_H |
superphil06 | 0:f1951c6c9187 | 25 | |
superphil06 | 0:f1951c6c9187 | 26 | #include <mbed.h> // |
superphil06 | 0:f1951c6c9187 | 27 | |
superphil06 | 0:f1951c6c9187 | 28 | // TH02 I2C Device address |
superphil06 | 0:f1951c6c9187 | 29 | #define TH02_I2C_ADDR 0x40 |
superphil06 | 0:f1951c6c9187 | 30 | |
superphil06 | 0:f1951c6c9187 | 31 | // TH02 Registers addresses |
superphil06 | 0:f1951c6c9187 | 32 | #define TH02_STATUS 0 |
superphil06 | 0:f1951c6c9187 | 33 | #define TH02_DATAh 1 |
superphil06 | 0:f1951c6c9187 | 34 | #define TH02_DATAl 2 |
superphil06 | 0:f1951c6c9187 | 35 | #define TH02_CONFIG 3 |
superphil06 | 0:f1951c6c9187 | 36 | #define TH02_ID 17 |
superphil06 | 0:f1951c6c9187 | 37 | |
superphil06 | 0:f1951c6c9187 | 38 | // TH02 custom error code return function |
superphil06 | 0:f1951c6c9187 | 39 | #define TH02_I2C_ERR 0xFF |
superphil06 | 0:f1951c6c9187 | 40 | |
superphil06 | 0:f1951c6c9187 | 41 | // Unititialized values (arbitrary) |
superphil06 | 0:f1951c6c9187 | 42 | #define TH02_UNINITIALIZED_TEMP 55555 // int32_t internal value |
superphil06 | 0:f1951c6c9187 | 43 | #define TH02_UNINITIALIZED_RH 1111 // int32_t internal value |
superphil06 | 0:f1951c6c9187 | 44 | #define TH02_UNDEFINED_VALUE 12345 // int16_t returned value |
superphil06 | 0:f1951c6c9187 | 45 | |
superphil06 | 0:f1951c6c9187 | 46 | // we decide error if conversion is >= 50ms |
superphil06 | 0:f1951c6c9187 | 47 | #define TH02_CONVERSION_TIME_OUT 50 |
superphil06 | 0:f1951c6c9187 | 48 | |
superphil06 | 0:f1951c6c9187 | 49 | // Bit definition of TH02 registers values |
superphil06 | 0:f1951c6c9187 | 50 | #define TH02_STATUS_RDY 0x01 |
superphil06 | 0:f1951c6c9187 | 51 | |
superphil06 | 0:f1951c6c9187 | 52 | #define TH02_CONFIG_START 0x01 |
superphil06 | 0:f1951c6c9187 | 53 | #define TH02_CONFIG_HEAT 0x02 |
superphil06 | 0:f1951c6c9187 | 54 | #define TH02_CONFIG_TEMP 0x10 |
superphil06 | 0:f1951c6c9187 | 55 | #define TH02_CONFIG_HUMI 0x00 |
superphil06 | 0:f1951c6c9187 | 56 | #define TH02_CONFIG_FAST 0x20 |
superphil06 | 0:f1951c6c9187 | 57 | |
superphil06 | 0:f1951c6c9187 | 58 | // THO2 Linearization Coefficients |
superphil06 | 0:f1951c6c9187 | 59 | #define TH02_A0 -4.7844 |
superphil06 | 0:f1951c6c9187 | 60 | #define TH02_A1 0.4008 |
superphil06 | 0:f1951c6c9187 | 61 | #define TH02_A2 -0.00393 |
superphil06 | 0:f1951c6c9187 | 62 | |
superphil06 | 0:f1951c6c9187 | 63 | // TH02 Temperature compensation Linearization Coefficients |
superphil06 | 0:f1951c6c9187 | 64 | #define TH02_Q0 0.1973 |
superphil06 | 0:f1951c6c9187 | 65 | #define TH02_Q1 0.00237 |
superphil06 | 0:f1951c6c9187 | 66 | |
superphil06 | 0:f1951c6c9187 | 67 | |
superphil06 | 0:f1951c6c9187 | 68 | |
superphil06 | 0:f1951c6c9187 | 69 | |
superphil06 | 0:f1951c6c9187 | 70 | |
superphil06 | 0:f1951c6c9187 | 71 | class TH02 { |
superphil06 | 0:f1951c6c9187 | 72 | public: |
superphil06 | 0:f1951c6c9187 | 73 | TH02(uint8_t address); |
superphil06 | 0:f1951c6c9187 | 74 | uint8_t getId(uint8_t * pvalue); |
superphil06 | 0:f1951c6c9187 | 75 | uint8_t getId(void); |
superphil06 | 0:f1951c6c9187 | 76 | uint8_t getStatus(uint8_t * pvalue); |
superphil06 | 0:f1951c6c9187 | 77 | bool isConverting(void); |
superphil06 | 0:f1951c6c9187 | 78 | uint8_t waitEndConversion(void); |
superphil06 | 0:f1951c6c9187 | 79 | uint8_t getConfig(uint8_t * pvalue); |
superphil06 | 0:f1951c6c9187 | 80 | uint8_t setConfig(uint8_t config); |
superphil06 | 0:f1951c6c9187 | 81 | uint8_t startTempConv(bool fastmode = false, bool heater = false); |
superphil06 | 0:f1951c6c9187 | 82 | uint8_t startRHConv(bool fastmode = false, bool heater = false); |
superphil06 | 0:f1951c6c9187 | 83 | int16_t roundInt(float value); |
superphil06 | 0:f1951c6c9187 | 84 | int16_t getConversionValue(void); |
superphil06 | 0:f1951c6c9187 | 85 | int16_t getConpensatedRH(bool round); |
superphil06 | 0:f1951c6c9187 | 86 | int32_t getLastRawRH(void); |
superphil06 | 0:f1951c6c9187 | 87 | int32_t getLastRawTemp(void); |
superphil06 | 0:f1951c6c9187 | 88 | |
superphil06 | 0:f1951c6c9187 | 89 | /** |
superphil06 | 0:f1951c6c9187 | 90 | * TH02 constructor |
superphil06 | 0:f1951c6c9187 | 91 | * |
superphil06 | 0:f1951c6c9187 | 92 | * @param sda I2C data pin |
superphil06 | 0:f1951c6c9187 | 93 | * @param scl I2C clock pin |
superphil06 | 0:f1951c6c9187 | 94 | * @param address I2C slave sensor address |
superphil06 | 0:f1951c6c9187 | 95 | |
superphil06 | 0:f1951c6c9187 | 96 | */ |
superphil06 | 0:f1951c6c9187 | 97 | TH02(PinName sda, PinName scl, uint8_t address); |
superphil06 | 0:f1951c6c9187 | 98 | |
superphil06 | 0:f1951c6c9187 | 99 | /** |
superphil06 | 0:f1951c6c9187 | 100 | * MFRC522 destructor |
superphil06 | 0:f1951c6c9187 | 101 | */ |
superphil06 | 0:f1951c6c9187 | 102 | ~TH02(); |
superphil06 | 0:f1951c6c9187 | 103 | |
superphil06 | 0:f1951c6c9187 | 104 | |
superphil06 | 0:f1951c6c9187 | 105 | |
superphil06 | 0:f1951c6c9187 | 106 | private: |
superphil06 | 0:f1951c6c9187 | 107 | I2C m_I2C; |
superphil06 | 0:f1951c6c9187 | 108 | uint8_t writeCommand(uint8_t command, bool release=true); |
superphil06 | 0:f1951c6c9187 | 109 | uint8_t writeRegister(uint8_t reg, uint8_t value); |
superphil06 | 0:f1951c6c9187 | 110 | uint8_t readRegister(uint8_t reg, uint8_t * value); |
superphil06 | 0:f1951c6c9187 | 111 | |
superphil06 | 0:f1951c6c9187 | 112 | int32_t _last_temp; // Last measured temperature (for linearization) |
superphil06 | 0:f1951c6c9187 | 113 | int32_t _last_rh; // Last measured RH |
superphil06 | 0:f1951c6c9187 | 114 | uint8_t _address; // I2C Module Address |
superphil06 | 0:f1951c6c9187 | 115 | |
superphil06 | 0:f1951c6c9187 | 116 | |
superphil06 | 0:f1951c6c9187 | 117 | |
superphil06 | 0:f1951c6c9187 | 118 | }; |
superphil06 | 0:f1951c6c9187 | 119 | |
superphil06 | 0:f1951c6c9187 | 120 | |
superphil06 | 0:f1951c6c9187 | 121 | |
superphil06 | 0:f1951c6c9187 | 122 | #endif |