This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

HDC2080: I can not read or write in register library

Part Number: HDC2080
Other Parts Discussed in Thread: HDC2010

Hi, i am studying with I2C and HDC2080. And I'm using stm32f030c6t, but i can not understand how to do this. I also read datasheet. And here is my example. Where am i doing wrong?

#include "stdint.h"
#include "stm32f0xx.h" 
#define ADDR_I2C 0x82 //DHC2080 i2c Address
////  Constants for setting measurement resolution
#define FOURTEEN_BIT 0
#define ELEVEN_BIT 1
#define NINE_BIT  2
////  Constants for setting sensor mode
#define TEMP_AND_HUMID 0
#define TEMP_ONLY    1
#define HUMID_ONLY    2
#define ACTIVE_LOW    0
#define ACTIVE_HIGH    1
#define LEVEL_MODE 0
#define COMPARATOR_MODE 1
///////////////////////////////////////
#define TEMP_LOW 0x00
#define TEMP_HIGH 0x01
#define HUMID_LOW 0x02
#define HUMID_HIGH 0x03
#define INTERRUPT_DRDY 0x04
#define TEMP_MAX 0x05
#define HUMID_MAX 0x06
#define INTERRUPT_CONFIG 0x07
#define TEMP_OFFSET_ADJUST 0x08
#define HUM_OFFSET_ADJUST 0x09
#define TEMP_THR_L 0x0A
#define TEMP_THR_H 0x0B
#define HUMID_THR_L 0x0C
#define HUMID_THR_H 0x0D
#define CONFIG 0x0E
#define MEASUREMENT_CONFIG 0x0F
#define MID_L 0xFC
#define MID_H 0xFD
#define DEVICE_ID_L 0xFE
#define DEVICE_ID_H 0xFF
typedef enum
{
  MANUAL,
  TWO_MINS,
  ONE_MINS,
  TEN_SECONDS,
  FIVE_SECONDS,
  ONE_HZ,
  TWO_HZ,
  FIVE_HZ,
}HDC2080_RATE_t; 
extern HDC2080_RATE_t HDC2080_rate;
extern uint32_t delayCounter;
extern uint8_t delayFlag;
extern void Start_HDC2080();
extern void Reset_HDC2080();
extern void HDC2080_setMeasurementMode(int mode);
extern void HDC2080_setRate(int rate);
extern void HDC2080_triggerMeasurement();
extern void HDC2080_setTempRes(int resolution);
extern void HDC2080_setHumidRes(int resolution);
extern void HDC2080_setHighTemp(float temp);
extern void HDC2080_setLowTemp(float temp);
extern void HDC2080_setHighHumidity(float hum);
extern void HDC2080_setLowHumidity(float hum);
extern int16_t HDC2080_readTemp();
extern int16_t HDC2080_readHumidity();
extern uint8_t HDC2080_readReg(uint8_t reg);
extern void HDC2080_openReg(uint8_t reg);
extern void HDC2080_writeReg(uint8_t reg, uint8_t data);
extern void DelayinMs();
////////////////////////////////////////////////////////////////////////////////
#include "HDC2080.h"
//Private Variables
HDC2080_RATE_t HDC2080_rate;
uint32_t delayCounter = 0;
uint8_t delayFlag = 1;
//Function Prototypes
void Start_HDC2080();
void Reset_HDC2080();
void HDC2080_setMeasurementMode(int mode);
void HDC2080_setRate(int rate);
void HDC2080_triggerMeasurement();
void HDC2080_setTempRes(int resolution);
void HDC2080_setHumidRes(int resolution);
void HDC2080_setHighTemp(float temp);
void HDC2080_setLowTemp(float temp);
void HDC2080_setHighHumidity(float hum);
void HDC2080_setLowHumidity(float hum);
int16_t HDC2080_readTemp();
int16_t HDC2080_readHumidity();
uint8_t HDC2080_readReg(uint8_t reg);
void HDC2080_openReg(uint8_t reg);
void HDC2080_writeReg(uint8_t reg, uint8_t data);
void DelayinMs();
void Start_HDC2080()
{
    Reset_HDC2080();                            // Begin with a device reset
    HDC2080_setHighTemp(28);                    // High temperature of 28C
    HDC2080_setLowTemp(22);                     // Low temperature of 22C
    HDC2080_setHighHumidity(55);                // High humidity of 55%
    HDC2080_setLowHumidity(40);                 // Low humidity of 40%
    HDC2080_setMeasurementMode(TEMP_AND_HUMID); // Set measurements to temperature and humidity
    HDC2080_setRate(ONE_HZ);                    // Set measurement frequency to 1 Hz
    HDC2080_setTempRes(FOURTEEN_BIT);
    HDC2080_setHumidRes(FOURTEEN_BIT);
    HDC2080_triggerMeasurement();
}
void Reset_HDC2080()
{
    uint8_t configContents;
    configContents = HDC2080_readReg(CONFIG);
    configContents = (configContents | 0x80);
    HDC2080_writeReg(CONFIG, configContents);
    delayFlag = 0;
    DelayinMs(25); //50ms delay
    while (delayFlag == 0);
}
void HDC2080_setMeasurementMode(int mode)
{
    uint8_t configContents;
    configContents = HDC2080_readReg(MEASUREMENT_CONFIG);
    switch (mode)
    {
    case TEMP_AND_HUMID:
        configContents = (configContents & 0xF9);
        break;
    case TEMP_ONLY:
        configContents = (configContents & 0xFC);
        configContents = (configContents | 0x02);
        break;
    case HUMID_ONLY:
        configContents = (configContents & 0xFD);
        configContents = (configContents | 0x04);
        break;
    default:
        configContents = (configContents & 0xF9);
    }
    HDC2080_writeReg(MEASUREMENT_CONFIG, configContents);
}
void HDC2080_setRate(int rate)
{
    uint8_t configContents;
    configContents = HDC2080_readReg(CONFIG);
    switch (rate)
    {
    case MANUAL:
        configContents = (configContents & 0x8F);
        break;
    case TWO_MINS:
        configContents = (configContents & 0x9F);
        configContents = (configContents | 0x10);
        break;
    case ONE_MINS:
        configContents = (configContents & 0xAF);
        configContents = (configContents | 0x20);
        break;
    case TEN_SECONDS:
        configContents = (configContents & 0xBF);
        configContents = (configContents | 0x30);
        break;
    case FIVE_SECONDS:
        configContents = (configContents & 0xCF);
        configContents = (configContents | 0x40);
        break;
    case ONE_HZ:
        configContents = (configContents & 0xDF);
        configContents = (configContents | 0x50);
        break;
    case TWO_HZ:
        configContents = (configContents & 0xEF);
        configContents = (configContents | 0x60);
        break;
    case FIVE_HZ:
        configContents = (configContents | 0x70);
        break;
    default:
        configContents = (configContents & 0x8F);
    }
    HDC2080_writeReg(CONFIG, configContents);
}
void HDC2080_triggerMeasurement()
{
    uint8_t configContents;
    configContents = HDC2080_readReg(MEASUREMENT_CONFIG);
    configContents = (configContents | 0x01);
    HDC2080_writeReg(MEASUREMENT_CONFIG, configContents);
}
void HDC2080_setTempRes(int resolution)
{
    uint8_t configContents;
    configContents = HDC2080_readReg(MEASUREMENT_CONFIG);
    switch (resolution)
    {
    case FOURTEEN_BIT:
        configContents = (configContents & 0x3F);
        break;
    case ELEVEN_BIT:
        configContents = (configContents & 0x7F);
        configContents = (configContents | 0x40);
        break;
    case NINE_BIT:
        configContents = (configContents & 0xBF);
        configContents = (configContents | 0x80);
        break;
    default:
        configContents = (configContents & 0x3F);
    }
    HDC2080_writeReg(MEASUREMENT_CONFIG, configContents);
}
void HDC2080_setHumidRes(int resolution)
{
    uint8_t configContents;
    configContents = HDC2080_readReg(MEASUREMENT_CONFIG);
    switch (resolution)
    {
    case FOURTEEN_BIT:
        configContents = (configContents & 0xCF);
        break;
    case ELEVEN_BIT:
        configContents = (configContents & 0xDF);
        configContents = (configContents | 0x10);
        break;
    case NINE_BIT:
        configContents = (configContents & 0xEF);
        configContents = (configContents | 0x20);
        break;
    default:
        configContents = (configContents & 0xCF);
    }
    HDC2080_writeReg(MEASUREMENT_CONFIG, configContents);
}
void HDC2080_setHighTemp(float temp)
{
    uint8_t temp_thresh_high;
    // Verify user is not trying to set value outside bounds
    if (temp < -40)
    {
        temp = -40;
    }
    else if (temp > 125)
    {
        temp = 125;
    }
    // Calculate value to load into register
    temp_thresh_high = (uint8_t)(256 * (temp + 40) / 165);
    HDC2080_writeReg(TEMP_THR_H, temp_thresh_high);
}
void HDC2080_setLowTemp(float temp)
{
    uint8_t temp_thresh_low;
    // Verify user is not trying to set value outside bounds
    if (temp < -40)
    {
        temp = -40;
    }
    else if (temp > 125)
    {
        temp = 125;
    }
    // Calculate value to load into register
    temp_thresh_low = (uint8_t)(256 * (temp + 40) / 165);
    HDC2080_writeReg(TEMP_THR_L, temp_thresh_low);
}
void HDC2080_setHighHumidity(float hum)
{
    uint8_t humid_thresh;
    // Verify user is not trying to set value outside bounds
    if (hum < 0)
    {
        hum = 0;
    }
    else if (hum > 100)
    {
        hum = 100;
    }
    // Calculate value to load into register
    humid_thresh = (uint8_t)(256 * (hum) / 100);
    HDC2080_writeReg(HUMID_THR_H, humid_thresh);
}
void HDC2080_setLowHumidity(float hum)
{
    uint8_t humid_thresh;
    // Verify user is not trying to set value outside bounds
    if (hum < 0)
    {
        hum = 0;
    }
    else if (hum > 100)
    {
        hum = 100;
    }
    // Calculate value to load into register
    humid_thresh = (uint8_t)(256 * (hum) / 100);
    HDC2080_writeReg(HUMID_THR_L, humid_thresh);
}
int16_t HDC2080_readTemp()
{
    uint8_t byte[2];
    uint16_t temp;
    byte[0] = HDC2080_readReg(TEMP_LOW);
    byte[1] = HDC2080_readReg(TEMP_HIGH);
    temp = (unsigned int)byte[1] << 8 | byte[0];
    return (int16_t)(temp)*165 / 65536 - 40;
}
int16_t HDC2080_readHumidity()
{
    uint8_t byte[2];
    uint16_t humidity;
    byte[0] = HDC2080_readReg(HUMID_LOW);
    byte[1] = HDC2080_readReg(HUMID_HIGH);
    humidity = (unsigned int)byte[1] << 8 | byte[0];
    return (int16_t)(humidity) / (65536) * 100;
}
uint8_t HDC2080_readReg(uint8_t reg)
{
    uint8_t data = 0;
    HDC2080_openReg(reg);
    I2C1->CR1 = 0x00000001;
    I2C1->CR2 = 0x02012482;
    while (I2C1->ISR & I2C_ISR_RXNE == RESET);
    data = I2C1->RXDR;
    while (I2C1->ISR & I2C_ISR_STOPF == RESET);
    I2C1->CR1 = 0x00000000;
    return data ;
}
void HDC2080_openReg(uint8_t reg)
{
    I2C1->CR1 = 0x00000001;
    I2C1->CR2 = 0x00012082;
    while (I2C1->ISR & I2C_ISR_BUSY == RESET);
    I2C1->TXDR = reg;
    while (I2C1->ISR & I2C_ISR_TC == RESET);
    I2C1->CR1 = 0x00000000;
}
void HDC2080_writeReg(uint8_t reg, uint8_t data)
{
    I2C1->CR1 = 0x00000001;
    I2C1->CR2 = 0x00012082;
    while (I2C1->ISR & I2C_ISR_BUSY == RESET);
    I2C1->TXDR = reg;
    while (I2C1->ISR & I2C_ISR_TC == RESET);
    I2C1->TXDR = data;
    while (I2C1->ISR & I2C_ISR_TC == RESET);
    I2C1->CR1 = 0x00000000;
}
void DelayinMs()
{
    if (delayCounter > 0)
    {
        delayCounter--;
    }
    else if (delayCounter == 0)
    {
        delayFlag = 1;
    }
}
////////////////////////////////////////////////