Uttam Bhat / driver_mbed_TH02

Fork of driver_mbed_TH02 by Taylor Gy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers driver_mbed_TH02.cpp Source File

driver_mbed_TH02.cpp

00001 /*
00002  * TH02_dev.cpp
00003  * Driver for DIGITAL I2C HUMIDITY AND TEMPERATURE SENSOR
00004  *  
00005  * Copyright (c) 2014 seeed technology inc.
00006  * Website    : www.seeed.cc
00007  * Author     : Oliver Wang
00008  * Create Time: April 2014
00009  * Change Log :
00010  *
00011  * The MIT License (MIT)
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining a copy
00014  * of this software and associated documentation files (the "Software"), to deal
00015  * in the Software without restriction, including without limitation the rights
00016  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00017  * copies of the Software, and to permit persons to whom the Software is
00018  * furnished to do so, subject to the following conditions:
00019  *
00020  * The above copyright notice and this permission notice shall be included in
00021  * all copies or substantial portions of the Software.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00024  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00025  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00026  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00027  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00028  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00029  * THE SOFTWARE.
00030  */
00031 
00032 /****************************************************************************/
00033 /***        Include files                                                 ***/
00034 /****************************************************************************/
00035 #include "driver_mbed_TH02.h"
00036 // #include <Wire.h>
00037 // #include <Arduino.h>
00038 
00039 /* Use Serial IIC */
00040 #ifdef SERIAL_IIC
00041 #endif
00042 
00043 TH02_dev myTH02;
00044 I2C i2cth02(D14, D15);
00045 /****************************************************************************/
00046 /***       Local Variable                                                 ***/
00047 /****************************************************************************/
00048 
00049 
00050 /****************************************************************************/
00051 /***       Class member Functions                                         ***/
00052 /****************************************************************************/
00053 
00054 void TH02_dev::begin(void)
00055 {
00056     /* Start IIC */
00057     // Wire.begin();
00058     /* TH02 don't need to software reset */
00059     printf("System Start !\r\n\r\n");
00060 }
00061 
00062 float TH02_dev::ReadTemperature(void)
00063 {    
00064     /* Start a new temperature conversion */
00065     TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_TEMP);         
00066     //delay(100);
00067     /* Wait until conversion is done */
00068     while(!isAvailable());
00069     uint16_t value = TH02_IIC_ReadData();
00070     
00071     value = value >> 2;
00072     /* 
00073       Formula:
00074       Temperature(C) = (Value/32) - 50    
00075     */  
00076     float temper = (value/32.0)-50.0;
00077     
00078     return temper;
00079 }
00080  
00081 float TH02_dev::ReadHumidity(void)
00082 {
00083  /* Start a new humility conversion */
00084     TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_HUMI);
00085     
00086     /* Wait until conversion is done */
00087     //delay(100);
00088     while(!isAvailable());
00089     uint16_t value = TH02_IIC_ReadData();
00090     
00091     value = value >> 4;
00092  
00093     /* 
00094       Formula:
00095       Humidity(%) = (Value/16) - 24   
00096     */  
00097 
00098     float humility = (value/16.0)-24.0;
00099     
00100     return humility;
00101 }
00102 
00103 /****************************************************************************/
00104 /***       Local Functions                                                ***/
00105 /****************************************************************************/
00106 uint8_t TH02_dev::isAvailable()
00107 {
00108     uint8_t status =  TH02_IIC_ReadReg(REG_STATUS);
00109     if(status & STATUS_RDY_MASK)
00110     {
00111         return 0;    //ready
00112     }
00113     else
00114     {
00115         return 1;    //not ready yet
00116     }
00117 }
00118 
00119 void TH02_dev::TH02_IIC_WriteCmd(uint8_t u8Cmd)
00120 {       
00121     char cmd = u8Cmd;
00122     /* Port to arduino */
00123     // Wire.beginTransmission(TH02_I2C_DEV_ID);
00124     // Wire.write(u8Cmd);
00125     // Wire.endTransmission();
00126     i2cth02.write(TH02_I2C_DEV_ID, &cmd, 1);
00127 
00128 }
00129 
00130 uint8_t TH02_dev::TH02_IIC_ReadReg(uint8_t u8Reg)
00131 {
00132     /* Port to arduino */
00133     char Temp = 0;
00134     
00135     /* Send a register reading command */
00136     TH02_IIC_WriteCmd(u8Reg);   
00137          
00138     // Wire.requestFrom(TH02_I2C_DEV_ID, 1);     
00139     // while(Wire.available())
00140     // {
00141     //     Temp = Wire.read();   
00142     // }
00143 
00144     i2cth02.read(TH02_I2C_DEV_ID, &Temp, 1);
00145         
00146     return Temp;
00147 } 
00148 
00149 void TH02_dev::TH02_IIC_WriteReg(uint8_t u8Reg,uint8_t u8Data)
00150 {           
00151     // Wire.beginTransmission(TH02_I2C_DEV_ID);  
00152     // Wire.write(u8Reg);    
00153     // Wire.write(u8Data);   
00154     // Wire.endTransmission();   
00155     char cmd[2];
00156     cmd[0] = u8Reg;
00157     cmd[1] = u8Data;
00158     i2cth02.write(TH02_I2C_DEV_ID, cmd, 2);
00159 }
00160 
00161 uint16_t TH02_dev::TH02_IIC_ReadData(void)
00162 {                        
00163     /* Port to arduino */    
00164     uint16_t Temp = TH02_IIC_ReadData2byte(); 
00165     return Temp;
00166 }
00167 
00168 uint16_t TH02_dev::TH02_IIC_ReadData2byte()
00169 {
00170     uint16_t TempData = 0;
00171     // uint16_t tmpArray[3]={0};
00172     char tmpArray[3];
00173 
00174     TH02_IIC_WriteCmd(REG_DATA_H);  
00175     
00176     // Wire.requestFrom(TH02_I2C_DEV_ID, 3);     
00177     // while(Wire.available())
00178     // {
00179     //     tmpArray[cnt] = (uint16_t)Wire.read();                       
00180     //  cnt++;
00181     // }
00182     i2cth02.read(TH02_I2C_DEV_ID, tmpArray, 3);
00183 
00184     /* MSB */
00185     TempData = (tmpArray[1]<<8)|(tmpArray[2]); 
00186     return TempData;
00187 }