HDC1080 sensor library

Committer:
MACRUM
Date:
Sun Nov 10 14:22:08 2019 +0000
Revision:
4:250aceaa7a01
Parent:
3:d7627c8dcfe3
Include mbed.h

Who changed what in which revision?

UserRevisionLine numberNew 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
MACRUM 3:d7627c8dcfe3 18 #if MBED_MINOR_VERSION < 14
MACRUM 3:d7627c8dcfe3 19 #define thread_sleep_for(X) wait_ms(X)
MACRUM 3:d7627c8dcfe3 20 #endif
shivanandgowdakr 0:fdb750cc9ca8 21
MACRUM 1:a0e46d956969 22 HDC1080::HDC1080(PinName sda, PinName slc) : I2C(sda, slc)
shivanandgowdakr 0:fdb750cc9ca8 23 {
MACRUM 1:a0e46d956969 24 memset(_buffer,'\0',5);
MACRUM 1:a0e46d956969 25 _buffer[0] = HDC_CONFIG_OFF;
shivanandgowdakr 0:fdb750cc9ca8 26 this->frequency(I2C_FREQ);
MACRUM 1:a0e46d956969 27 int res = this->write(CHIP_ADDRESS, _buffer, 2);
shivanandgowdakr 0:fdb750cc9ca8 28 }
shivanandgowdakr 0:fdb750cc9ca8 29
MACRUM 1:a0e46d956969 30 int HDC1080::ReadSignature(void)
MACRUM 1:a0e46d956969 31 {
MACRUM 1:a0e46d956969 32 uint16_t Manufacturer_ID = read2Bytes(CHIP_ADDRESS, HDC_MANID_OFF);
MACRUM 1:a0e46d956969 33
shivanandgowdakr 0:fdb750cc9ca8 34 if (Manufacturer_ID == 0) {
MACRUM 1:a0e46d956969 35 return (int)HDC_CHIP_ERROR;
MACRUM 1:a0e46d956969 36 } else {
shivanandgowdakr 0:fdb750cc9ca8 37 return Manufacturer_ID;
MACRUM 1:a0e46d956969 38 }
MACRUM 1:a0e46d956969 39 }
MACRUM 1:a0e46d956969 40
MACRUM 1:a0e46d956969 41 float HDC1080::readTemperature()
shivanandgowdakr 0:fdb750cc9ca8 42 {
shivanandgowdakr 0:fdb750cc9ca8 43 uint16_t rawT = read2Bytes(CHIP_ADDRESS, HDC_TEMP_OFF);
MACRUM 1:a0e46d956969 44
shivanandgowdakr 0:fdb750cc9ca8 45 if (rawT == 0) {
shivanandgowdakr 0:fdb750cc9ca8 46 return HDC_CHIP_ERROR;
shivanandgowdakr 0:fdb750cc9ca8 47 } else {
shivanandgowdakr 0:fdb750cc9ca8 48 float temp = ((float) rawT / pow(2.0f, 16.0f)) * 165.0f - 40.0f;
shivanandgowdakr 0:fdb750cc9ca8 49 return temp;
shivanandgowdakr 0:fdb750cc9ca8 50 }
MACRUM 1:a0e46d956969 51 }
MACRUM 1:a0e46d956969 52
MACRUM 1:a0e46d956969 53 float HDC1080::readHumidity()
shivanandgowdakr 0:fdb750cc9ca8 54 {
shivanandgowdakr 0:fdb750cc9ca8 55 uint16_t rawH = read2Bytes(CHIP_ADDRESS, HDC_HUMID_OFF);
MACRUM 1:a0e46d956969 56
shivanandgowdakr 0:fdb750cc9ca8 57 if (rawH == 0) {
shivanandgowdakr 0:fdb750cc9ca8 58 return HDC_CHIP_ERROR;
shivanandgowdakr 0:fdb750cc9ca8 59 } else {
shivanandgowdakr 0:fdb750cc9ca8 60 float humidity = ((float) rawH / pow(2.0f, 16.0f)) * 100.0f;
shivanandgowdakr 0:fdb750cc9ca8 61 return humidity;
shivanandgowdakr 0:fdb750cc9ca8 62 }
MACRUM 1:a0e46d956969 63 }
MACRUM 1:a0e46d956969 64
MACRUM 1:a0e46d956969 65 unsigned long HDC1080::readSerialNumber(void)
MACRUM 1:a0e46d956969 66 {
MACRUM 2:b913d2690215 67 thread_sleep_for(15);
MACRUM 1:a0e46d956969 68 memset(_buffer,0,4);
MACRUM 1:a0e46d956969 69 _buffer[0] = HDC_MANID_OFF;
shivanandgowdakr 0:fdb750cc9ca8 70
MACRUM 1:a0e46d956969 71 int res = this->write(CHIP_ADDRESS, _buffer, 1);
shivanandgowdakr 0:fdb750cc9ca8 72 if (res != 0) {
MACRUM 1:a0e46d956969 73 return (unsigned long) HDC_CHIP_SER_ERROR;
shivanandgowdakr 0:fdb750cc9ca8 74 }
MACRUM 1:a0e46d956969 75
MACRUM 2:b913d2690215 76 thread_sleep_for(15);
MACRUM 1:a0e46d956969 77 memset(_buffer,0,4);
MACRUM 1:a0e46d956969 78
MACRUM 1:a0e46d956969 79 res = this->read(CHIP_ADDRESS, _buffer,4);
MACRUM 1:a0e46d956969 80 if (res != 0) {
MACRUM 1:a0e46d956969 81 return (unsigned long) HDC_CHIP_SER_ERROR;
MACRUM 1:a0e46d956969 82 }
MACRUM 1:a0e46d956969 83
MACRUM 1:a0e46d956969 84 unsigned long rawser = _buffer[2] << 16 | _buffer[1] << 8 | _buffer[0];
shivanandgowdakr 0:fdb750cc9ca8 85 return rawser;
shivanandgowdakr 0:fdb750cc9ca8 86 }
MACRUM 1:a0e46d956969 87
MACRUM 1:a0e46d956969 88 //Private Member functions
MACRUM 1:a0e46d956969 89
MACRUM 1:a0e46d956969 90 uint16_t HDC1080::read2Bytes(int chip_addr, int offset)
shivanandgowdakr 0:fdb750cc9ca8 91 {
MACRUM 1:a0e46d956969 92 memset(_buffer,0,3);
MACRUM 1:a0e46d956969 93
shivanandgowdakr 0:fdb750cc9ca8 94 // send chip address onto buss
MACRUM 1:a0e46d956969 95 _buffer[0] = offset;
MACRUM 1:a0e46d956969 96 int res =this->write(chip_addr, _buffer, 1);
shivanandgowdakr 0:fdb750cc9ca8 97 if (res != 0) {
shivanandgowdakr 0:fdb750cc9ca8 98 return 0;
shivanandgowdakr 0:fdb750cc9ca8 99 }
MACRUM 1:a0e46d956969 100
shivanandgowdakr 0:fdb750cc9ca8 101 // read data from chip
MACRUM 2:b913d2690215 102 thread_sleep_for(15);
MACRUM 1:a0e46d956969 103 memset(_buffer,0,3);
MACRUM 1:a0e46d956969 104 res = this->read(CHIP_ADDRESS, _buffer,2);
shivanandgowdakr 0:fdb750cc9ca8 105 if (res != 0) {
shivanandgowdakr 0:fdb750cc9ca8 106 return 0;
shivanandgowdakr 0:fdb750cc9ca8 107 }
MACRUM 1:a0e46d956969 108 return _buffer[0] << 8 | _buffer[1];
shivanandgowdakr 0:fdb750cc9ca8 109 }