20170911
Dependents: Hexi_Click_HDC1000 Hexi_Click_HDC1000_v2
Revision 0:2dee66cf94c2, committed 2017-09-11
- Comitter:
- i_am_kitsune
- Date:
- Mon Sep 11 02:06:47 2017 +0000
- Child:
- 1:383429056bd4
- Commit message:
- 20170911
Changed in this revision
| HDC1000.cpp | Show annotated file Show diff for this revision Revisions of this file |
| HDC1000.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HDC1000.cpp Mon Sep 11 02:06:47 2017 +0000
@@ -0,0 +1,138 @@
+/*
+ * mbed library program
+ * Low Power, High Accuracy Digital Humidity Sensor with Integrated Temperature Sensor
+ * HDC1000 Texas Instruments
+ *
+ * Copyright (c) 2015,'17 Kenji Arai / JH1PJL
+ * http://www.page.sannet.ne.jp/kenjia/index.html
+ * http://mbed.org/users/kenjiArai/
+ * Created: Feburary 9th, 2015
+ * Revised: AAugust 21st, 2017
+ */
+
+#include "HDC1000.h"
+
+HDC1000::HDC1000 (PinName p_sda, PinName p_scl) :
+ _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
+{
+ HDC1000_addr = HDC1000ADDR;
+ init();
+}
+
+HDC1000::HDC1000 (PinName p_sda, PinName p_scl, uint8_t addr) :
+ _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
+{
+ HDC1000_addr = addr;
+ init();
+}
+
+HDC1000::HDC1000 (I2C& p_i2c) : _i2c(p_i2c)
+{
+ HDC1000_addr = HDC1000ADDR;
+ init();
+}
+
+HDC1000::HDC1000 (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c)
+{
+ HDC1000_addr = addr;
+ init();
+}
+
+/////////////// Start conv. and gwt all data //////////////
+void HDC1000::get()
+{
+
+ dt[0] = HDC1000_REG_TEMP;
+ _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
+ wait_ms(15);
+ _i2c.read((int)HDC1000_addr, (char *)dt, 4, false);
+ temp = dt[0] << 8 | dt[1];
+ humi = dt[2] << 8 | dt[3];
+}
+
+/////////////// Read data from sensor /////////////////////
+float HDC1000::temperature()
+{
+ return (float)temp * 165 / 65536 - 40;
+}
+
+float HDC1000::conv_c_to_f()
+{
+ float tmp = temperature();
+ tmp = temp;
+ return (tmp * 1.8f) + 32.0f ;
+}
+
+
+/////////////// Read data from sensor /////////////////////
+float HDC1000::humidity()
+{
+ return (float)humi * 100 / 65536;
+}
+
+/////////////// Initialize ////////////////////////////////
+void HDC1000::init()
+{
+ _i2c.frequency(100000);
+ get_IDs();
+ set_config(BOTH_T_14_H_14);
+}
+
+/////////////// ID ////////////////////////////////////////
+uint16_t HDC1000::read_M_ID()
+{
+ return manufacturer_id_number;
+}
+
+uint16_t HDC1000::read_D_ID()
+{
+ return device_id_number;
+}
+
+uint8_t HDC1000::who_am_i()
+{
+ if (device_id_number == I_AM_HDC1000) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+/////////////// Configration //////////////////////////////
+uint16_t HDC1000::set_config(uint16_t cfg)
+{
+ dt[0] = HDC1000_REG_CONFIG;
+ dt[1] = (uint8_t)(cfg >> 8);
+ dt[2] = (uint8_t)(cfg & 0xff);
+ _i2c.write((int)HDC1000_addr, (char *)dt, 3, false);
+ return read_config();
+}
+
+uint16_t HDC1000::read_config(void)
+{
+ dt[0] = HDC1000_REG_CONFIG;
+ _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
+ _i2c.read(HDC1000_addr, (char *)dt, 2, false);
+ return (uint16_t)(dt[0] << 8 | dt[1]);
+}
+
+/////////////// I2C Freq. /////////////////////////////////
+void HDC1000::frequency(int hz)
+{
+ _i2c.frequency(hz);
+}
+
+/////////////// Read ID ///////////////////////////////////
+void HDC1000::get_IDs()
+{
+ // Manufacturer ID
+ dt[0] = HDC1000_REG_M_ID;
+ _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
+ _i2c.read(HDC1000_addr, (char *)dt, 2, false);
+ manufacturer_id_number = dt[0] << 8 | dt[1];
+ // Device ID
+ dt[0] = HDC1000_REG_D_ID;
+ _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
+ _i2c.read(HDC1000_addr, (char *)dt, 2, false);
+ device_id_number = dt[0] << 8 | dt[1];
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HDC1000.h Mon Sep 11 02:06:47 2017 +0000
@@ -0,0 +1,153 @@
+
+/*
+ * mbed library program
+ * Low Power, High Accuracy Digital Humidity Sensor with Integrated Temperature Sensor
+ * HDC1000 Texas Instruments
+ *
+ * Copyright (c) 2015,'17 Kenji Arai / JH1PJL
+ * http://www.page.sannet.ne.jp/kenjia/index.html
+ * http://mbed.org/users/kenjiArai/
+ * Created: Feburary 9th, 2015
+ * Revised: AAugust 21st, 2017
+ */
+/*
+ *---------------- REFERENCE ----------------------------------------------------------------------
+ * http://www.ti.com/product/HDC1000/description
+ * http://akizukidenshi.com/catalog/g/gM-08775/ (Not avairable now)
+ */
+
+#ifndef HDC1000_H
+#define HDC1000_H
+
+#include "mbed.h"
+
+// Humidity / Temperature Sensor, HDC1000 T.I.
+// Address b7=1,b6=0,b5=0,b4=0,b3=0,b2=0,b1=0, b0=R/W
+#define HDC1000ADDR (0x40 << 1) // CLICK BOARD
+
+
+////////////// Registers //////////////////////////////////
+// Register definition
+#define HDC1000_REG_TEMP 0x00
+#define HDC1000_REG_HUMI 0x01
+#define HDC1000_REG_CONFIG 0x02
+#define HDC1000_REG_S_ID_F 0xfb
+#define HDC1000_REG_S_ID_M 0xfc
+#define HDC1000_REG_S_ID_L 0xfd
+#define HDC1000_REG_M_ID 0xfe
+#define HDC1000_REG_D_ID 0xff
+
+////////////// ID /////////////////////////////////////////
+#define I_AM_HDC1000 0x1000
+#define DEV_REG_ID 0x5449
+
+////////////// Operating mode ///////////////////
+#define ACQ_MODE_SEPARETE (0UL << 12)
+#define ACQ_MODE_BOTH (1UL << 12)
+#define TRES_14BIT (0UL << 10)
+#define TRES_11BIT (1UL << 10)
+#define HRES_14BIT (0UL << 8)
+#define HRES_11BIT (1UL << 8)
+#define HRES_08BIT (2UL << 8)
+#define BOTH_T_14_H_14 (TRES_14BIT + HRES_14BIT + ACQ_MODE_BOTH)
+
+
+class HDC1000
+{
+public:
+ /** Configure data pin (with other devices on I2C line)
+ * @param data SDA and SCL pins
+ */
+ HDC1000(PinName p_sda, PinName p_scl);
+
+ /** Configure data pin (with other devices on I2C line)
+ * @param data SDA and SCL pins
+ * @param device address
+ */
+ HDC1000(PinName p_sda, PinName p_scl, uint8_t addr);
+
+ /** Configure data pin (with other devices on I2C line)
+ * @param I2C previous definition
+ */
+ HDC1000(I2C& p_i2c);
+ HDC1000(I2C& p_i2c, uint8_t addr);
+
+ /** Start convertion & data save
+ * @param none
+ * @return none
+ */
+ void get(void);
+
+ /** Read temperature data
+ * @param none
+ * @return temperature
+ */
+ float temperature(void);
+
+ float conv_c_to_f(void);
+
+ /** Read humidity data
+ * @param none
+ * @return humidity
+ */
+ float humidity(void);
+
+ /** HDC1000 Configuration
+ * @param none
+ * @return none
+ */
+ void config(void);
+
+ /** Read Configuration
+ * @param none
+ * @return config. data
+ */
+ uint16_t read_config(void);
+
+ /** Set config register
+ * @param config parameter
+ * @return config read data
+ */
+ uint16_t set_config(uint16_t cfg);
+
+ /** Set I2C clock frequency
+ * @param freq.
+ * @return none
+ */
+ void frequency(int hz);
+
+ /** check Device ID number
+ * @param none
+ * @return HDC1000 = 1, others 0
+ */
+ uint8_t who_am_i(void);
+
+ /** Read Manufacturer ID
+ * @param none
+ * @return ID
+ */
+ uint16_t read_M_ID(void);
+
+ /** Read Device ID
+ * @param none
+ * @return ID
+ */
+ uint16_t read_D_ID(void);
+
+protected:
+ I2C *_i2c_p;
+ I2C &_i2c;
+
+ void get_IDs(void);
+ void init(void);
+
+private:
+ uint8_t HDC1000_addr;
+ uint8_t dt[4];
+ uint16_t temp;
+ uint16_t humi;
+ uint16_t manufacturer_id_number;
+ uint16_t device_id_number;
+};
+
+#endif // HDC1000_H
\ No newline at end of file