HDC1080 sensor library

Committer:
shivanandgowdakr
Date:
Sat Oct 06 12:08:55 2018 +0000
Revision:
0:fdb750cc9ca8
Child:
1:a0e46d956969
HDC1080 Temperature Humidity Sensor ; i2c based or  controlled ;

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
shivanandgowdakr 0:fdb750cc9ca8 13 #define CHIP_ADDRESS (0x40 << 1) // left shift 1 bit for 7 bit address required by
shivanandgowdakr 0:fdb750cc9ca8 14
shivanandgowdakr 0:fdb750cc9ca8 15 // Shift by one bit to get 7 bit I2C Addrress
shivanandgowdakr 0:fdb750cc9ca8 16 char HDC_COMMN = HDC_MANID_OFF;
shivanandgowdakr 0:fdb750cc9ca8 17 const float HDC_CHIP_ERROR = -255;
shivanandgowdakr 0:fdb750cc9ca8 18 const unsigned long HDC_CHIP_SER_ERROR = 0;
shivanandgowdakr 0:fdb750cc9ca8 19 char Buffer[5];
shivanandgowdakr 0:fdb750cc9ca8 20
shivanandgowdakr 0:fdb750cc9ca8 21
shivanandgowdakr 0:fdb750cc9ca8 22 HDC1080::HDC1080(PinName sda, PinName slc) : I2C(sda,slc)
shivanandgowdakr 0:fdb750cc9ca8 23 {
shivanandgowdakr 0:fdb750cc9ca8 24 memset(Buffer,'\0',5);
shivanandgowdakr 0:fdb750cc9ca8 25 Buffer[0] = HDC_CONFIG_OFF;
shivanandgowdakr 0:fdb750cc9ca8 26 this->frequency(I2C_FREQ);
shivanandgowdakr 0:fdb750cc9ca8 27 int res = this->write(CHIP_ADDRESS, Buffer, 2);
shivanandgowdakr 0:fdb750cc9ca8 28 printf("HDC Constructor Initialization : Res =%d\r\n", res);
shivanandgowdakr 0:fdb750cc9ca8 29 }
shivanandgowdakr 0:fdb750cc9ca8 30
shivanandgowdakr 0:fdb750cc9ca8 31 int HDC1080::ReadSignature(void)
shivanandgowdakr 0:fdb750cc9ca8 32 {
shivanandgowdakr 0:fdb750cc9ca8 33
shivanandgowdakr 0:fdb750cc9ca8 34 uint16_t Manufacturer_ID = read2Bytes(CHIP_ADDRESS, HDC_MANID_OFF);
shivanandgowdakr 0:fdb750cc9ca8 35 if (Manufacturer_ID == 0) {
shivanandgowdakr 0:fdb750cc9ca8 36
shivanandgowdakr 0:fdb750cc9ca8 37 printf("Error reading HDC Manufacturer ID\r\n");
shivanandgowdakr 0:fdb750cc9ca8 38
shivanandgowdakr 0:fdb750cc9ca8 39 return (int) HDC_CHIP_ERROR;
shivanandgowdakr 0:fdb750cc9ca8 40 } else {
shivanandgowdakr 0:fdb750cc9ca8 41
shivanandgowdakr 0:fdb750cc9ca8 42 printf("Manufacturer_ID :%x\r\n", (int) Manufacturer_ID);
shivanandgowdakr 0:fdb750cc9ca8 43
shivanandgowdakr 0:fdb750cc9ca8 44 return Manufacturer_ID;
shivanandgowdakr 0:fdb750cc9ca8 45 }
shivanandgowdakr 0:fdb750cc9ca8 46 }
shivanandgowdakr 0:fdb750cc9ca8 47
shivanandgowdakr 0:fdb750cc9ca8 48
shivanandgowdakr 0:fdb750cc9ca8 49
shivanandgowdakr 0:fdb750cc9ca8 50
shivanandgowdakr 0:fdb750cc9ca8 51
shivanandgowdakr 0:fdb750cc9ca8 52 float HDC1080::readTemperature()
shivanandgowdakr 0:fdb750cc9ca8 53 {
shivanandgowdakr 0:fdb750cc9ca8 54 uint16_t rawT = read2Bytes(CHIP_ADDRESS, HDC_TEMP_OFF);
shivanandgowdakr 0:fdb750cc9ca8 55 if (rawT == 0) {
shivanandgowdakr 0:fdb750cc9ca8 56
shivanandgowdakr 0:fdb750cc9ca8 57 printf("error in reading chip Temp\r\n");
shivanandgowdakr 0:fdb750cc9ca8 58
shivanandgowdakr 0:fdb750cc9ca8 59 return HDC_CHIP_ERROR;
shivanandgowdakr 0:fdb750cc9ca8 60 } else {
shivanandgowdakr 0:fdb750cc9ca8 61 float temp = ((float) rawT / pow(2.0f, 16.0f)) * 165.0f - 40.0f;
shivanandgowdakr 0:fdb750cc9ca8 62
shivanandgowdakr 0:fdb750cc9ca8 63 printf("Temperature : %0.3f\r\n", temp);
shivanandgowdakr 0:fdb750cc9ca8 64
shivanandgowdakr 0:fdb750cc9ca8 65 return temp;
shivanandgowdakr 0:fdb750cc9ca8 66 }
shivanandgowdakr 0:fdb750cc9ca8 67 }
shivanandgowdakr 0:fdb750cc9ca8 68
shivanandgowdakr 0:fdb750cc9ca8 69
shivanandgowdakr 0:fdb750cc9ca8 70
shivanandgowdakr 0:fdb750cc9ca8 71 float HDC1080::readHumidity()
shivanandgowdakr 0:fdb750cc9ca8 72 {
shivanandgowdakr 0:fdb750cc9ca8 73 uint16_t rawH = read2Bytes(CHIP_ADDRESS, HDC_HUMID_OFF);
shivanandgowdakr 0:fdb750cc9ca8 74 if (rawH == 0) {
shivanandgowdakr 0:fdb750cc9ca8 75
shivanandgowdakr 0:fdb750cc9ca8 76 printf("error in reading chip Temp\r\n");
shivanandgowdakr 0:fdb750cc9ca8 77
shivanandgowdakr 0:fdb750cc9ca8 78 return HDC_CHIP_ERROR;
shivanandgowdakr 0:fdb750cc9ca8 79 } else {
shivanandgowdakr 0:fdb750cc9ca8 80 float humidity = ((float) rawH / pow(2.0f, 16.0f)) * 100.0f;
shivanandgowdakr 0:fdb750cc9ca8 81
shivanandgowdakr 0:fdb750cc9ca8 82 printf("Humidity : %0.3f\r\n", humidity);
shivanandgowdakr 0:fdb750cc9ca8 83
shivanandgowdakr 0:fdb750cc9ca8 84 return humidity;
shivanandgowdakr 0:fdb750cc9ca8 85 }
shivanandgowdakr 0:fdb750cc9ca8 86 }
shivanandgowdakr 0:fdb750cc9ca8 87
shivanandgowdakr 0:fdb750cc9ca8 88 unsigned long HDC1080::readSerialNumber(void)
shivanandgowdakr 0:fdb750cc9ca8 89 {
shivanandgowdakr 0:fdb750cc9ca8 90 wait(0.015);
shivanandgowdakr 0:fdb750cc9ca8 91 memset(Buffer,0,4);
shivanandgowdakr 0:fdb750cc9ca8 92 Buffer[0] = HDC_MANID_OFF;
shivanandgowdakr 0:fdb750cc9ca8 93 int res = this->write(CHIP_ADDRESS, Buffer, 1);
shivanandgowdakr 0:fdb750cc9ca8 94 if (res != 0) {
shivanandgowdakr 0:fdb750cc9ca8 95
shivanandgowdakr 0:fdb750cc9ca8 96 printf("Error writing chip addr res=%d\r\n", res);
shivanandgowdakr 0:fdb750cc9ca8 97
shivanandgowdakr 0:fdb750cc9ca8 98 return (unsigned long) HDC_CHIP_SER_ERROR;
shivanandgowdakr 0:fdb750cc9ca8 99 }
shivanandgowdakr 0:fdb750cc9ca8 100
shivanandgowdakr 0:fdb750cc9ca8 101 wait(0.015);
shivanandgowdakr 0:fdb750cc9ca8 102 memset(Buffer,0,4);
shivanandgowdakr 0:fdb750cc9ca8 103 res = this->read(CHIP_ADDRESS, Buffer,4);
shivanandgowdakr 0:fdb750cc9ca8 104 if (res != 0) {
shivanandgowdakr 0:fdb750cc9ca8 105
shivanandgowdakr 0:fdb750cc9ca8 106 printf("Errot reading chip serial res=%d#\r\n", res);
shivanandgowdakr 0:fdb750cc9ca8 107
shivanandgowdakr 0:fdb750cc9ca8 108 return (unsigned long) HDC_CHIP_SER_ERROR;
shivanandgowdakr 0:fdb750cc9ca8 109 }
shivanandgowdakr 0:fdb750cc9ca8 110
shivanandgowdakr 0:fdb750cc9ca8 111 // unsigned long rawser = Buffer[0] << 16 | Buffer[1] << 8 | Buffer[0];
shivanandgowdakr 0:fdb750cc9ca8 112 unsigned long rawser = Buffer[2] << 16 | Buffer[1] << 8 | Buffer[0];
shivanandgowdakr 0:fdb750cc9ca8 113
shivanandgowdakr 0:fdb750cc9ca8 114 printf("Serial Number is =%lu\r\n", rawser);
shivanandgowdakr 0:fdb750cc9ca8 115
shivanandgowdakr 0:fdb750cc9ca8 116 return rawser;
shivanandgowdakr 0:fdb750cc9ca8 117 }
shivanandgowdakr 0:fdb750cc9ca8 118
shivanandgowdakr 0:fdb750cc9ca8 119
shivanandgowdakr 0:fdb750cc9ca8 120 //Private Member functions
shivanandgowdakr 0:fdb750cc9ca8 121
shivanandgowdakr 0:fdb750cc9ca8 122 uint16_t HDC1080::read2Bytes(int chip_addr, int offset)
shivanandgowdakr 0:fdb750cc9ca8 123 {
shivanandgowdakr 0:fdb750cc9ca8 124 memset(Buffer,0,3);
shivanandgowdakr 0:fdb750cc9ca8 125 // send chip address onto buss
shivanandgowdakr 0:fdb750cc9ca8 126 Buffer[0] = offset;
shivanandgowdakr 0:fdb750cc9ca8 127 int res =this->write(chip_addr, Buffer, 1);
shivanandgowdakr 0:fdb750cc9ca8 128 if (res != 0) {
shivanandgowdakr 0:fdb750cc9ca8 129
shivanandgowdakr 0:fdb750cc9ca8 130 printf("error Communicating to chip %d offst=%d\r\n", chip_addr, offset);
shivanandgowdakr 0:fdb750cc9ca8 131
shivanandgowdakr 0:fdb750cc9ca8 132 return 0;
shivanandgowdakr 0:fdb750cc9ca8 133 }
shivanandgowdakr 0:fdb750cc9ca8 134 // read data from chip
shivanandgowdakr 0:fdb750cc9ca8 135 wait(0.015);
shivanandgowdakr 0:fdb750cc9ca8 136 memset(Buffer,0,3);
shivanandgowdakr 0:fdb750cc9ca8 137 res = this->read(CHIP_ADDRESS, Buffer,2);
shivanandgowdakr 0:fdb750cc9ca8 138 if (res != 0) {
shivanandgowdakr 0:fdb750cc9ca8 139 printf("error Communicating to chip %d offst=%d\r\n", chip_addr, offset);
shivanandgowdakr 0:fdb750cc9ca8 140 return 0;
shivanandgowdakr 0:fdb750cc9ca8 141 }
shivanandgowdakr 0:fdb750cc9ca8 142 return Buffer[0] << 8 | Buffer[1];
shivanandgowdakr 0:fdb750cc9ca8 143 }