interface library to support TI hdc1080 humidity and temperature sensor
Dependents: xj-Nucleo-F303K8-hdc1080-test HelloWorld_NFC02A1laatste
hdc1080.h@1:dd3a07b44fe2, 2016-09-14 (annotated)
- Committer:
- joeata2wh
- Date:
- Wed Sep 14 02:31:15 2016 +0000
- Revision:
- 1:dd3a07b44fe2
- Parent:
- 0:36666c79a60f
tested working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joeata2wh | 0:36666c79a60f | 1 | /* |
joeata2wh | 1:dd3a07b44fe2 | 2 | By Joseph Ellsworth CTO of A2WH |
joeata2wh | 1:dd3a07b44fe2 | 3 | Take a look at A2WH.com Producing Water from Air using Solar Energy |
joeata2wh | 1:dd3a07b44fe2 | 4 | March-2016 License: https://developer.mbed.org/handbook/MIT-Licence |
joeata2wh | 1:dd3a07b44fe2 | 5 | Please contact us http://a2wh.com for help with custom design projects. |
joeata2wh | 0:36666c79a60f | 6 | |
joeata2wh | 1:dd3a07b44fe2 | 7 | See Also: |
joeata2wh | 0:36666c79a60f | 8 | http://www.ti.com/lit/ds/symlink/hdc1080.pdf |
joeata2wh | 0:36666c79a60f | 9 | https://github.com/closedcube/ClosedCube_HDC1080_Arduino/blob/master/src/ClosedCube_HDC1080.cpp |
joeata2wh | 0:36666c79a60f | 10 | http://e.pavlin.si/2016/06/04/hdlc-like-data-link-via-rs485/ |
joeata2wh | 1:dd3a07b44fe2 | 11 | */ |
joeata2wh | 0:36666c79a60f | 12 | |
joeata2wh | 1:dd3a07b44fe2 | 13 | /** |
joeata2wh | 1:dd3a07b44fe2 | 14 | #include "mbed.h" |
joeata2wh | 1:dd3a07b44fe2 | 15 | #include <stdint.h> |
joeata2wh | 1:dd3a07b44fe2 | 16 | |
joeata2wh | 1:dd3a07b44fe2 | 17 | //Pin Defines for I2C Bus |
joeata2wh | 1:dd3a07b44fe2 | 18 | #define D_SDA PB_7 // specific for Nucleo-F303K8 |
joeata2wh | 1:dd3a07b44fe2 | 19 | #define D_SCL PB_6 // specific for Nucleo-F303K8 |
joeata2wh | 1:dd3a07b44fe2 | 20 | I2C hdc_i2c(D_SDA, D_SCL); |
joeata2wh | 1:dd3a07b44fe2 | 21 | #include "hdc1080.h" |
joeata2wh | 1:dd3a07b44fe2 | 22 | |
joeata2wh | 1:dd3a07b44fe2 | 23 | // Host PC Communication channels |
joeata2wh | 1:dd3a07b44fe2 | 24 | Serial pc(USBTX, USBRX); // tx, rx |
joeata2wh | 1:dd3a07b44fe2 | 25 | DigitalOut myled(LED1); |
joeata2wh | 1:dd3a07b44fe2 | 26 | |
joeata2wh | 1:dd3a07b44fe2 | 27 | int main() |
joeata2wh | 1:dd3a07b44fe2 | 28 | { |
joeata2wh | 1:dd3a07b44fe2 | 29 | pc.baud(9600); |
joeata2wh | 1:dd3a07b44fe2 | 30 | while(1) { |
joeata2wh | 1:dd3a07b44fe2 | 31 | printf("\r\\nHDC1080 Test\r\n"); |
joeata2wh | 1:dd3a07b44fe2 | 32 | myled = !myled; |
joeata2wh | 1:dd3a07b44fe2 | 33 | hdc_begin(); |
joeata2wh | 1:dd3a07b44fe2 | 34 | uint16_t manId = hdc_readManufactId(); |
joeata2wh | 1:dd3a07b44fe2 | 35 | float tempC = hdc_readTemp(); |
joeata2wh | 1:dd3a07b44fe2 | 36 | float humid = hdc_readHumid(); |
joeata2wh | 1:dd3a07b44fe2 | 37 | unsigned long serNum = hdc_readSerial(); |
joeata2wh | 1:dd3a07b44fe2 | 38 | printf("manId=x%x, tempC=%0.3f humid=%0.3f serfNum=%ld\r\n", |
joeata2wh | 1:dd3a07b44fe2 | 39 | manId, tempC, humid, serNum); |
joeata2wh | 1:dd3a07b44fe2 | 40 | wait(3.0f); |
joeata2wh | 1:dd3a07b44fe2 | 41 | } |
joeata2wh | 1:dd3a07b44fe2 | 42 | } |
joeata2wh | 1:dd3a07b44fe2 | 43 | **/ |
joeata2wh | 1:dd3a07b44fe2 | 44 | |
joeata2wh | 1:dd3a07b44fe2 | 45 | |
joeata2wh | 0:36666c79a60f | 46 | |
joeata2wh | 0:36666c79a60f | 47 | #ifndef hdc1080_h |
joeata2wh | 0:36666c79a60f | 48 | #define hdc1080_h |
joeata2wh | 0:36666c79a60f | 49 | #include "mbed.h" |
joeata2wh | 0:36666c79a60f | 50 | |
joeata2wh | 1:dd3a07b44fe2 | 51 | #define hdc_chip_addr (0x40 << 1) // Page #8.5.1.1 - 1000000 |
joeata2wh | 1:dd3a07b44fe2 | 52 | // left shift 1 bit for 7 bit address required by |
joeata2wh | 1:dd3a07b44fe2 | 53 | // I2C library |
joeata2wh | 1:dd3a07b44fe2 | 54 | //#define hdc_chip_addr 0x40 // Page #8.5.1.1 - 1000000 |
joeata2wh | 1:dd3a07b44fe2 | 55 | const int hdc_off_temp = 0x00; |
joeata2wh | 1:dd3a07b44fe2 | 56 | const int hdc_off_humid = 0x01; |
joeata2wh | 1:dd3a07b44fe2 | 57 | const int hdc_off_config = 0x02; |
joeata2wh | 1:dd3a07b44fe2 | 58 | const int hdc_off_man_id = 0xFE; |
joeata2wh | 1:dd3a07b44fe2 | 59 | const int hdc_off_serial_first = 0xFB; |
joeata2wh | 1:dd3a07b44fe2 | 60 | const int hdc_off_serial_mid = 0xFC; |
joeata2wh | 1:dd3a07b44fe2 | 61 | const int hdc_off_serial_last = 0xFD; |
joeata2wh | 1:dd3a07b44fe2 | 62 | char hdc_comm = hdc_off_man_id; |
joeata2wh | 1:dd3a07b44fe2 | 63 | const float hdc_chip_error = -255; |
joeata2wh | 1:dd3a07b44fe2 | 64 | const unsigned long hdc_chip_err_l = 0; |
joeata2wh | 1:dd3a07b44fe2 | 65 | char hdc_buff[5]; |
joeata2wh | 1:dd3a07b44fe2 | 66 | |
joeata2wh | 1:dd3a07b44fe2 | 67 | void hdc_begin() |
joeata2wh | 1:dd3a07b44fe2 | 68 | { |
joeata2wh | 1:dd3a07b44fe2 | 69 | //printf("hdc_begin\r\n"); |
joeata2wh | 1:dd3a07b44fe2 | 70 | memset(hdc_buff,0,3); |
joeata2wh | 1:dd3a07b44fe2 | 71 | hdc_buff[0] = hdc_off_config; |
joeata2wh | 1:dd3a07b44fe2 | 72 | int res = hdc_i2c.write(hdc_chip_addr, hdc_buff, 2); |
joeata2wh | 1:dd3a07b44fe2 | 73 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 74 | printf("hdc_begin write res=%d\r\n", res); |
joeata2wh | 1:dd3a07b44fe2 | 75 | #endif |
joeata2wh | 0:36666c79a60f | 76 | } |
joeata2wh | 1:dd3a07b44fe2 | 77 | |
joeata2wh | 1:dd3a07b44fe2 | 78 | uint16_t hdc_readData16(int chip_addr, int offset) |
joeata2wh | 1:dd3a07b44fe2 | 79 | { |
joeata2wh | 1:dd3a07b44fe2 | 80 | memset(hdc_buff,0,3); |
joeata2wh | 1:dd3a07b44fe2 | 81 | // send chip address onto buss |
joeata2wh | 1:dd3a07b44fe2 | 82 | hdc_buff[0] = offset; |
joeata2wh | 1:dd3a07b44fe2 | 83 | int res = hdc_i2c.write(chip_addr, hdc_buff, 1); |
joeata2wh | 1:dd3a07b44fe2 | 84 | if (res != 0) { |
joeata2wh | 1:dd3a07b44fe2 | 85 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 86 | printf("error talking to chip %d offst=%d\r\n", chip_addr, offset); |
joeata2wh | 1:dd3a07b44fe2 | 87 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 88 | return 0; |
joeata2wh | 1:dd3a07b44fe2 | 89 | } |
joeata2wh | 1:dd3a07b44fe2 | 90 | // read data from chip |
joeata2wh | 1:dd3a07b44fe2 | 91 | wait(0.015); |
joeata2wh | 1:dd3a07b44fe2 | 92 | memset(hdc_buff,0,3); |
joeata2wh | 1:dd3a07b44fe2 | 93 | res = hdc_i2c.read(hdc_chip_addr, hdc_buff,2); |
joeata2wh | 1:dd3a07b44fe2 | 94 | if (res != 0) { |
joeata2wh | 1:dd3a07b44fe2 | 95 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 96 | printf("error reading chip %d offst=%d\r\n", chip_addr, offset); |
joeata2wh | 1:dd3a07b44fe2 | 97 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 98 | return 0; |
joeata2wh | 1:dd3a07b44fe2 | 99 | } |
joeata2wh | 1:dd3a07b44fe2 | 100 | return hdc_buff[0] << 8 | hdc_buff[1]; |
joeata2wh | 0:36666c79a60f | 101 | } |
joeata2wh | 0:36666c79a60f | 102 | |
joeata2wh | 1:dd3a07b44fe2 | 103 | /* Read temperature from hdc_1080 chip. Returns float |
joeata2wh | 1:dd3a07b44fe2 | 104 | containing the Celcius temperature or hdc_chip_error if |
joeata2wh | 1:dd3a07b44fe2 | 105 | error occurs reading the sensor */ |
joeata2wh | 1:dd3a07b44fe2 | 106 | float hdc_readTemp() |
joeata2wh | 1:dd3a07b44fe2 | 107 | { |
joeata2wh | 1:dd3a07b44fe2 | 108 | uint16_t rawT = hdc_readData16(hdc_chip_addr, hdc_off_temp); |
joeata2wh | 1:dd3a07b44fe2 | 109 | if (rawT == 0) { |
joeata2wh | 1:dd3a07b44fe2 | 110 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 111 | printf("error reading hdc chip temp\r\n"); |
joeata2wh | 1:dd3a07b44fe2 | 112 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 113 | return hdc_chip_error; |
joeata2wh | 1:dd3a07b44fe2 | 114 | } else { |
joeata2wh | 1:dd3a07b44fe2 | 115 | float temp = ((float) rawT / pow(2.0f, 16.0f)) * 165.0f - 40.0f; |
joeata2wh | 1:dd3a07b44fe2 | 116 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 117 | printf("temp=%0.3f\r\n", temp); |
joeata2wh | 1:dd3a07b44fe2 | 118 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 119 | return temp; |
joeata2wh | 1:dd3a07b44fe2 | 120 | } |
joeata2wh | 1:dd3a07b44fe2 | 121 | } |
joeata2wh | 1:dd3a07b44fe2 | 122 | |
joeata2wh | 1:dd3a07b44fe2 | 123 | /* Read humidity from hdc_1080 chip. Returns a float |
joeata2wh | 1:dd3a07b44fe2 | 124 | containing the humidity or hdc_chip_error if error |
joeata2wh | 1:dd3a07b44fe2 | 125 | occurs reading the sensor */ |
joeata2wh | 1:dd3a07b44fe2 | 126 | float hdc_readHumid() |
joeata2wh | 1:dd3a07b44fe2 | 127 | { |
joeata2wh | 1:dd3a07b44fe2 | 128 | uint16_t rawH = hdc_readData16(hdc_chip_addr, hdc_off_humid); |
joeata2wh | 1:dd3a07b44fe2 | 129 | if (rawH == 0) { |
joeata2wh | 1:dd3a07b44fe2 | 130 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 131 | printf("error reading hdc chip humid\r\n"); |
joeata2wh | 1:dd3a07b44fe2 | 132 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 133 | return hdc_chip_error; |
joeata2wh | 1:dd3a07b44fe2 | 134 | } else { |
joeata2wh | 1:dd3a07b44fe2 | 135 | float humid = ((float) rawH / pow(2.0f, 16.0f)) * 100.0f; |
joeata2wh | 1:dd3a07b44fe2 | 136 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 137 | printf("humid humid=%0.3f\r\n", humid); |
joeata2wh | 1:dd3a07b44fe2 | 138 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 139 | return humid; |
joeata2wh | 1:dd3a07b44fe2 | 140 | } |
joeata2wh | 0:36666c79a60f | 141 | } |
joeata2wh | 0:36666c79a60f | 142 | |
joeata2wh | 1:dd3a07b44fe2 | 143 | int hdc_readManufactId() |
joeata2wh | 1:dd3a07b44fe2 | 144 | { |
joeata2wh | 1:dd3a07b44fe2 | 145 | uint16_t rawid = hdc_readData16(hdc_chip_addr, hdc_off_man_id); |
joeata2wh | 1:dd3a07b44fe2 | 146 | if (rawid == 0) { |
joeata2wh | 1:dd3a07b44fe2 | 147 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 148 | printf("error reading hdc chip manId\r\n"); |
joeata2wh | 1:dd3a07b44fe2 | 149 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 150 | return (int) hdc_chip_error; |
joeata2wh | 1:dd3a07b44fe2 | 151 | } else { |
joeata2wh | 1:dd3a07b44fe2 | 152 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 153 | printf("man id=%x\r\n", (int) rawid); |
joeata2wh | 1:dd3a07b44fe2 | 154 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 155 | return rawid; |
joeata2wh | 1:dd3a07b44fe2 | 156 | } |
joeata2wh | 1:dd3a07b44fe2 | 157 | } |
joeata2wh | 1:dd3a07b44fe2 | 158 | |
joeata2wh | 1:dd3a07b44fe2 | 159 | unsigned long hdc_readSerial() |
joeata2wh | 1:dd3a07b44fe2 | 160 | { |
joeata2wh | 1:dd3a07b44fe2 | 161 | wait(0.015); |
joeata2wh | 1:dd3a07b44fe2 | 162 | memset(hdc_buff,0,4); |
joeata2wh | 1:dd3a07b44fe2 | 163 | hdc_buff[0] = hdc_off_man_id; |
joeata2wh | 1:dd3a07b44fe2 | 164 | int res = hdc_i2c.write(hdc_chip_addr, hdc_buff, 1); |
joeata2wh | 1:dd3a07b44fe2 | 165 | if (res != 0) { |
joeata2wh | 1:dd3a07b44fe2 | 166 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 167 | printf("Error writing chip addr res=%d\r\n", res); |
joeata2wh | 1:dd3a07b44fe2 | 168 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 169 | return (unsigned long) hdc_chip_err_l; |
joeata2wh | 1:dd3a07b44fe2 | 170 | } |
joeata2wh | 1:dd3a07b44fe2 | 171 | |
joeata2wh | 1:dd3a07b44fe2 | 172 | wait(0.015); |
joeata2wh | 1:dd3a07b44fe2 | 173 | memset(hdc_buff,0,4); |
joeata2wh | 1:dd3a07b44fe2 | 174 | res = hdc_i2c.read(hdc_chip_addr, hdc_buff,4); |
joeata2wh | 1:dd3a07b44fe2 | 175 | if (res != 0) { |
joeata2wh | 1:dd3a07b44fe2 | 176 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 177 | printf("Errot reading chip serial res=%d#\r\n", res); |
joeata2wh | 1:dd3a07b44fe2 | 178 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 179 | return (unsigned long) hdc_chip_err_l; |
joeata2wh | 1:dd3a07b44fe2 | 180 | } |
joeata2wh | 1:dd3a07b44fe2 | 181 | |
joeata2wh | 1:dd3a07b44fe2 | 182 | unsigned long rawser = hdc_buff[0] << 16 | hdc_buff[1] << 8 | hdc_buff[0]; |
joeata2wh | 1:dd3a07b44fe2 | 183 | #ifdef DEBUG3 |
joeata2wh | 1:dd3a07b44fe2 | 184 | printf("ser=%lu\r\n", rawser); |
joeata2wh | 1:dd3a07b44fe2 | 185 | #endif |
joeata2wh | 1:dd3a07b44fe2 | 186 | return rawser; |
joeata2wh | 0:36666c79a60f | 187 | } |
joeata2wh | 0:36666c79a60f | 188 | |
joeata2wh | 0:36666c79a60f | 189 | #endif |