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.

cc2650DK and HDC1050

Other Parts Discussed in Thread: HDC1050, HDC1000, TIDA-00374, TMP006, HDC1080, SYSBIOS, OPT3001, CC2650

Hello:

I have one HDC1050 and it's connect CC2650DK via i2C,

Does any sample code ? Thank you.

  • Hi Tzuyu,

    For the HDC1050 you can use the same reference source code you have used for the HDC1000.

    HDC1050 and HDC1000 are software compatible, with the only difference that HDC1050 doesn't have the DRDYn interrupt pin. For this reason in the HDC_measurement() function in hdc1000.c file, you should replace the follow instruction:

    //Wait for nDRDY_RDY pin to signal measurement is ready to read
      while(HDC_data_received == false);

    with a delay of 40msec:

    //Wait 40msec before that the measurement is ready to be read
      delay(40); // 40msec

    Please let me know if this reply to your question or if you need further information.

    Best regards,

    Carmine

  • There are no HDC_measurement( ) function in sensor_hdc1000.c
    Can you show me where is hdc1000.c , thank you.
  • Hi Tzuyu,

    as suggested in your previous post,  you should have already downloaded and installed the source code files of the TIDA-00374 from the following link:

    www.ti.com/.../tidc949

    The HDC1000 specific code is in C:\ti\TIDA-00374_firmware\TIDA-00374_firmware\source\tasks\hdc1000.c.

    Attached I have uploaded the files for your reference:

    hdc1000.h

    hdc1000.c
    /*
     * Copyright (c) 2014-2015, Texas Instruments Incorporated - http://www.ti.com
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*****************************************************************************
    * INCLUDES
    */
    #include <ti/drivers/I2C.h>
    #include <ti/drivers/PIN.h>
    #include <ti/drivers/pin/PINCC26XX.h>
    #include "Board.h"
    #include "hdc1000.h"
    
    #include "string.h"
    
    /*****************************************************************************
    * GLOBAL VARIABLES
    */
    extern PIN_Handle       DONE_handle;
    
    /*****************************************************************************
    * LOCAL FUNCTION PROTOTYPES
    */
    
    /*****************************************************************************
    * LOCAL VARIABLES
    */
    
    //I2C variables
    I2C_Handle              AppI2cHandle;
    I2C_Params              AppI2cParams;
    I2C_Transaction         i2cTransaction;
    uint8_t                 txBuf[3] = {0};
    bool                    status = false;
    
    //GPIO variables
    static PIN_State        nDRDY_state;
    static PIN_Handle       nDRDY_handle;
    PIN_Config              nDRDYPinTable[] = {
        nDRDY_HDC | PIN_INPUT_EN | PIN_NOPULL | PIN_HYSTERESIS,
        PIN_TERMINATE                               
    };
    
    //HDC variables
    volatile bool           HDC_data_received = false;
    
    
    
    /*****************************************************************************
    * FUNCTIONS
    */
    /*****************************************************************************
    * @brief    HWI interrupt function to trigger on nDRDY pin
    *
    * @return   None
    ******************************************************************************/
    void nDRDY_HwiFxn(PIN_Handle hPin, PIN_Id pinId){
      if(pinId == nDRDY_HDC){
        HDC_data_received = true;
      }
    }
    
    /*****************************************************************************
    * @brief    Initialize HDC I2C and GPIO driver
    *
    * @return   None
    ******************************************************************************/
    void HDC_peripheral_init(void){
      //Initialize I2C
      I2C_Params_init(&AppI2cParams);
      AppI2cParams.transferMode = I2C_MODE_BLOCKING;
      AppI2cParams.transferCallbackFxn = NULL;
      AppI2cHandle = I2C_open(CC2650_I2C0, NULL);  
      if(AppI2cHandle == NULL){
         /* Error opening I2C */
      }
      
      //Initialize nDRDY_HDC
      nDRDY_handle = PIN_open(&nDRDY_state, nDRDYPinTable);
      PIN_registerIntCb(nDRDY_handle, &nDRDY_HwiFxn);
      PIN_setConfig(nDRDY_handle, PIN_BM_IRQ, nDRDY_HDC | PIN_IRQ_NEGEDGE);  
    }
    
    /*****************************************************************************
    * @brief    Initialize HDC1000 and configure the temp/humidity resolution
    *
    * @return   None
    ******************************************************************************/
    void HDC_config_init(void){
      
      uint8_t tries = 0;
      status = false;  
      
      while(status == false){
        //Write to configuration register
        txBuf[0] = CONFIGURATION_ADDRESS;
        txBuf[1] = TEMP_RH_11BIT_MSB;
        txBuf[2] = TEMP_RF_11BIT_LSB;
      
        i2cTransaction.writeBuf = txBuf;
        i2cTransaction.writeCount = 3;
        i2cTransaction.readBuf = 0;
        i2cTransaction.readCount = 0;
        i2cTransaction.slaveAddress = HDC1000_ADDRESS;
        status = I2C_transfer(AppI2cHandle, &i2cTransaction);  
        
        tries++;
           
        if(status == false){
          //ERROR: HDC is not responding
          
          //Tried 5 times, power off the uC
          if(tries == 5){
            PIN_setOutputValue(DONE_handle, DONE, 1);
          }
            
          //If less than 5 times, wait 1ms and try again
          CPUdelay(10000);        
        }
      }
    }
    
    /*****************************************************************************
    * @brief    Return the HDC1000 manufacturer ID
    *
    * @return   None
    * @param    buffer will hold manufacturer ID
    ******************************************************************************/
    void HDC_revision(uint8_t* buffer){
      
      status = false;
      txBuf[0] = MANUFACTURER_ID_ADDRESS;
      
      i2cTransaction.writeBuf = txBuf;
      i2cTransaction.writeCount = 1;
      i2cTransaction.readBuf = buffer;
      i2cTransaction.readCount = 2;
      i2cTransaction.slaveAddress = HDC1000_ADDRESS;
      status = I2C_transfer(AppI2cHandle, &i2cTransaction);
      
      if(status == false){
        /* Error */
      }  
    }
    
    /*****************************************************************************
    * @brief    Return the configuration register content
    *
    * @return   None
    * @param    buffer will contain configuration register content
    ******************************************************************************/
    void HDC_configuration_read(uint8_t* buffer){
      
      status = false;
      txBuf[0] = CONFIGURATION_ADDRESS;
      
      i2cTransaction.writeBuf = txBuf;
      i2cTransaction.writeCount = 1;
      i2cTransaction.readBuf = buffer;
      i2cTransaction.readCount = 2;
      i2cTransaction.slaveAddress = HDC1000_ADDRESS;
      status = I2C_transfer(AppI2cHandle, &i2cTransaction);
      
      if(status == false){
        /* Error */
      }  
    }
    
    /*****************************************************************************
    * @brief    Start temperature and humidity measurement, wait for the 
    *           nDRDY_HDC pin to go low indicating a measurement is ready 
    *           for reading, read temperature and humidity registers
    *
    * @return   None
    * @param    buffer will contain both temperature and humidity measurement
    *           [0] Temperature MSB
    *           [1] Temperature LSB
    *           [2] Relative humidity MSB
    *           [3] Relative humidity LSB
    ******************************************************************************/
    void HDC_measurement(uint8_t* buffer){
      
      status = false;
      HDC_data_received = false;
      txBuf[0] = TEMPERATURE_ADDRESS;  
      
      //Write data to TEMP register to trigger a measurement
      i2cTransaction.writeBuf = txBuf;
      i2cTransaction.writeCount = 1;
      i2cTransaction.readBuf = buffer;
      i2cTransaction.readCount = 0;
      i2cTransaction.slaveAddress = HDC1000_ADDRESS;
      status = I2C_transfer(AppI2cHandle, &i2cTransaction);  
      if(status == false){
        /* Error */
      }  
      
      //Wait for nDRDY_RDY pin to signal measurement is ready to read
      while(HDC_data_received == false);
      
      //Read data
      status = false;
      i2cTransaction.writeBuf = txBuf;
      i2cTransaction.writeCount = 0;
      i2cTransaction.readBuf = buffer;
      i2cTransaction.readCount = 4;
      i2cTransaction.slaveAddress = HDC1000_ADDRESS;
      status = I2C_transfer(AppI2cHandle, &i2cTransaction);
      if(status == false){
        /* Error */
      }  
    }

    Please let me know if it is ok now.

    Best regards,

    Carmine

  • Hello:
    Just out of curious , if I want to see HDC1050's data , where I can add
    System_printf("Temperature is %d (C)\n", temperature) ;
    System_printf("Humidity is %d \n", humidity) ;
    Thank you.
  • By the way , HDC1050 only SDA and SCL  need to define , but

    HDC1000 source code : Board.h has define nDRDY_HDC IOID_2 and DONE IOID_3 ?

  • Hi Tzuyu,
    I found a similar error due to the fact that the file board.h is not in the same directory of hdc1000.c.
    Please modify the line39 in hdc1000.c from #include "Board.h" to #include "../Board.h" and let me know if this work.
    Best regards,
    Carmine
  • Try to use I2C TMP006 example for CC26xx and replace the following code to taskFxn in i2ctmp006.c.

    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 (C)\n", rxBuffer[0], rxBuffer[1]);
        } else {
            System_printf("Device ID fail!\n");
        }
        if (I2C_transfer(i2c, &i2cTransaction)){
            System_printf("Device ID: %x%x (C)\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 (C)\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 (C)\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 (C)\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);
                temperature |= rxBuffer[1];
                temperature = ((double)(int16_t)temperature / 65536)*165 - 40;

                //System_printf("Temperature %u: %x%x (C)\n", i, rxBuffer[0], rxBuffer[1]);
                System_printf("Temperature %u: %d (C)\n", i, temperature);

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

                //System_printf("Humidity %u: %x%x (C)\n", i, rxBuffer[2], rxBuffer[3]);
                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();
    }

  • Dear Yikai:
    Task_sleep(1000000 / Clock_tickPeriod); //error
  • Do you replace the code to taskFxn in i2ctmp006.c? I build it without any problem.
  • My mistake,I miss include <ti/sysbios/knl/Clock.h>
    that why Task_sleep(1000000 / Clock_tickPeriod)
    will show error.
  • Does it work now?
  • Hello Yikai:

    Thanks for your help.

    This code is correct to read HDC1050 Sensor :

  • You are welcome and it's good to know it works.
  • HDC1050 has TIDA-00374_firmware can refer , How about OPT3001 ?
  • As I know, there is no similar source code reference for OPT3001. You should be able to revise my source code for HDC1050 and make it work for OPT3001.
  • How can I transfer 

     light_data= (val << 8) | (val>>8 &0xFF);

    into 

    txBuffer[0] = 0x00;
    i2cTransaction.slaveAddress = 0x44;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 0;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 4;

    if (I2C_transfer(i2c, &i2cTransaction)) {

    temperature = rxBuffer[0];
    temperature = (temperature<<8);
    temperature |= rxBuffer[1];
    temperature = ((double)(int16_t)temperature / 65536)*165 - 40;

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

    humidity = rxBuffer[2];
    humidity = (humidity<<8);
    humidity |= rxBuffer[3];
    humidity = ((double)humidity / 65536)*100;

    System_printf("Humidity %u: %d (RH)\n", i, humidity);

    }

  • I don't understand your question well. Can you elaborate?
  • I refer OPT3001 sample code:

    bool sensorOpt3001Read(uint16_t *rawData)

    {

     bool success;

     uint16_t val;

     success = sensorReadReg(REG_CONFIGURATION, (uint8_t *)&val, REGISTER_LENGTH);

     if (success)

     {

       success = (val & DATA_RDY_BIT) == DATA_RDY_BIT;

     }

     if (success)

     {

       success = sensorReadReg(REG_RESULT, (uint8_t*)&val, DATA_LENGTH);

     }

     if (success)

     {

       // Swap bytes

       *rawData = (val << 8) | (val>>8 &0xFF);  //read data

     }

     else

     {

       sensorSetErrorData((uint8_t*)rawData, DATA_LENGTH);

     }

     SENSOR_DESELECT();

     return success;

    }

    and according to your HDC1050 sample code :

    for(i=0; i<20; i++){
    txBuffer[0] = 0x00;
    i2cTransaction.slaveAddress = 0x40;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 0;
    I2C_transfer(i2c, &i2cTransaction)

    Task_sleep(1000000 / Clock_tickPeriod);

    txBuffer[0] = 0x00;
    i2cTransaction.slaveAddress = 0x40;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 0;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 4;
    if (I2C_transfer(i2c, &i2cTransaction)) {
    temperature = rxBuffer[0];
    temperature = (temperature<<8);
    temperature |= rxBuffer[1];
    temperature = ((double)(int16_t)temperature / 65536)*165 - 40;  //read temperature data

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


    humidity = rxBuffer[2];
    humidity = (humidity<<8);
    humidity |= rxBuffer[3];
    humidity = ((double)humidity / 65536)*100; //read humidity data

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

    System_flush();
    Task_sleep(1000000 / Clock_tickPeriod);

    }

    I want *rawData = (val << 8) | (val>>8 &0xFF) transfer into like up.

  • You can use the following code to do raw data conversion.

    uint16_t e, m;
    float lux;

    m = rawData & 0x0FFF;
    e = (rawData & 0xF000) >> 12;

    lux = m * (0.01 * exp2(e));
  • the result is :

  • Are you sure that your code read raw data correctly? Do you do printf to val to check it?
  • first , I declare

    then I send txbuffer like this:

    but result is :

  • What's your point?
  • the result is 0 lx , It's impossible
  • Can you dump rawData under different light condition to see if the reading changes?
  • I guess that is light sensor ?

    maybe I am not send  txBuffer[0] = 0x00;

    It's wrong ?

    for(i=0; i<20; i++){
    txBuffer[0] = 0x00;
    i2cTransaction.slaveAddress = 0x44;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 0;
    I2C_transfer(i2c, &i2cTransaction);

    Task_sleep(1000000 / Clock_tickPeriod);

    txBuffer[0] = 0x01;
    i2cTransaction.slaveAddress = 0x44;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 0;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 4;
    if (I2C_transfer(i2c, &i2cTransaction)) {
    val = rxBuffer[0];
    rawdata = (val << 8) | (val>>8 &0xFF);
    m = rawdata & 0x0FFF;
    e = (rawdata & 0xF000) >> 12;
    lux = m * (0.01 * exp2(e));  //show exp2 declared implicitly ?
    System_printf("light %u: %d (lx)\n", i, lux);

  • 1. Yes, U1 is OPT3001 light sensor.
    2. Try to following code:

    for(i=0; i<20; i++){
    txBuffer[0] = 0x01;
    i2cTransaction.slaveAddress = 0x44;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;
    I2C_transfer(i2c, &i2cTransaction);

    Task_sleep(1000000 / Clock_tickPeriod);

    txBuffer[0] = 0x00;
    i2cTransaction.slaveAddress = 0x44;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;

    if (I2C_transfer(i2c, &i2cTransaction)) {
    val = rxBuffer[0];
    rawdata = (val << 8) | (val>>8 &0xFF);
    m = rawdata & 0x0FFF;
    e = (rawdata & 0xF000) >> 12;
    lux = m * (0.01 * exp2(e)); //show exp2 declared implicitly ? You have to include math.h
    System_printf("light %u: %f (lx)\n", i, lux);
  • Do I miss some thing?

    The light still no value:

    I also declare rawdata val e m is uint16_t :

  • Try to write Configuration Register to 0xC4 0x10.
  • writeCount should be 2 and readCount should be 0.
  •    txBuffer[0] = 0xC4; //config

       txBuffer[1] = 0x10;

       i2cTransaction.slaveAddress = 0x44;

       i2cTransaction.writeBuf = txBuffer;

       i2cTransaction.writeCount = 2;

       i2cTransaction.readBuf = rxBuffer;

       i2cTransaction.readCount = 0;

       if (I2C_transfer(i2c, &i2cTransaction)) {

          System_printf("Conf:0x%x 0x%x(C)\n",rxBuffer[0],rxBuffer[1]);

       }else{

          System_printf("Config ID fail!\n");

       }

  • I user the following taskFxn to read OPT3001 with my CC2650 LaunchPad and it works fine.

    Void taskFxn(UArg arg0, UArg arg1)
    {
    unsigned int i;
    uint16_t lux;
    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");
    }

    Task_sleep(1000000 / Clock_tickPeriod);

    //Read OPT3001 device ID
    txBuffer[0] = 0x7F;
    txBuffer[1] = 0x10;
    txBuffer[2] = 0x00;
    i2cTransaction.slaveAddress = 0x44;//OPT3001 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 OPT3001 ADDR Manufacture ID
    txBuffer[0] = 0x7E;
    i2cTransaction.slaveAddress = 0x44;//OPT3001 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");
    }


    /* Take 20 samples and print them out onto the console */
    for (i = 0; i < 20; i++) {
    //Config OPT3001
    txBuffer[0] = 0x01;
    txBuffer[1] = 0xC4;
    txBuffer[2] = 0x10;
    i2cTransaction.slaveAddress = 0x44;//OPT3001 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 (C)\n", rxBuffer[0], rxBuffer[1]);
    } else {
    System_printf("Config write fail!\n");
    }

    //Read OPT3001 Config
    txBuffer[0] = 0x01;
    i2cTransaction.slaveAddress = 0x44;//OPT3001 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 %u: 0x%x 0x%x\n", i, rxBuffer[0], rxBuffer[1]);
    } else {
    System_printf("Config read fail!\n");
    }

    //Task_sleep(1000000 / Clock_tickPeriod);

    txBuffer[0] = 0x00;
    i2cTransaction.slaveAddress = 0x44;//OPT3001 ADDR;
    i2cTransaction.writeBuf = txBuffer;
    i2cTransaction.writeCount = 1;
    i2cTransaction.readBuf = rxBuffer;
    i2cTransaction.readCount = 2;
    if (I2C_transfer(i2c, &i2cTransaction)) {
    uint16_t e, m;
    float val;

    /* Extract degrees C from the received data */
    lux = rxBuffer[0];
    lux = (lux<<8);
    lux |= rxBuffer[1];

    m = lux & 0x0FFF;
    e = (lux & 0xF000) >> 12;
    val= (float)m * (0.01 * exp2(e));
    //System_printf("Lux %u: 0x%x 0x%x \n", i, rxBuffer[0], rxBuffer[1]);
    System_printf("Lux %u: %d \n", i, (int)val);
    }
    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();
    }
  • One more question, if I want to use HDC1050 and OPT3001 to read data at the same time ,
    which code that I need to revise ?
  • You can not read them at the same time since the share the same SCL and SDA I2C bus. You have to do them in sequential.
  • Can you show me how to do sequential ?
  • You can put my example code for HDC1050 and OPT3001 to two different function calls. Then, can them one after another in taskFxn.
  • You mean create two taskFxn , for example , read HDC1050 is call
    void taskHDC1050Fxn(UArg arg0, UArg arg1){....} and read OPT3001 is call
    void taskOPT3001Fxn(UArg arg0, UArg arg1){.....} or ?
  • It's like the followings:

    void HDC1050(void){...}
    void OPT3001(void){...}

    Void taskFxn(UArg arg0, UArg arg1)
    {
    ...
    HDC1050();
    OPT3001();
    ...
    }
  • By the way , If I want to disperse two sensors , for example , HDC1050 connect SDA0 and SCL0 , but OPT3001 connect to SDA1 and SCL1 , not use the same I2C pin in common , what do I do ?
  • I never do this so I cannot help.
  • That's OK. I find HDC1050 sensor need to read Device ID twice or data will be wrong.
  • Yes, that's why I put read Device ID twice in my example.
  • Now ,I use IOID_7 and  IOID_6 , if I want to use  IOID_8 and  IOID_9 , what do I do?

  • Just change Board_I2C0_SDA0 to IOID_8 and Board_I2C0_SCL0 to IOID9.
  • May I just straight use Board_I2C0_SDA1 and Board_I2C0_SCL1 ?
    I can let HDC1050 use IOID_8 and IOID_9 , and OPT3001 use IOID_7 and IOID_6 .
    no clash.
  • I think it should be OK.
  • I try to let OPT3001 use IOID_7(SDA) and IOID_6(SCL) , my code need to open I2C :

    i2c = I2C_open(CC2650_I2C0, &i2cParams);

    but of I want to HDC1050 use IOID_8(SDA) and IOID_9(SCL),

    should I need to revise i2c = I2C_open(CC2650_I2C0, &i2cParams) ?

  • Yes, you should do it.