:)

Dependencies:   Servo Cayenne-LPP

Committer:
superphil06
Date:
Tue Oct 22 08:45:41 2019 +0000
Revision:
58:81c66fac6476
mise a jour avec lib TH02

Who changed what in which revision?

UserRevisionLine numberNew contents of line
superphil06 58:81c66fac6476 1 // **********************************************************************************
superphil06 58:81c66fac6476 2 // Driver definition for HopeRF TH02 temperature and humidity sensor
superphil06 58:81c66fac6476 3 // **********************************************************************************
superphil06 58:81c66fac6476 4 // Creative Commons Attrib Share-Alike License
superphil06 58:81c66fac6476 5 // You are free to use/extend this library but please abide with the CC-BY-SA license:
superphil06 58:81c66fac6476 6 // http://creativecommons.org/licenses/by-sa/4.0/
superphil06 58:81c66fac6476 7 //
superphil06 58:81c66fac6476 8 // For any explanation see TH02 sensor information at
superphil06 58:81c66fac6476 9 // http://www.hoperf.com/sensor/app/TH02.htm
superphil06 58:81c66fac6476 10 //
superphil06 58:81c66fac6476 11 // Code based on following datasheet
superphil06 58:81c66fac6476 12 // http://www.hoperf.com/upload/sensor/TH02_V1.1.pdf
superphil06 58:81c66fac6476 13 //
superphil06 58:81c66fac6476 14 // Written by Charles-Henri Hallard (http://hallard.me)
superphil06 58:81c66fac6476 15 //ported to mbed env by Philippe LAURENT (IUT de NICE France)
superphil06 58:81c66fac6476 16 //
superphil06 58:81c66fac6476 17 // History : V1.00 2014-07-14 - First release
superphil06 58:81c66fac6476 18 // V1.10 2015-04-13 - changed to Wire library instead of I2C
superphil06 58:81c66fac6476 19 //
superphil06 58:81c66fac6476 20 // All text above must be included in any redistribution.
superphil06 58:81c66fac6476 21 //
superphil06 58:81c66fac6476 22 // **********************************************************************************
superphil06 58:81c66fac6476 23 #ifndef TH02_H
superphil06 58:81c66fac6476 24 #define TH02_H
superphil06 58:81c66fac6476 25
superphil06 58:81c66fac6476 26 #include <mbed.h> //
superphil06 58:81c66fac6476 27
superphil06 58:81c66fac6476 28 // TH02 I2C Device address
superphil06 58:81c66fac6476 29 #define TH02_I2C_ADDR 0x40
superphil06 58:81c66fac6476 30
superphil06 58:81c66fac6476 31 // TH02 Registers addresses
superphil06 58:81c66fac6476 32 #define TH02_STATUS 0
superphil06 58:81c66fac6476 33 #define TH02_DATAh 1
superphil06 58:81c66fac6476 34 #define TH02_DATAl 2
superphil06 58:81c66fac6476 35 #define TH02_CONFIG 3
superphil06 58:81c66fac6476 36 #define TH02_ID 17
superphil06 58:81c66fac6476 37
superphil06 58:81c66fac6476 38 // TH02 custom error code return function
superphil06 58:81c66fac6476 39 #define TH02_I2C_ERR 0xFF
superphil06 58:81c66fac6476 40
superphil06 58:81c66fac6476 41 // Unititialized values (arbitrary)
superphil06 58:81c66fac6476 42 #define TH02_UNINITIALIZED_TEMP 55555 // int32_t internal value
superphil06 58:81c66fac6476 43 #define TH02_UNINITIALIZED_RH 1111 // int32_t internal value
superphil06 58:81c66fac6476 44 #define TH02_UNDEFINED_VALUE 12345 // int16_t returned value
superphil06 58:81c66fac6476 45
superphil06 58:81c66fac6476 46 // we decide error if conversion is >= 50ms
superphil06 58:81c66fac6476 47 #define TH02_CONVERSION_TIME_OUT 50
superphil06 58:81c66fac6476 48
superphil06 58:81c66fac6476 49 // Bit definition of TH02 registers values
superphil06 58:81c66fac6476 50 #define TH02_STATUS_RDY 0x01
superphil06 58:81c66fac6476 51
superphil06 58:81c66fac6476 52 #define TH02_CONFIG_START 0x01
superphil06 58:81c66fac6476 53 #define TH02_CONFIG_HEAT 0x02
superphil06 58:81c66fac6476 54 #define TH02_CONFIG_TEMP 0x10
superphil06 58:81c66fac6476 55 #define TH02_CONFIG_HUMI 0x00
superphil06 58:81c66fac6476 56 #define TH02_CONFIG_FAST 0x20
superphil06 58:81c66fac6476 57
superphil06 58:81c66fac6476 58 // THO2 Linearization Coefficients
superphil06 58:81c66fac6476 59 #define TH02_A0 -4.7844
superphil06 58:81c66fac6476 60 #define TH02_A1 0.4008
superphil06 58:81c66fac6476 61 #define TH02_A2 -0.00393
superphil06 58:81c66fac6476 62
superphil06 58:81c66fac6476 63 // TH02 Temperature compensation Linearization Coefficients
superphil06 58:81c66fac6476 64 #define TH02_Q0 0.1973
superphil06 58:81c66fac6476 65 #define TH02_Q1 0.00237
superphil06 58:81c66fac6476 66
superphil06 58:81c66fac6476 67
superphil06 58:81c66fac6476 68
superphil06 58:81c66fac6476 69
superphil06 58:81c66fac6476 70
superphil06 58:81c66fac6476 71 class TH02 {
superphil06 58:81c66fac6476 72 public:
superphil06 58:81c66fac6476 73 TH02(uint8_t address);
superphil06 58:81c66fac6476 74 uint8_t getId(uint8_t * pvalue);
superphil06 58:81c66fac6476 75 uint8_t getId(void);
superphil06 58:81c66fac6476 76 uint8_t getStatus(uint8_t * pvalue);
superphil06 58:81c66fac6476 77 bool isConverting(void);
superphil06 58:81c66fac6476 78 uint8_t waitEndConversion(void);
superphil06 58:81c66fac6476 79 uint8_t getConfig(uint8_t * pvalue);
superphil06 58:81c66fac6476 80 uint8_t setConfig(uint8_t config);
superphil06 58:81c66fac6476 81 uint8_t startTempConv(bool fastmode = false, bool heater = false);
superphil06 58:81c66fac6476 82 uint8_t startRHConv(bool fastmode = false, bool heater = false);
superphil06 58:81c66fac6476 83 int16_t roundInt(float value);
superphil06 58:81c66fac6476 84 int16_t getConversionValue(void);
superphil06 58:81c66fac6476 85 int16_t getConpensatedRH(bool round);
superphil06 58:81c66fac6476 86 int32_t getLastRawRH(void);
superphil06 58:81c66fac6476 87 int32_t getLastRawTemp(void);
superphil06 58:81c66fac6476 88 // int16_t getConversionValue_Raw(void);
superphil06 58:81c66fac6476 89
superphil06 58:81c66fac6476 90 /**
superphil06 58:81c66fac6476 91 * TH02 constructor
superphil06 58:81c66fac6476 92 *
superphil06 58:81c66fac6476 93 * @param sda I2C data pin
superphil06 58:81c66fac6476 94 * @param scl I2C clock pin
superphil06 58:81c66fac6476 95 * @param address I2C slave sensor address
superphil06 58:81c66fac6476 96
superphil06 58:81c66fac6476 97 */
superphil06 58:81c66fac6476 98 TH02(PinName sda, PinName scl, uint8_t address);
superphil06 58:81c66fac6476 99
superphil06 58:81c66fac6476 100 /**
superphil06 58:81c66fac6476 101 * MFRC522 destructor
superphil06 58:81c66fac6476 102 */
superphil06 58:81c66fac6476 103 ~TH02();
superphil06 58:81c66fac6476 104
superphil06 58:81c66fac6476 105
superphil06 58:81c66fac6476 106
superphil06 58:81c66fac6476 107 private:
superphil06 58:81c66fac6476 108 I2C m_I2C;
superphil06 58:81c66fac6476 109 uint8_t writeCommand(uint8_t command, bool release=true);
superphil06 58:81c66fac6476 110 uint8_t writeRegister(uint8_t reg, uint8_t value);
superphil06 58:81c66fac6476 111 uint8_t readRegister(uint8_t reg, uint8_t * value);
superphil06 58:81c66fac6476 112
superphil06 58:81c66fac6476 113 int32_t _last_temp; // Last measured temperature (for linearization)
superphil06 58:81c66fac6476 114 int32_t _last_rh; // Last measured RH
superphil06 58:81c66fac6476 115 uint8_t _address; // I2C Module Address
superphil06 58:81c66fac6476 116
superphil06 58:81c66fac6476 117
superphil06 58:81c66fac6476 118
superphil06 58:81c66fac6476 119 };
superphil06 58:81c66fac6476 120
superphil06 58:81c66fac6476 121
superphil06 58:81c66fac6476 122
superphil06 58:81c66fac6476 123 #endif