HDC1050 Library

HDC1050.cpp

Committer:
zebrin1422
Date:
2017-07-08
Revision:
0:771ed287f6a8
Child:
1:db08a3faa811

File content as of revision 0:771ed287f6a8:

#include"mbed.h"
#include"HDC1050.h"

//Serial pc(USBTX, USBRX); //tx, rx

myHDC1050::myHDC1050(I2C &i2cBus){
    i2c = &i2cBus;
    i2c->frequency(400000);
}

void myHDC1050::setup(int i)
{
    reg = check_reg;
    char cmd[3][2];
    cmd[0][0] = 0x10; cmd[0][1] = 0x00;
    cmd[1][0] = 0x00; cmd[1][1] = 0x00;
    cmd[1][0] = 0x03; cmd[3][1] = 0x00;
    
    i2c->write(SLV_WRITE, &reg, 1, true);
    i2c->write(SLV_WRITE, cmd[i], 2, false);
}
    
int myHDC1050::Connection_check()
{
    char reg = check_reg;
    char check[2];
    
    i2c->write(SLV_WRITE,&reg,1);
    i2c->read(SLV_READ,check,2);
    
    if(check[0] == 0x10 && check[1] == 0x50)return 0;
    else return 1;

}    

void myHDC1050::get_temp_hum(float *temp, float *hum)
{
    char reg = Temperature_reg;
    char buff[4];
    unsigned int val[4];
    
    setup(0);
    
    i2c->write(SLV_WRITE, &reg,1);
    wait_ms(250);
    i2c->read(SLV_READ, buff,4,false);
    
    val[0] = (unsigned int)buff[0] << 8;
    val[1] = (unsigned int)buff[1];
    val[2] = (unsigned int)buff[2] << 8;
    val[3] = (unsigned int)buff[3];
    
    *temp = (float)(val[0] | val[1]);
    *hum  = (float)(val[2] | val[3]);
    
    *temp = *temp*165.0/65536.0 - 40.0;
    *hum = *hum*100.0/65536.0;

}

void myHDC1050::get_temp(float *temp)
{
    char reg = Temperature_reg;
    char buff[2];
    unsigned int val[2];     
    
    setup(1);
       
    i2c->write(SLV_WRITE, &reg,1);
    wait_ms(130);
    i2c->read(SLV_READ, buff, 2,false);
    
    val[0] = (unsigned int)buff[0] << 8;
    val[1] = (unsigned int)buff[1];
    
    *temp = (float)(val[0] | val[1]);
    *temp = *temp *165.0/65536.0 - 40.0;
    
}

void myHDC1050::get_hum(float *hum)
{
    char reg = Humidity_reg;
    char buff[2];
    unsigned int val[2];
    
    setup(1);
     
    i2c->write(SLV_WRITE, &reg,1);
    wait_ms(130);
    i2c->read(SLV_READ, buff, 2,false);
    
    val[0] = (unsigned int)buff[0] << 8;
    val[1] = (unsigned int)buff[1];
    
    *hum = (float)(val[0] | val[1]);
    *hum = *hum*100.0/65536.0;

}

void myHDC1050::get_temp_hater(float *temp)
{
    char reg = Temperature_reg;
    char buff[2];
    unsigned int val[2];     
    
    setup(2);
       
    i2c->write(SLV_WRITE, &reg,1);
    wait_ms(130);
    i2c->read(SLV_READ, buff, 2,false);
    
    val[0] = (unsigned int)buff[0] << 8;
    val[1] = (unsigned int)buff[1];
    
    *temp = (float)(val[0] | val[1]);
    *temp = *temp *165.0/65536.0 - 40.0;
    
}