20170911

Dependents:   Hexi_Click_HDC1000 Hexi_Click_HDC1000_v2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HDC1000.cpp Source File

HDC1000.cpp

00001 /*
00002  *  mbed library program
00003  *  Low Power, High Accuracy Digital Humidity Sensor with Integrated Temperature Sensor
00004  *  HDC1000 Texas Instruments
00005  *
00006  * Copyright (c) 2015,'17 Kenji Arai / JH1PJL
00007  *  http://www.page.sannet.ne.jp/kenjia/index.html
00008  *  http://mbed.org/users/kenjiArai/
00009  *      Created: Feburary   9th, 2015
00010  *      Revised: AAugust   21st, 2017
00011  */
00012 
00013 #include "HDC1000.h"
00014 
00015 HDC1000::HDC1000 (PinName p_sda, PinName p_scl) :
00016  _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
00017 {
00018     HDC1000_addr = HDC1000ADDR;
00019     init();
00020 }
00021 
00022 HDC1000::HDC1000 (PinName p_sda, PinName p_scl, uint8_t addr) :
00023  _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
00024 {
00025     HDC1000_addr = addr;
00026     init();
00027 }
00028 
00029 HDC1000::HDC1000 (I2C& p_i2c) : _i2c(p_i2c)
00030 {
00031     HDC1000_addr = HDC1000ADDR;
00032     init();
00033 }
00034 
00035 HDC1000::HDC1000 (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c)
00036 {
00037     HDC1000_addr = addr;
00038     init();
00039 }
00040 
00041 /////////////// Start conv. and gwt all data //////////////
00042 void HDC1000::get()
00043 {
00044 
00045     dt[0] = HDC1000_REG_TEMP;
00046     _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
00047     wait_ms(15);
00048     _i2c.read((int)HDC1000_addr, (char *)dt, 4, false);
00049     temp = dt[0] << 8 | dt[1];
00050     humi = dt[2] << 8 | dt[3];
00051 }
00052 
00053 /////////////// Read data from sensor /////////////////////
00054 float HDC1000::temperature()
00055 {
00056     return (float)temp * 165 / 65536 - 40;
00057 }
00058 
00059 float HDC1000::conv_c_to_f()
00060 {
00061     float tmp = temperature();
00062     tmp = temp;
00063     return (tmp * 1.8f) + 32.0f ;
00064 }
00065 
00066 uint16_t HDC1000::send_temp()
00067 {
00068     float tmp = temperature();
00069     tmp = temp;
00070     return (uint16_t)((tmp * 1.8f) + 32.0f) ;
00071 }
00072 
00073 uint16_t HDC1000::send_humi()
00074 {
00075     float tmp = humidity();
00076     tmp = humi;
00077     return (uint16_t)((tmp * 100.0f) / 65536.0f) ;
00078 }
00079 
00080 /////////////// Read data from sensor /////////////////////
00081 float HDC1000::humidity()
00082 {
00083     return (float)humi * 100 / 65536;
00084 }
00085 
00086 /////////////// Initialize ////////////////////////////////
00087 void HDC1000::init()
00088 {
00089     _i2c.frequency(100000);
00090     get_IDs();
00091     set_config(BOTH_T_14_H_14);
00092 }
00093 
00094 /////////////// ID ////////////////////////////////////////
00095 uint16_t HDC1000::read_M_ID()
00096 {
00097     return manufacturer_id_number;
00098 }
00099 
00100 uint16_t HDC1000::read_D_ID()
00101 {
00102     return device_id_number;
00103 }
00104 
00105 uint8_t HDC1000::who_am_i()
00106 {
00107     if (device_id_number == I_AM_HDC1000) {
00108         return 1;
00109     } else {
00110         return 0;
00111     }
00112 }
00113 
00114 /////////////// Configration //////////////////////////////
00115 uint16_t HDC1000::set_config(uint16_t cfg)
00116 {
00117     dt[0] = HDC1000_REG_CONFIG;
00118     dt[1] = (uint8_t)(cfg >> 8);
00119     dt[2] = (uint8_t)(cfg & 0xff);
00120     _i2c.write((int)HDC1000_addr, (char *)dt, 3, false);
00121     return read_config();
00122 }
00123 
00124 uint16_t HDC1000::read_config(void)
00125 {
00126     dt[0] = HDC1000_REG_CONFIG;
00127     _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
00128     _i2c.read(HDC1000_addr, (char *)dt, 2, false);
00129     return (uint16_t)(dt[0] << 8 | dt[1]);
00130 }
00131 
00132 /////////////// I2C Freq. /////////////////////////////////
00133 void HDC1000::frequency(int hz)
00134 {
00135     _i2c.frequency(hz);
00136 }
00137 
00138 /////////////// Read ID ///////////////////////////////////
00139 void HDC1000::get_IDs()
00140 {
00141     // Manufacturer ID
00142     dt[0] = HDC1000_REG_M_ID;
00143     _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
00144     _i2c.read(HDC1000_addr, (char *)dt, 2, false);
00145     manufacturer_id_number = dt[0] << 8 | dt[1];
00146     // Device ID
00147     dt[0] = HDC1000_REG_D_ID;
00148     _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
00149     _i2c.read(HDC1000_addr, (char *)dt, 2, false);
00150     device_id_number = dt[0] << 8 | dt[1];
00151 }