HDC1080 sensor library
HDC1080.cpp@2:b913d2690215, 2019-11-08 (annotated)
- Committer:
- MACRUM
- Date:
- Fri Nov 08 05:08:12 2019 +0000
- Revision:
- 2:b913d2690215
- Parent:
- 1:a0e46d956969
- Child:
- 3:d7627c8dcfe3
Add thread_sleep_for calls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shivanandgowdakr | 0:fdb750cc9ca8 | 1 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 2 | #include "mbed.h" |
shivanandgowdakr | 0:fdb750cc9ca8 | 3 | #include "HDC1080.h" |
shivanandgowdakr | 0:fdb750cc9ca8 | 4 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 5 | #define HDC_TEMP_OFF 0x00 |
shivanandgowdakr | 0:fdb750cc9ca8 | 6 | #define HDC_HUMID_OFF 0x01 |
shivanandgowdakr | 0:fdb750cc9ca8 | 7 | #define HDC_CONFIG_OFF 0x02 |
shivanandgowdakr | 0:fdb750cc9ca8 | 8 | #define HDC_MANID_OFF 0xFE |
shivanandgowdakr | 0:fdb750cc9ca8 | 9 | #define HDC_SER_OFF_FIRST 0xFB |
shivanandgowdakr | 0:fdb750cc9ca8 | 10 | #define HDC_SER_OFF_MID 0xFC |
shivanandgowdakr | 0:fdb750cc9ca8 | 11 | #define HDC_SER_OFF_LAST 0xFD |
shivanandgowdakr | 0:fdb750cc9ca8 | 12 | #define I2C_FREQ 100000 |
MACRUM | 1:a0e46d956969 | 13 | #define CHIP_ADDRESS (0x40 << 1) |
shivanandgowdakr | 0:fdb750cc9ca8 | 14 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 15 | const float HDC_CHIP_ERROR = -255; |
shivanandgowdakr | 0:fdb750cc9ca8 | 16 | const unsigned long HDC_CHIP_SER_ERROR = 0; |
shivanandgowdakr | 0:fdb750cc9ca8 | 17 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 18 | |
MACRUM | 1:a0e46d956969 | 19 | HDC1080::HDC1080(PinName sda, PinName slc) : I2C(sda, slc) |
shivanandgowdakr | 0:fdb750cc9ca8 | 20 | { |
MACRUM | 1:a0e46d956969 | 21 | memset(_buffer,'\0',5); |
MACRUM | 1:a0e46d956969 | 22 | _buffer[0] = HDC_CONFIG_OFF; |
shivanandgowdakr | 0:fdb750cc9ca8 | 23 | this->frequency(I2C_FREQ); |
MACRUM | 1:a0e46d956969 | 24 | int res = this->write(CHIP_ADDRESS, _buffer, 2); |
shivanandgowdakr | 0:fdb750cc9ca8 | 25 | } |
shivanandgowdakr | 0:fdb750cc9ca8 | 26 | |
MACRUM | 1:a0e46d956969 | 27 | int HDC1080::ReadSignature(void) |
MACRUM | 1:a0e46d956969 | 28 | { |
MACRUM | 1:a0e46d956969 | 29 | uint16_t Manufacturer_ID = read2Bytes(CHIP_ADDRESS, HDC_MANID_OFF); |
MACRUM | 1:a0e46d956969 | 30 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 31 | if (Manufacturer_ID == 0) { |
MACRUM | 1:a0e46d956969 | 32 | return (int)HDC_CHIP_ERROR; |
MACRUM | 1:a0e46d956969 | 33 | } else { |
shivanandgowdakr | 0:fdb750cc9ca8 | 34 | return Manufacturer_ID; |
MACRUM | 1:a0e46d956969 | 35 | } |
MACRUM | 1:a0e46d956969 | 36 | } |
MACRUM | 1:a0e46d956969 | 37 | |
MACRUM | 1:a0e46d956969 | 38 | float HDC1080::readTemperature() |
shivanandgowdakr | 0:fdb750cc9ca8 | 39 | { |
shivanandgowdakr | 0:fdb750cc9ca8 | 40 | uint16_t rawT = read2Bytes(CHIP_ADDRESS, HDC_TEMP_OFF); |
MACRUM | 1:a0e46d956969 | 41 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 42 | if (rawT == 0) { |
shivanandgowdakr | 0:fdb750cc9ca8 | 43 | return HDC_CHIP_ERROR; |
shivanandgowdakr | 0:fdb750cc9ca8 | 44 | } else { |
shivanandgowdakr | 0:fdb750cc9ca8 | 45 | float temp = ((float) rawT / pow(2.0f, 16.0f)) * 165.0f - 40.0f; |
shivanandgowdakr | 0:fdb750cc9ca8 | 46 | return temp; |
shivanandgowdakr | 0:fdb750cc9ca8 | 47 | } |
MACRUM | 1:a0e46d956969 | 48 | } |
MACRUM | 1:a0e46d956969 | 49 | |
MACRUM | 1:a0e46d956969 | 50 | float HDC1080::readHumidity() |
shivanandgowdakr | 0:fdb750cc9ca8 | 51 | { |
shivanandgowdakr | 0:fdb750cc9ca8 | 52 | uint16_t rawH = read2Bytes(CHIP_ADDRESS, HDC_HUMID_OFF); |
MACRUM | 1:a0e46d956969 | 53 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 54 | if (rawH == 0) { |
shivanandgowdakr | 0:fdb750cc9ca8 | 55 | return HDC_CHIP_ERROR; |
shivanandgowdakr | 0:fdb750cc9ca8 | 56 | } else { |
shivanandgowdakr | 0:fdb750cc9ca8 | 57 | float humidity = ((float) rawH / pow(2.0f, 16.0f)) * 100.0f; |
shivanandgowdakr | 0:fdb750cc9ca8 | 58 | return humidity; |
shivanandgowdakr | 0:fdb750cc9ca8 | 59 | } |
MACRUM | 1:a0e46d956969 | 60 | } |
MACRUM | 1:a0e46d956969 | 61 | |
MACRUM | 1:a0e46d956969 | 62 | unsigned long HDC1080::readSerialNumber(void) |
MACRUM | 1:a0e46d956969 | 63 | { |
MACRUM | 2:b913d2690215 | 64 | thread_sleep_for(15); |
MACRUM | 1:a0e46d956969 | 65 | memset(_buffer,0,4); |
MACRUM | 1:a0e46d956969 | 66 | _buffer[0] = HDC_MANID_OFF; |
shivanandgowdakr | 0:fdb750cc9ca8 | 67 | |
MACRUM | 1:a0e46d956969 | 68 | int res = this->write(CHIP_ADDRESS, _buffer, 1); |
shivanandgowdakr | 0:fdb750cc9ca8 | 69 | if (res != 0) { |
MACRUM | 1:a0e46d956969 | 70 | return (unsigned long) HDC_CHIP_SER_ERROR; |
shivanandgowdakr | 0:fdb750cc9ca8 | 71 | } |
MACRUM | 1:a0e46d956969 | 72 | |
MACRUM | 2:b913d2690215 | 73 | thread_sleep_for(15); |
MACRUM | 1:a0e46d956969 | 74 | memset(_buffer,0,4); |
MACRUM | 1:a0e46d956969 | 75 | |
MACRUM | 1:a0e46d956969 | 76 | res = this->read(CHIP_ADDRESS, _buffer,4); |
MACRUM | 1:a0e46d956969 | 77 | if (res != 0) { |
MACRUM | 1:a0e46d956969 | 78 | return (unsigned long) HDC_CHIP_SER_ERROR; |
MACRUM | 1:a0e46d956969 | 79 | } |
MACRUM | 1:a0e46d956969 | 80 | |
MACRUM | 1:a0e46d956969 | 81 | unsigned long rawser = _buffer[2] << 16 | _buffer[1] << 8 | _buffer[0]; |
shivanandgowdakr | 0:fdb750cc9ca8 | 82 | return rawser; |
shivanandgowdakr | 0:fdb750cc9ca8 | 83 | } |
MACRUM | 1:a0e46d956969 | 84 | |
MACRUM | 1:a0e46d956969 | 85 | //Private Member functions |
MACRUM | 1:a0e46d956969 | 86 | |
MACRUM | 1:a0e46d956969 | 87 | uint16_t HDC1080::read2Bytes(int chip_addr, int offset) |
shivanandgowdakr | 0:fdb750cc9ca8 | 88 | { |
MACRUM | 1:a0e46d956969 | 89 | memset(_buffer,0,3); |
MACRUM | 1:a0e46d956969 | 90 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 91 | // send chip address onto buss |
MACRUM | 1:a0e46d956969 | 92 | _buffer[0] = offset; |
MACRUM | 1:a0e46d956969 | 93 | int res =this->write(chip_addr, _buffer, 1); |
shivanandgowdakr | 0:fdb750cc9ca8 | 94 | if (res != 0) { |
shivanandgowdakr | 0:fdb750cc9ca8 | 95 | return 0; |
shivanandgowdakr | 0:fdb750cc9ca8 | 96 | } |
MACRUM | 1:a0e46d956969 | 97 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 98 | // read data from chip |
MACRUM | 2:b913d2690215 | 99 | thread_sleep_for(15); |
MACRUM | 1:a0e46d956969 | 100 | memset(_buffer,0,3); |
MACRUM | 1:a0e46d956969 | 101 | res = this->read(CHIP_ADDRESS, _buffer,2); |
shivanandgowdakr | 0:fdb750cc9ca8 | 102 | if (res != 0) { |
shivanandgowdakr | 0:fdb750cc9ca8 | 103 | return 0; |
shivanandgowdakr | 0:fdb750cc9ca8 | 104 | } |
MACRUM | 1:a0e46d956969 | 105 | return _buffer[0] << 8 | _buffer[1]; |
shivanandgowdakr | 0:fdb750cc9ca8 | 106 | } |