Friday, February 26, 2016

Basic example to use HDC1080 on CC2650 LaunchPad

1. Connect HDC1080 SCL to CC2650 IOID_6 and SDA to IOID_5.



2. Replace the following code to taskFxn in i2ctmp006.c (in CC26xx I2C TMP006 example) will make CC2650 LaunchPad to read HDC1080 temperature/humidity data.

Void taskFxn(UArg arg0, UArg arg1)
{
    unsigned int    i;
    uint16_t        temperature;
    uint16_t        humidity;
    uint8_t         txBuffer[4];
    uint8_t         rxBuffer[4];
    I2C_Handle      i2c;
    I2C_Params      i2cParams;
    I2C_Transaction i2cTransaction;

    /* Create I2C for usage */
    I2C_Params_init(&i2cParams);
    i2cParams.bitRate = I2C_400kHz;
    i2c = I2C_open(Board_I2C_TMP, &i2cParams);
    if (i2c == NULL) {
        System_abort("Error Initializing I2C\n");
    }
    else {
        System_printf("I2C Initialized!\n");
    }

    //Read HDC1080 device ID
    txBuffer[0] = 0xFF;
    txBuffer[1] = 0x10;
    txBuffer[2] = 0x00;
    i2cTransaction.slaveAddress = 0x40;//HDC1080 ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction)){
        System_printf("Device ID: %x%x\n", rxBuffer[0], rxBuffer[1]);
    } else {
        System_printf("Device ID fail!\n");
    }
    if (I2C_transfer(i2c, &i2cTransaction)){
        System_printf("Device ID: %x%x\n", rxBuffer[0], rxBuffer[1]);
    } else {
        System_printf("Device ID fail!\n");
    }


    //Read HDC1080 ADDR Manufacture ID
    txBuffer[0] = 0xFE;
    i2cTransaction.slaveAddress = 0x40;//HDC1080 ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;
    if (I2C_transfer(i2c, &i2cTransaction)) {
        System_printf("Manufacture ID: %x%x\n", rxBuffer[0], rxBuffer[1]);
    } else {
        System_printf("Manufacture ID fail!\n");
    }

    //Config HDC1080
    txBuffer[0] = 0x02;
    txBuffer[1] = 0x10;
    txBuffer[2] = 0x00;
    i2cTransaction.slaveAddress = 0x40;//Board_TMP006_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 3;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 0;

    if (I2C_transfer(i2c, &i2cTransaction)){
        System_printf("Config write!\n");
        //System_printf("Conf: 0x%x 0x%x\n", rxBuffer[0], rxBuffer[1]);
    } else {
        System_printf("Config fail!\n");
    }

    //Read HDC1080 Config
    txBuffer[0] = 0x02;
    txBuffer[1] = 0x10;
    txBuffer[2] = 0x00;
    i2cTransaction.slaveAddress = 0x40;//Board_TMP006_ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;
    if (I2C_transfer(i2c, &i2cTransaction)){
        System_printf("Config read!\n");
        System_printf("Conf: 0x%x 0x%x\n", rxBuffer[0], rxBuffer[1]);
    } else {
        System_printf("Config fail!\n");
    }


    /* Take 20 samples and print them out onto the console */
    for (i = 0; i < 20; i++) {
        txBuffer[0] = 0x00;
        i2cTransaction.slaveAddress = 0x40;//HDC1080 ADDR;
        i2cTransaction.writeBuf = txBuffer;
        i2cTransaction.writeCount = 1;
        i2cTransaction.readBuf = rxBuffer;
        i2cTransaction.readCount = 0;
        if (I2C_transfer(i2c, &i2cTransaction)) {
            System_printf("Temp/Humd!\n");
        } else {
            System_printf("Temp/Humd Fail!\n");
        }

        Task_sleep(1000000 / Clock_tickPeriod);
        txBuffer[0] = 0x00;
        i2cTransaction.slaveAddress = 0x40;//HDC1080 ADDR;
        i2cTransaction.writeBuf = txBuffer;
        i2cTransaction.writeCount = 0;
        i2cTransaction.readBuf = rxBuffer;
        i2cTransaction.readCount = 4;
        if (I2C_transfer(i2c, &i2cTransaction)) {
            /* Extract degrees C from the received data */
            temperature = rxBuffer[0];
            temperature = (temperature<<8 br="">            temperature |= rxBuffer[1];
            temperature = ((double)(int16_t)temperature / 65536)*165 - 40;

            System_printf("Temperature %u: %d (C)\n", i, temperature);

            /* Extract humidity RH from the received data */
            humidity = rxBuffer[2];
            humidity = (humidity<<8 br="">            humidity |= rxBuffer[3];
            //-- calculate relative humidity [%RH]
            humidity = ((double)humidity / 65536)*100;

            System_printf("Humidity %u: %d (RH)\n", i, humidity);
        }
        else {
            System_printf("I2C Bus fault\n");
        }

        System_flush();
        Task_sleep(1000000 / Clock_tickPeriod);
    }

    /* Deinitialized I2C */
    I2C_close(i2c);
    System_printf("I2C closed!\n");

    System_flush();
}
 

No comments:

Post a Comment