Shivanand Gowda / HDC1080

Dependents:   hdc1080_Hellow_world Temp_Humid

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HDC1080.cpp Source File

HDC1080.cpp

00001 
00002 #include "mbed.h"
00003 #include "HDC1080.h"
00004 
00005 #define     HDC_TEMP_OFF        0x00
00006 #define     HDC_HUMID_OFF       0x01
00007 #define     HDC_CONFIG_OFF      0x02
00008 #define     HDC_MANID_OFF       0xFE
00009 #define     HDC_SER_OFF_FIRST   0xFB
00010 #define     HDC_SER_OFF_MID     0xFC
00011 #define     HDC_SER_OFF_LAST    0xFD
00012 #define     I2C_FREQ            100000
00013 #define     CHIP_ADDRESS        (0x40 << 1)   // left shift 1 bit for 7 bit address required by
00014 
00015  // Shift by one bit to get 7 bit I2C Addrress
00016 char HDC_COMMN = HDC_MANID_OFF;
00017 const float HDC_CHIP_ERROR = -255;
00018 const unsigned long HDC_CHIP_SER_ERROR = 0;
00019 char Buffer[5];
00020 
00021 
00022 HDC1080::HDC1080(PinName sda, PinName slc) : I2C(sda,slc) 
00023 {
00024     memset(Buffer,'\0',5);
00025     Buffer[0] = HDC_CONFIG_OFF;
00026     this->frequency(I2C_FREQ);
00027     int res = this->write(CHIP_ADDRESS, Buffer, 2);
00028     printf("HDC Constructor Initialization  : Res =%d\r\n", res);
00029 }
00030 
00031   int HDC1080::ReadSignature(void)
00032 { 
00033     
00034 uint16_t  Manufacturer_ID = read2Bytes(CHIP_ADDRESS, HDC_MANID_OFF);
00035     if (Manufacturer_ID == 0) {
00036      
00037         printf("Error  reading HDC Manufacturer ID\r\n");
00038        
00039         return (int) HDC_CHIP_ERROR;
00040     } else {  
00041            
00042         printf("Manufacturer_ID  :%x\r\n", (int) Manufacturer_ID);
00043       
00044         return Manufacturer_ID;
00045     }    
00046 }  
00047     
00048     
00049     
00050     
00051     
00052  float HDC1080::readTemperature()
00053 {
00054     uint16_t  rawT = read2Bytes(CHIP_ADDRESS, HDC_TEMP_OFF);
00055     if (rawT == 0) {
00056        
00057         printf("error in reading  chip Temp\r\n");
00058        
00059         return HDC_CHIP_ERROR;
00060     } else {
00061         float temp = ((float) rawT / pow(2.0f, 16.0f)) * 165.0f - 40.0f;
00062         
00063         printf("Temperature   : %0.3f\r\n", temp);
00064         
00065         return temp;
00066     }
00067 }   
00068     
00069        
00070     
00071  float HDC1080::readHumidity()
00072 {
00073     uint16_t  rawH = read2Bytes(CHIP_ADDRESS, HDC_HUMID_OFF);
00074     if (rawH == 0) {
00075        
00076         printf("error in reading  chip Temp\r\n");
00077        
00078         return HDC_CHIP_ERROR;
00079     } else {
00080         float humidity = ((float) rawH / pow(2.0f, 16.0f)) * 100.0f;
00081         
00082         printf("Humidity   : %0.3f\r\n", humidity);
00083         
00084         return humidity;
00085     }
00086 }   
00087     
00088     unsigned long HDC1080::readSerialNumber(void)
00089 {    
00090     wait(0.015);
00091     memset(Buffer,0,4);
00092     Buffer[0] = HDC_MANID_OFF;
00093     int res = this->write(CHIP_ADDRESS, Buffer, 1);
00094     if (res != 0) {      
00095      
00096       printf("Error writing chip addr res=%d\r\n", res);
00097     
00098       return (unsigned long) HDC_CHIP_SER_ERROR;
00099     }      
00100  
00101     wait(0.015);
00102     memset(Buffer,0,4);
00103     res = this->read(CHIP_ADDRESS, Buffer,4);
00104     if (res != 0) {
00105      
00106       printf("Errot reading chip serial res=%d#\r\n", res);
00107      
00108       return (unsigned long) HDC_CHIP_SER_ERROR;
00109     }
00110       
00111 //    unsigned long rawser = Buffer[0] << 16 | Buffer[1] << 8 | Buffer[0];
00112       unsigned long rawser = Buffer[2] << 16 | Buffer[1] << 8 | Buffer[0];
00113   
00114     printf("Serial Number is =%lu\r\n", rawser);
00115    
00116     return rawser;
00117 }
00118     
00119     
00120     //Private Member functions 
00121     
00122     uint16_t HDC1080::read2Bytes(int chip_addr, int offset)
00123 {
00124     memset(Buffer,0,3);
00125     // send chip address onto buss
00126     Buffer[0] = offset;
00127     int res =this->write(chip_addr, Buffer, 1);
00128     if (res != 0) {
00129        
00130         printf("error Communicating  to chip %d offst=%d\r\n", chip_addr, offset);
00131        
00132         return 0;
00133     }
00134     // read data from chip
00135     wait(0.015);
00136     memset(Buffer,0,3);
00137     res = this->read(CHIP_ADDRESS, Buffer,2);
00138     if (res != 0) {
00139         printf("error Communicating to  chip %d offst=%d\r\n", chip_addr, offset);
00140         return 0;
00141     }
00142     return  Buffer[0] << 8 | Buffer[1];
00143 }