Advertisement
jamrab

TH02_dev.cpp

Apr 23rd, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. /*
  2.     TH02_dev.cpp
  3.     Driver for DIGITAL I2C HUMIDITY AND TEMPERATURE SENSOR
  4.  
  5.     Copyright (c) 2014 seeed technology inc.
  6.     Website    : www.seeed.cc
  7.     Author     : Oliver Wang
  8.     Create Time: April 2014
  9.     Change Log :
  10.  
  11.     The MIT License (MIT)
  12.  
  13.     Permission is hereby granted, free of charge, to any person obtaining a copy
  14.     of this software and associated documentation files (the "Software"), to deal
  15.     in the Software without restriction, including without limitation the rights
  16.     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17.     copies of the Software, and to permit persons to whom the Software is
  18.     furnished to do so, subject to the following conditions:
  19.  
  20.     The above copyright notice and this permission notice shall be included in
  21.     all copies or substantial portions of the Software.
  22.  
  23.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24.     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25.     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26.     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27.     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29.     THE SOFTWARE.
  30. */
  31.  
  32. /****************************************************************************/
  33. /***        Include files                                                 ***/
  34. /****************************************************************************/
  35. #include "TH02_dev.h"
  36. #include <Wire.h>
  37. #include <Particle.h>
  38. /* Use Serial IIC */
  39. #ifdef SERIAL_IIC
  40. #endif
  41.  
  42. TH02_dev TH02;
  43. /****************************************************************************/
  44. /***       Local Variable                                                 ***/
  45. /****************************************************************************/
  46.  
  47.  
  48. /****************************************************************************/
  49. /***       Class member Functions                                         ***/
  50. /****************************************************************************/
  51.  
  52. void TH02_dev::begin(void) {
  53.     /* Start IIC */
  54.     Wire.begin();
  55.     /* TH02 don't need to software reset */
  56. }
  57.  
  58. float TH02_dev::ReadTemperature(void) {
  59.     /* Start a new temperature conversion */
  60.     TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_TEMP);
  61.     //delay(100);
  62.     /* Wait until conversion is done */
  63.     while (!isAvailable());
  64.     uint16_t value = TH02_IIC_ReadData();
  65.  
  66.     value = value >> 2;
  67.     /*
  68.         Formula:
  69.         Temperature(C) = (Value/32) - 50
  70.     */
  71.     float temper = (value / 32.0) - 50.0;
  72.  
  73.     return temper;
  74. }
  75.  
  76. float TH02_dev::ReadHumidity(void) {
  77.     /* Start a new humility conversion */
  78.     TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_HUMI);
  79.  
  80.     /* Wait until conversion is done */
  81.     //delay(100);
  82.     while (!isAvailable());
  83.     uint16_t value = TH02_IIC_ReadData();
  84.  
  85.     value = value >> 4;
  86.  
  87.     /*
  88.         Formula:
  89.         Humidity(%) = (Value/16) - 24
  90.     */
  91.  
  92.     float humility = (value / 16.0) - 24.0;
  93.  
  94.     return humility;
  95. }
  96.  
  97. /****************************************************************************/
  98. /***       Local Functions                                                ***/
  99. /****************************************************************************/
  100. uint8_t TH02_dev::isAvailable() {
  101.     uint8_t status =  TH02_IIC_ReadReg(REG_STATUS);
  102.     if (status & STATUS_RDY_MASK) {
  103.         return 0;    //ready
  104.     } else {
  105.         return 1;    //not ready yet
  106.     }
  107. }
  108.  
  109. void TH02_dev::TH02_IIC_WriteCmd(uint8_t u8Cmd) {
  110.     /* Port to arduino */
  111.     Wire.beginTransmission(TH02_I2C_DEV_ID);
  112.     Wire.write(u8Cmd);
  113.     Wire.endTransmission();
  114. }
  115.  
  116. uint8_t TH02_dev::TH02_IIC_ReadReg(uint8_t u8Reg) {
  117.     /* Port to arduino */
  118.     uint8_t Temp = 0;
  119.  
  120.     /* Send a register reading command */
  121.     TH02_IIC_WriteCmd(u8Reg);
  122.  
  123.     Wire.requestFrom(TH02_I2C_DEV_ID, 1);
  124.     while (Wire.available()) {
  125.         Temp = Wire.read();
  126.     }
  127.  
  128.     return Temp;
  129. }
  130.  
  131. void TH02_dev::TH02_IIC_WriteReg(uint8_t u8Reg, uint8_t u8Data) {
  132.     Wire.beginTransmission(TH02_I2C_DEV_ID);
  133.     Wire.write(u8Reg);
  134.     Wire.write(u8Data);
  135.     Wire.endTransmission();
  136. }
  137.  
  138. uint16_t TH02_dev::TH02_IIC_ReadData(void) {
  139.     /* Port to arduino */
  140.     uint16_t Temp = TH02_IIC_ReadData2byte();
  141.     return Temp;
  142. }
  143.  
  144. uint16_t TH02_dev::TH02_IIC_ReadData2byte() {
  145.     uint16_t TempData = 0;
  146.     uint16_t tmpArray[3] = {0};
  147.  
  148.     int cnt = 0;
  149.     TH02_IIC_WriteCmd(REG_DATA_H);
  150.  
  151.     Wire.requestFrom(TH02_I2C_DEV_ID, 3);
  152.     while (Wire.available()) {
  153.         tmpArray[cnt] = (uint16_t)Wire.read();
  154.         cnt++;
  155.     }
  156.     /* MSB */
  157.     TempData = (tmpArray[1] << 8) | (tmpArray[2]);
  158.     return TempData;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement