Digital Humidity Sensor with Temperature Sensor by TI. Default address sets Akizuki kit module.

Dependents:   AmbientExampleLPC1768 HumidifierController_LPC824 TYBLE16_mbedlized_Thermometer

Committer:
kenjiArai
Date:
Sun Feb 22 01:04:44 2015 +0000
Revision:
0:927110422689
Child:
1:c8f2c01befe1
Digital Humidity Sensor with Temperature Sensor by TI. Default address sets Akizuki kit module.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:927110422689 1 /*
kenjiArai 0:927110422689 2 * mbed library program
kenjiArai 0:927110422689 3 * Low Power, High Accuracy Digital Humidity Sensor with Integrated Temperature Sensor
kenjiArai 0:927110422689 4 * HDC1000 Texas Instruments
kenjiArai 0:927110422689 5 *
kenjiArai 0:927110422689 6 * Copyright (c) 2015 Kenji Arai / JH1PJL
kenjiArai 0:927110422689 7 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 0:927110422689 8 * http://mbed.org/users/kenjiArai/
kenjiArai 0:927110422689 9 * Created: Feburary 9th, 2015
kenjiArai 0:927110422689 10 * Revised: Feburary 22nd, 2015
kenjiArai 0:927110422689 11 *
kenjiArai 0:927110422689 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
kenjiArai 0:927110422689 13 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
kenjiArai 0:927110422689 14 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kenjiArai 0:927110422689 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenjiArai 0:927110422689 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kenjiArai 0:927110422689 17 */
kenjiArai 0:927110422689 18
kenjiArai 0:927110422689 19 #include "HDC1000.h"
kenjiArai 0:927110422689 20
kenjiArai 0:927110422689 21 HDC1000::HDC1000 (PinName p_sda, PinName p_scl) : _i2c(p_sda, p_scl)
kenjiArai 0:927110422689 22 {
kenjiArai 0:927110422689 23 HDC1000_addr = HDC1000ADDR;
kenjiArai 0:927110422689 24 init();
kenjiArai 0:927110422689 25 }
kenjiArai 0:927110422689 26
kenjiArai 0:927110422689 27 HDC1000::HDC1000 (PinName p_sda, PinName p_scl, uint8_t addr) : _i2c(p_sda, p_scl)
kenjiArai 0:927110422689 28 {
kenjiArai 0:927110422689 29 HDC1000_addr = addr;
kenjiArai 0:927110422689 30 init();
kenjiArai 0:927110422689 31 }
kenjiArai 0:927110422689 32
kenjiArai 0:927110422689 33 HDC1000::HDC1000 (I2C& p_i2c) : _i2c(p_i2c)
kenjiArai 0:927110422689 34 {
kenjiArai 0:927110422689 35 HDC1000_addr = HDC1000ADDR;
kenjiArai 0:927110422689 36 init();
kenjiArai 0:927110422689 37 }
kenjiArai 0:927110422689 38
kenjiArai 0:927110422689 39 HDC1000::HDC1000 (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c)
kenjiArai 0:927110422689 40 {
kenjiArai 0:927110422689 41 HDC1000_addr = addr;
kenjiArai 0:927110422689 42 init();
kenjiArai 0:927110422689 43 }
kenjiArai 0:927110422689 44
kenjiArai 0:927110422689 45 /////////////// Start conv. and gwt all data //////////////
kenjiArai 0:927110422689 46 void HDC1000::get()
kenjiArai 0:927110422689 47 {
kenjiArai 0:927110422689 48
kenjiArai 0:927110422689 49 dt[0] = HDC1000_REG_TEMP;
kenjiArai 0:927110422689 50 _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
kenjiArai 0:927110422689 51 wait_ms(15);
kenjiArai 0:927110422689 52 _i2c.read((int)HDC1000_addr, (char *)dt, 4, false);
kenjiArai 0:927110422689 53 temp = dt[0] << 8 | dt[1];
kenjiArai 0:927110422689 54 humi = dt[2] << 8 | dt[3];
kenjiArai 0:927110422689 55 }
kenjiArai 0:927110422689 56
kenjiArai 0:927110422689 57 /////////////// Read data from sensor /////////////////////
kenjiArai 0:927110422689 58 float HDC1000::temperature()
kenjiArai 0:927110422689 59 {
kenjiArai 0:927110422689 60 return (float)temp * 165 / 65536 - 40;
kenjiArai 0:927110422689 61 }
kenjiArai 0:927110422689 62
kenjiArai 0:927110422689 63 /////////////// Read data from sensor /////////////////////
kenjiArai 0:927110422689 64 float HDC1000::humidity()
kenjiArai 0:927110422689 65 {
kenjiArai 0:927110422689 66 return (float)humi * 100 / 65536;
kenjiArai 0:927110422689 67 }
kenjiArai 0:927110422689 68
kenjiArai 0:927110422689 69 /////////////// Initialize ////////////////////////////////
kenjiArai 0:927110422689 70 void HDC1000::init()
kenjiArai 0:927110422689 71 {
kenjiArai 0:927110422689 72 _i2c.frequency(100000);
kenjiArai 0:927110422689 73 get_IDs();
kenjiArai 0:927110422689 74 set_config(BOTH_T_14_H_14);
kenjiArai 0:927110422689 75 }
kenjiArai 0:927110422689 76
kenjiArai 0:927110422689 77 /////////////// ID ////////////////////////////////////////
kenjiArai 0:927110422689 78 uint16_t HDC1000::read_M_ID()
kenjiArai 0:927110422689 79 {
kenjiArai 0:927110422689 80 return manufacturer_id_number;
kenjiArai 0:927110422689 81 }
kenjiArai 0:927110422689 82
kenjiArai 0:927110422689 83 uint16_t HDC1000::read_D_ID()
kenjiArai 0:927110422689 84 {
kenjiArai 0:927110422689 85 return device_id_number;
kenjiArai 0:927110422689 86 }
kenjiArai 0:927110422689 87
kenjiArai 0:927110422689 88 uint8_t HDC1000::who_am_i()
kenjiArai 0:927110422689 89 {
kenjiArai 0:927110422689 90 if (device_id_number == I_AM_HDC1000) {
kenjiArai 0:927110422689 91 return 1;
kenjiArai 0:927110422689 92 } else {
kenjiArai 0:927110422689 93 return 0;
kenjiArai 0:927110422689 94 }
kenjiArai 0:927110422689 95 }
kenjiArai 0:927110422689 96
kenjiArai 0:927110422689 97 /////////////// Configration //////////////////////////////
kenjiArai 0:927110422689 98 uint16_t HDC1000::set_config(uint16_t cfg)
kenjiArai 0:927110422689 99 {
kenjiArai 0:927110422689 100 dt[0] = HDC1000_REG_CONFIG;
kenjiArai 0:927110422689 101 dt[1] = (uint8_t)(cfg >> 8);
kenjiArai 0:927110422689 102 dt[2] = (uint8_t)(cfg & 0xff);
kenjiArai 0:927110422689 103 _i2c.write((int)HDC1000_addr, (char *)dt, 3, false);
kenjiArai 0:927110422689 104 return read_config();
kenjiArai 0:927110422689 105 }
kenjiArai 0:927110422689 106
kenjiArai 0:927110422689 107 uint16_t HDC1000::read_config(void)
kenjiArai 0:927110422689 108 {
kenjiArai 0:927110422689 109 dt[0] = HDC1000_REG_CONFIG;
kenjiArai 0:927110422689 110 _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
kenjiArai 0:927110422689 111 _i2c.read(HDC1000_addr, (char *)dt, 2, false);
kenjiArai 0:927110422689 112 return (uint16_t)(dt[0] << 8 | dt[1]);
kenjiArai 0:927110422689 113 }
kenjiArai 0:927110422689 114
kenjiArai 0:927110422689 115 /////////////// I2C Freq. /////////////////////////////////
kenjiArai 0:927110422689 116 void HDC1000::frequency(int hz)
kenjiArai 0:927110422689 117 {
kenjiArai 0:927110422689 118 _i2c.frequency(hz);
kenjiArai 0:927110422689 119 }
kenjiArai 0:927110422689 120
kenjiArai 0:927110422689 121 /////////////// Read ID ///////////////////////////////////
kenjiArai 0:927110422689 122 void HDC1000::get_IDs()
kenjiArai 0:927110422689 123 {
kenjiArai 0:927110422689 124 // Manufacturer ID
kenjiArai 0:927110422689 125 dt[0] = HDC1000_REG_M_ID;
kenjiArai 0:927110422689 126 _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
kenjiArai 0:927110422689 127 _i2c.read(HDC1000_addr, (char *)dt, 2, false);
kenjiArai 0:927110422689 128 manufacturer_id_number = dt[0] << 8 | dt[1];
kenjiArai 0:927110422689 129 // Device ID
kenjiArai 0:927110422689 130 dt[0] = HDC1000_REG_D_ID;
kenjiArai 0:927110422689 131 _i2c.write((int)HDC1000_addr, (char *)dt, 1, true);
kenjiArai 0:927110422689 132 _i2c.read(HDC1000_addr, (char *)dt, 2, false);
kenjiArai 0:927110422689 133 device_id_number = dt[0] << 8 | dt[1];
kenjiArai 0:927110422689 134 }