Subversion Repositories ChibiGauge

Rev

Rev 6 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //==============================================================================
  2. //    E - R A D I O N I C A . C O M,  H.Kolomana 6/A, Djakovo 31400, Croatia
  3. //==============================================================================
  4. // Project   :  SHT21 Arduino Library (V1.0)
  5. // File      :  SHT21.h
  6. // Author    :  e-radionica.com 2017
  7. // Licence   :  Open-source !
  8. //==============================================================================
  9. //==============================================================================
  10. // Use with any SHT21 breakout. Check ours:
  11. // https://e-radionica.com/en/sht21-humidity-and-temperature-sensor.html
  12. // If any questions,
  13. // just contact techsupport@e-radionica.com
  14. //==============================================================================
  15.  
  16. #ifndef SHT21_H
  17. #define SHT21_H
  18. #include "ch.h"
  19. #include "hal.h"
  20.  
  21. //---------- Defines -----------------------------------------------------------
  22. #define I2C_ADD 0x40    // I2C device address
  23.  
  24. const uint16_t POLYNOMIAL = 0x131;  // P(x)=x^8+x^5+x^4+1 = 100110001
  25.  
  26. //==============================================================================
  27. #define TRIGGER_T_MEASUREMENT_HM 0XE3   // command trig. temp meas. hold master
  28. #define TRIGGER_RH_MEASUREMENT_HM 0XE5  // command trig. hum. meas. hold master
  29. #define TRIGGER_T_MEASUREMENT_NHM 0XF3  // command trig. temp meas. no hold master
  30. #define TRIGGER_RH_MEASUREMENT_NHM 0XF5 // command trig. hum. meas. no hold master
  31. #define USER_REGISTER_W 0XE6                // command writing user register
  32. #define USER_REGISTER_R 0XE7            // command reading user register
  33. #define SOFT_RESET 0XFE                 // command soft reset
  34. //==============================================================================
  35. // HOLD MASTER - SCL line is blocked (controlled by sensor) during measurement
  36. // NO HOLD MASTER - allows other I2C communication tasks while sensor performing
  37. // measurements.
  38.  
  39.  
  40. class SHT21
  41. {
  42.         private:
  43.                 //==============================================================================
  44.                 uint16_t readSensor_hm(uint8_t command);
  45.                 //==============================================================================
  46.                 // reads SHT21 with hold master operation mode
  47.                 // input:       temp/hum command
  48.                 // return:      temp/hum raw data (16bit scaled)
  49.  
  50.  
  51.                 //==============================================================================
  52.                 float CalcRH(uint16_t rh);
  53.                 //==============================================================================
  54.                 // calculates the relative humidity
  55.                 // input:  rh:   humidity raw value (16bit scaled)
  56.                 // return:               relative humidity [%RH] (float)
  57.  
  58.                 //==============================================================================
  59.                 float CalcT(uint16_t t);
  60.                 //==============================================================================
  61.                 // calculates the temperature
  62.                 // input:  t:   temperature raw value (16bit scaled)
  63.                 // return:              relative temperature [°C] (float)
  64.  
  65.                 //==============================================================================
  66.                 uint8_t CRC_Checksum(uint8_t data[], uint8_t no_of_bytes, uint8_t checksum);
  67.                 //==============================================================================
  68.                 // CRC-8 checksum for error detection
  69.                 // input:  data[]       checksum is built based on this data
  70.                 //         no_of_bytes  checksum is built for n bytes of data
  71.                 //         checksum     expected checksum
  72.                 // return:              1                          = checksum does not match
  73.                 //                      0              = checksum matches
  74.  
  75.  
  76.  
  77.                 const I2CDriver * m_driver;
  78.  
  79.         public:
  80.                 SHT21(I2CDriver * driver) { m_driver = driver; };
  81.  
  82.                 // I2C config for this chip
  83.                 static I2CConfig  constexpr i2ccfg = { OPMODE_I2C, 400000, FAST_DUTY_CYCLE_2, };
  84.  
  85.  
  86.                 //==============================================================================
  87.                 float getHumidity(void);
  88.                 //==============================================================================
  89.                 // calls humidity measurement with hold master mode
  90.  
  91.                 //==============================================================================
  92.                 float getTemperature(void);
  93.                 //==============================================================================
  94.                 // calls temperature measurement with hold master mode
  95.  
  96.                 //==============================================================================
  97.                 void reset();
  98.                 //==============================================================================
  99.                 // performs a soft reset, delays 15ms
  100.  
  101.                 //==============================================================================
  102.                 msg_t  getSerialNumber(uint8_t  * serialNumber );
  103.                 //==============================================================================
  104.                 // requires pointer to 8 byte buffer to return serial number
  105.                 // returns electronical identification code depending of selected memory
  106.                 // location
  107.  
  108. };
  109.  
  110. #endif
  111.