Wednesday, March 2, 2016

Basic example to use OPT3001 on CC2650 LaunchPad

1. Connect OPT3001EVM 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 OPT3001EVM Lux data.

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 8="" br=""> | 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();
}


 

1 comment:

  1. Hi.
    Can you help with the code for CC2530 the opt3001 sensor?

    ReplyDelete