20170911
Dependents: Hexi_Click_HDC1000 Hexi_Click_HDC1000_v2
HDC1000.cpp@0:2dee66cf94c2, 2017-09-11 (annotated)
- Committer:
- i_am_kitsune
- Date:
- Mon Sep 11 02:06:47 2017 +0000
- Revision:
- 0:2dee66cf94c2
- Child:
- 1:383429056bd4
20170911
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
i_am_kitsune | 0:2dee66cf94c2 | 1 | /* |
i_am_kitsune | 0:2dee66cf94c2 | 2 | * mbed library program |
i_am_kitsune | 0:2dee66cf94c2 | 3 | * Low Power, High Accuracy Digital Humidity Sensor with Integrated Temperature Sensor |
i_am_kitsune | 0:2dee66cf94c2 | 4 | * HDC1000 Texas Instruments |
i_am_kitsune | 0:2dee66cf94c2 | 5 | * |
i_am_kitsune | 0:2dee66cf94c2 | 6 | * Copyright (c) 2015,'17 Kenji Arai / JH1PJL |
i_am_kitsune | 0:2dee66cf94c2 | 7 | * http://www.page.sannet.ne.jp/kenjia/index.html |
i_am_kitsune | 0:2dee66cf94c2 | 8 | * http://mbed.org/users/kenjiArai/ |
i_am_kitsune | 0:2dee66cf94c2 | 9 | * Created: Feburary 9th, 2015 |
i_am_kitsune | 0:2dee66cf94c2 | 10 | * Revised: AAugust 21st, 2017 |
i_am_kitsune | 0:2dee66cf94c2 | 11 | */ |
i_am_kitsune | 0:2dee66cf94c2 | 12 | |
i_am_kitsune | 0:2dee66cf94c2 | 13 | #include "HDC1000.h" |
i_am_kitsune | 0:2dee66cf94c2 | 14 | |
i_am_kitsune | 0:2dee66cf94c2 | 15 | HDC1000::HDC1000 (PinName p_sda, PinName p_scl) : |
i_am_kitsune | 0:2dee66cf94c2 | 16 | _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p) |
i_am_kitsune | 0:2dee66cf94c2 | 17 | { |
i_am_kitsune | 0:2dee66cf94c2 | 18 | HDC1000_addr = HDC1000ADDR; |
i_am_kitsune | 0:2dee66cf94c2 | 19 | init(); |
i_am_kitsune | 0:2dee66cf94c2 | 20 | } |
i_am_kitsune | 0:2dee66cf94c2 | 21 | |
i_am_kitsune | 0:2dee66cf94c2 | 22 | HDC1000::HDC1000 (PinName p_sda, PinName p_scl, uint8_t addr) : |
i_am_kitsune | 0:2dee66cf94c2 | 23 | _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p) |
i_am_kitsune | 0:2dee66cf94c2 | 24 | { |
i_am_kitsune | 0:2dee66cf94c2 | 25 | HDC1000_addr = addr; |
i_am_kitsune | 0:2dee66cf94c2 | 26 | init(); |
i_am_kitsune | 0:2dee66cf94c2 | 27 | } |
i_am_kitsune | 0:2dee66cf94c2 | 28 | |
i_am_kitsune | 0:2dee66cf94c2 | 29 | HDC1000::HDC1000 (I2C& p_i2c) : _i2c(p_i2c) |
i_am_kitsune | 0:2dee66cf94c2 | 30 | { |
i_am_kitsune | 0:2dee66cf94c2 | 31 | HDC1000_addr = HDC1000ADDR; |
i_am_kitsune | 0:2dee66cf94c2 | 32 | init(); |
i_am_kitsune | 0:2dee66cf94c2 | 33 | } |
i_am_kitsune | 0:2dee66cf94c2 | 34 | |
i_am_kitsune | 0:2dee66cf94c2 | 35 | HDC1000::HDC1000 (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c) |
i_am_kitsune | 0:2dee66cf94c2 | 36 | { |
i_am_kitsune | 0:2dee66cf94c2 | 37 | HDC1000_addr = addr; |
i_am_kitsune | 0:2dee66cf94c2 | 38 | init(); |
i_am_kitsune | 0:2dee66cf94c2 | 39 | } |
i_am_kitsune | 0:2dee66cf94c2 | 40 | |
i_am_kitsune | 0:2dee66cf94c2 | 41 | /////////////// Start conv. and gwt all data ////////////// |
i_am_kitsune | 0:2dee66cf94c2 | 42 | void HDC1000::get() |
i_am_kitsune | 0:2dee66cf94c2 | 43 | { |
i_am_kitsune | 0:2dee66cf94c2 | 44 | |
i_am_kitsune | 0:2dee66cf94c2 | 45 | dt[0] = HDC1000_REG_TEMP; |
i_am_kitsune | 0:2dee66cf94c2 | 46 | _i2c.write((int)HDC1000_addr, (char *)dt, 1, true); |
i_am_kitsune | 0:2dee66cf94c2 | 47 | wait_ms(15); |
i_am_kitsune | 0:2dee66cf94c2 | 48 | _i2c.read((int)HDC1000_addr, (char *)dt, 4, false); |
i_am_kitsune | 0:2dee66cf94c2 | 49 | temp = dt[0] << 8 | dt[1]; |
i_am_kitsune | 0:2dee66cf94c2 | 50 | humi = dt[2] << 8 | dt[3]; |
i_am_kitsune | 0:2dee66cf94c2 | 51 | } |
i_am_kitsune | 0:2dee66cf94c2 | 52 | |
i_am_kitsune | 0:2dee66cf94c2 | 53 | /////////////// Read data from sensor ///////////////////// |
i_am_kitsune | 0:2dee66cf94c2 | 54 | float HDC1000::temperature() |
i_am_kitsune | 0:2dee66cf94c2 | 55 | { |
i_am_kitsune | 0:2dee66cf94c2 | 56 | return (float)temp * 165 / 65536 - 40; |
i_am_kitsune | 0:2dee66cf94c2 | 57 | } |
i_am_kitsune | 0:2dee66cf94c2 | 58 | |
i_am_kitsune | 0:2dee66cf94c2 | 59 | float HDC1000::conv_c_to_f() |
i_am_kitsune | 0:2dee66cf94c2 | 60 | { |
i_am_kitsune | 0:2dee66cf94c2 | 61 | float tmp = temperature(); |
i_am_kitsune | 0:2dee66cf94c2 | 62 | tmp = temp; |
i_am_kitsune | 0:2dee66cf94c2 | 63 | return (tmp * 1.8f) + 32.0f ; |
i_am_kitsune | 0:2dee66cf94c2 | 64 | } |
i_am_kitsune | 0:2dee66cf94c2 | 65 | |
i_am_kitsune | 0:2dee66cf94c2 | 66 | |
i_am_kitsune | 0:2dee66cf94c2 | 67 | /////////////// Read data from sensor ///////////////////// |
i_am_kitsune | 0:2dee66cf94c2 | 68 | float HDC1000::humidity() |
i_am_kitsune | 0:2dee66cf94c2 | 69 | { |
i_am_kitsune | 0:2dee66cf94c2 | 70 | return (float)humi * 100 / 65536; |
i_am_kitsune | 0:2dee66cf94c2 | 71 | } |
i_am_kitsune | 0:2dee66cf94c2 | 72 | |
i_am_kitsune | 0:2dee66cf94c2 | 73 | /////////////// Initialize //////////////////////////////// |
i_am_kitsune | 0:2dee66cf94c2 | 74 | void HDC1000::init() |
i_am_kitsune | 0:2dee66cf94c2 | 75 | { |
i_am_kitsune | 0:2dee66cf94c2 | 76 | _i2c.frequency(100000); |
i_am_kitsune | 0:2dee66cf94c2 | 77 | get_IDs(); |
i_am_kitsune | 0:2dee66cf94c2 | 78 | set_config(BOTH_T_14_H_14); |
i_am_kitsune | 0:2dee66cf94c2 | 79 | } |
i_am_kitsune | 0:2dee66cf94c2 | 80 | |
i_am_kitsune | 0:2dee66cf94c2 | 81 | /////////////// ID //////////////////////////////////////// |
i_am_kitsune | 0:2dee66cf94c2 | 82 | uint16_t HDC1000::read_M_ID() |
i_am_kitsune | 0:2dee66cf94c2 | 83 | { |
i_am_kitsune | 0:2dee66cf94c2 | 84 | return manufacturer_id_number; |
i_am_kitsune | 0:2dee66cf94c2 | 85 | } |
i_am_kitsune | 0:2dee66cf94c2 | 86 | |
i_am_kitsune | 0:2dee66cf94c2 | 87 | uint16_t HDC1000::read_D_ID() |
i_am_kitsune | 0:2dee66cf94c2 | 88 | { |
i_am_kitsune | 0:2dee66cf94c2 | 89 | return device_id_number; |
i_am_kitsune | 0:2dee66cf94c2 | 90 | } |
i_am_kitsune | 0:2dee66cf94c2 | 91 | |
i_am_kitsune | 0:2dee66cf94c2 | 92 | uint8_t HDC1000::who_am_i() |
i_am_kitsune | 0:2dee66cf94c2 | 93 | { |
i_am_kitsune | 0:2dee66cf94c2 | 94 | if (device_id_number == I_AM_HDC1000) { |
i_am_kitsune | 0:2dee66cf94c2 | 95 | return 1; |
i_am_kitsune | 0:2dee66cf94c2 | 96 | } else { |
i_am_kitsune | 0:2dee66cf94c2 | 97 | return 0; |
i_am_kitsune | 0:2dee66cf94c2 | 98 | } |
i_am_kitsune | 0:2dee66cf94c2 | 99 | } |
i_am_kitsune | 0:2dee66cf94c2 | 100 | |
i_am_kitsune | 0:2dee66cf94c2 | 101 | /////////////// Configration ////////////////////////////// |
i_am_kitsune | 0:2dee66cf94c2 | 102 | uint16_t HDC1000::set_config(uint16_t cfg) |
i_am_kitsune | 0:2dee66cf94c2 | 103 | { |
i_am_kitsune | 0:2dee66cf94c2 | 104 | dt[0] = HDC1000_REG_CONFIG; |
i_am_kitsune | 0:2dee66cf94c2 | 105 | dt[1] = (uint8_t)(cfg >> 8); |
i_am_kitsune | 0:2dee66cf94c2 | 106 | dt[2] = (uint8_t)(cfg & 0xff); |
i_am_kitsune | 0:2dee66cf94c2 | 107 | _i2c.write((int)HDC1000_addr, (char *)dt, 3, false); |
i_am_kitsune | 0:2dee66cf94c2 | 108 | return read_config(); |
i_am_kitsune | 0:2dee66cf94c2 | 109 | } |
i_am_kitsune | 0:2dee66cf94c2 | 110 | |
i_am_kitsune | 0:2dee66cf94c2 | 111 | uint16_t HDC1000::read_config(void) |
i_am_kitsune | 0:2dee66cf94c2 | 112 | { |
i_am_kitsune | 0:2dee66cf94c2 | 113 | dt[0] = HDC1000_REG_CONFIG; |
i_am_kitsune | 0:2dee66cf94c2 | 114 | _i2c.write((int)HDC1000_addr, (char *)dt, 1, true); |
i_am_kitsune | 0:2dee66cf94c2 | 115 | _i2c.read(HDC1000_addr, (char *)dt, 2, false); |
i_am_kitsune | 0:2dee66cf94c2 | 116 | return (uint16_t)(dt[0] << 8 | dt[1]); |
i_am_kitsune | 0:2dee66cf94c2 | 117 | } |
i_am_kitsune | 0:2dee66cf94c2 | 118 | |
i_am_kitsune | 0:2dee66cf94c2 | 119 | /////////////// I2C Freq. ///////////////////////////////// |
i_am_kitsune | 0:2dee66cf94c2 | 120 | void HDC1000::frequency(int hz) |
i_am_kitsune | 0:2dee66cf94c2 | 121 | { |
i_am_kitsune | 0:2dee66cf94c2 | 122 | _i2c.frequency(hz); |
i_am_kitsune | 0:2dee66cf94c2 | 123 | } |
i_am_kitsune | 0:2dee66cf94c2 | 124 | |
i_am_kitsune | 0:2dee66cf94c2 | 125 | /////////////// Read ID /////////////////////////////////// |
i_am_kitsune | 0:2dee66cf94c2 | 126 | void HDC1000::get_IDs() |
i_am_kitsune | 0:2dee66cf94c2 | 127 | { |
i_am_kitsune | 0:2dee66cf94c2 | 128 | // Manufacturer ID |
i_am_kitsune | 0:2dee66cf94c2 | 129 | dt[0] = HDC1000_REG_M_ID; |
i_am_kitsune | 0:2dee66cf94c2 | 130 | _i2c.write((int)HDC1000_addr, (char *)dt, 1, true); |
i_am_kitsune | 0:2dee66cf94c2 | 131 | _i2c.read(HDC1000_addr, (char *)dt, 2, false); |
i_am_kitsune | 0:2dee66cf94c2 | 132 | manufacturer_id_number = dt[0] << 8 | dt[1]; |
i_am_kitsune | 0:2dee66cf94c2 | 133 | // Device ID |
i_am_kitsune | 0:2dee66cf94c2 | 134 | dt[0] = HDC1000_REG_D_ID; |
i_am_kitsune | 0:2dee66cf94c2 | 135 | _i2c.write((int)HDC1000_addr, (char *)dt, 1, true); |
i_am_kitsune | 0:2dee66cf94c2 | 136 | _i2c.read(HDC1000_addr, (char *)dt, 2, false); |
i_am_kitsune | 0:2dee66cf94c2 | 137 | device_id_number = dt[0] << 8 | dt[1]; |
i_am_kitsune | 0:2dee66cf94c2 | 138 | } |