Thursday, August 10, 2023

LPSTK-CC1352R/HDC2080 measurement

Use the following code relacing mainThread in i2copt3001_cpp.cpp of i2copt3001_cpp example to read LPSTK-CC1352R/HDC2080 temperature/humidity measurement

/*
 *  ======== mainThread ========
 */
uint8_t reg_addr, high_id, low_id, drdy, high_temp, low_temp, high_humi, low_humi;
uint16_t temp16, humi16;
float temperature, humidity;
uint8_t drdy_int_conf[2];

void *mainThread(void *arg0)
{
    uint16_t sample;
    int32_t luxValue;
    uint8_t data;
    uint8_t addr;

    /* I2C structures*/
    I2C_Handle      i2c;
    I2C_Params      i2cParams;
    I2C_Transaction i2cTransaction;

    /* Call driver init functions */
    Display_init();
    GPIO_init();
    I2C_init();

    /* Open the UART display for output */
    display = Display_open(Display_Type_UART, NULL);
    if (display == NULL) {
        while (1);
    }

    /* Create I2C for usage */
    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_400kHz;
    i2c = I2C_open(CONFIG_I2C_OPT3001, &i2cParams);
    if (i2c == NULL) {
        Display_printf(display, 0, 0, (char *)"Error Initializing I2C!\n");
        while (1);
    }
    else {
        Display_printf(display, 0, 0, (char *)"I2C Initialized!");
    }

    i2cTransaction.targetAddress=0x41; //HDC2080 device address

    /* Read Device ID High byte */
    reg_addr=0xFF;
    i2cTransaction.writeBuf = &reg_addr;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = &high_id;
    i2cTransaction.readCount = 1;
    if (I2C_transfer(i2c, &i2cTransaction)){
        Display_printf(display, 0, 0,
                        (char *)"I2C device High ID 0x%x!", high_id);
    }

    /* Read Device ID Low byte */
    reg_addr=0xFE;
    i2cTransaction.writeBuf = &reg_addr;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = &low_id;
    i2cTransaction.readCount = 1;
    if (I2C_transfer(i2c, &i2cTransaction)){
        Display_printf(display, 0, 0,
                        (char *)"I2C device Low ID 0x%x!", low_id);
    }

    while(1)
    {
        //Write drdy_int_conf to 5 samples per second
        drdy_int_conf[0]=0x0F;
        drdy_int_conf[1]=0x01;
        i2cTransaction.writeBuf = drdy_int_conf;
        i2cTransaction.writeCount = 2;
        i2cTransaction.readBuf = &drdy;
        i2cTransaction.readCount = 0;
        if (I2C_transfer(i2c, &i2cTransaction)){
            Display_printf(display, 0, 0,
                            (char *)"Data ready byte? 0x%x", drdy);
        }

        //read if data is ready
        reg_addr=0x04;
        i2cTransaction.writeBuf = &reg_addr;
        i2cTransaction.writeCount = 1;
        i2cTransaction.readBuf = &drdy;
        i2cTransaction.readCount = 1;
        if (I2C_transfer(i2c, &i2cTransaction)){
            Display_printf(display, 0, 0,
                            (char *)"Data ready byte? 0x%x", drdy);
        }

        if(drdy&0x80)
        {
            //Data is ready and start to read temperature and temperature data
            /* Read Temperature Low byte */
            reg_addr=0x00;
            i2cTransaction.writeBuf = &reg_addr;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = &low_temp;
            i2cTransaction.readCount = 1;
            if (I2C_transfer(i2c, &i2cTransaction)){
                Display_printf(display, 0, 0,
                                (char *)"Temperature Low byte 0x%x!", low_temp);
            }

            /* Read Temperature High byte */
            reg_addr=0x01;
            i2cTransaction.writeBuf = &reg_addr;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = &high_temp;
            i2cTransaction.readCount = 1;
            if (I2C_transfer(i2c, &i2cTransaction)){
                Display_printf(display, 0, 0,
                                (char *)"Temperature high byte 0x%x!", high_temp);
            }

            temp16=(uint16_t)high_temp;
            temp16=(temp16<<8)|((uint16_t)low_temp);
            temperature = (((float)temp16)/65535)*165-40.5;
            Display_printf(display, 0, 0,(char *)"Temperature: %.1f", temperature);

            /* Read Humidity Low byte */
            reg_addr=0x02;
            i2cTransaction.writeBuf = &reg_addr;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = &low_humi;
            i2cTransaction.readCount = 1;
            if (I2C_transfer(i2c, &i2cTransaction)){
                Display_printf(display, 0, 0,
                                (char *)"Temperature Low byte 0x%x.1!", low_humi);
            }

            /* Read Humidity High byte */
            reg_addr=0x03;
            i2cTransaction.writeBuf = &reg_addr;
            i2cTransaction.writeCount = 1;
            i2cTransaction.readBuf = &high_humi;
            i2cTransaction.readCount = 1;
            if (I2C_transfer(i2c, &i2cTransaction)){
                Display_printf(display, 0, 0,
                                (char *)"Temperature high byte 0x%x!", high_humi);
            }

            humi16=(uint16_t)high_humi;
            humi16=(humi16<<8)|((uint16_t)low_humi);
            humidity = (((float)humi16)/65535)*100;
            Display_printf(display, 0, 0,(char *)"Humidity: %f", humidity);
        }
    }
    return (NULL);
}
 

No comments:

Post a Comment