Hello:
I have one HDC1050 and it's connect CC2650DK via i2C,
Does any sample code ? Thank you.
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.
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
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:
/* * 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
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();
}
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 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.
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);
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 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) ?