HDC1080 sensor library
HDC1080.cpp@1:a0e46d956969, 2019-11-08 (annotated)
- Committer:
- MACRUM
- Date:
- Fri Nov 08 04:56:00 2019 +0000
- Revision:
- 1:a0e46d956969
- Parent:
- 0:fdb750cc9ca8
- Child:
- 2:b913d2690215
Remove printf
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 | 1:a0e46d956969 | 64 | memset(_buffer,0,4); |
MACRUM | 1:a0e46d956969 | 65 | _buffer[0] = HDC_MANID_OFF; |
shivanandgowdakr | 0:fdb750cc9ca8 | 66 | |
MACRUM | 1:a0e46d956969 | 67 | int res = this->write(CHIP_ADDRESS, _buffer, 1); |
shivanandgowdakr | 0:fdb750cc9ca8 | 68 | if (res != 0) { |
MACRUM | 1:a0e46d956969 | 69 | return (unsigned long) HDC_CHIP_SER_ERROR; |
shivanandgowdakr | 0:fdb750cc9ca8 | 70 | } |
MACRUM | 1:a0e46d956969 | 71 | |
MACRUM | 1:a0e46d956969 | 72 | memset(_buffer,0,4); |
MACRUM | 1:a0e46d956969 | 73 | |
MACRUM | 1:a0e46d956969 | 74 | res = this->read(CHIP_ADDRESS, _buffer,4); |
MACRUM | 1:a0e46d956969 | 75 | if (res != 0) { |
MACRUM | 1:a0e46d956969 | 76 | return (unsigned long) HDC_CHIP_SER_ERROR; |
MACRUM | 1:a0e46d956969 | 77 | } |
MACRUM | 1:a0e46d956969 | 78 | |
MACRUM | 1:a0e46d956969 | 79 | unsigned long rawser = _buffer[2] << 16 | _buffer[1] << 8 | _buffer[0]; |
shivanandgowdakr | 0:fdb750cc9ca8 | 80 | return rawser; |
shivanandgowdakr | 0:fdb750cc9ca8 | 81 | } |
MACRUM | 1:a0e46d956969 | 82 | |
MACRUM | 1:a0e46d956969 | 83 | //Private Member functions |
MACRUM | 1:a0e46d956969 | 84 | |
MACRUM | 1:a0e46d956969 | 85 | uint16_t HDC1080::read2Bytes(int chip_addr, int offset) |
shivanandgowdakr | 0:fdb750cc9ca8 | 86 | { |
MACRUM | 1:a0e46d956969 | 87 | memset(_buffer,0,3); |
MACRUM | 1:a0e46d956969 | 88 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 89 | // send chip address onto buss |
MACRUM | 1:a0e46d956969 | 90 | _buffer[0] = offset; |
MACRUM | 1:a0e46d956969 | 91 | int res =this->write(chip_addr, _buffer, 1); |
shivanandgowdakr | 0:fdb750cc9ca8 | 92 | if (res != 0) { |
shivanandgowdakr | 0:fdb750cc9ca8 | 93 | return 0; |
shivanandgowdakr | 0:fdb750cc9ca8 | 94 | } |
MACRUM | 1:a0e46d956969 | 95 | |
shivanandgowdakr | 0:fdb750cc9ca8 | 96 | // read data from chip |
MACRUM | 1:a0e46d956969 | 97 | memset(_buffer,0,3); |
MACRUM | 1:a0e46d956969 | 98 | res = this->read(CHIP_ADDRESS, _buffer,2); |
shivanandgowdakr | 0:fdb750cc9ca8 | 99 | if (res != 0) { |
shivanandgowdakr | 0:fdb750cc9ca8 | 100 | return 0; |
shivanandgowdakr | 0:fdb750cc9ca8 | 101 | } |
MACRUM | 1:a0e46d956969 | 102 | return _buffer[0] << 8 | _buffer[1]; |
shivanandgowdakr | 0:fdb750cc9ca8 | 103 | } |