Other Parts Discussed in Thread: CC2640
Tool/software: TI C/C++ Compiler
Hello, I got the SHT31-D sensor (Humidity & Temperature), I've been working to interface it with the LAUNCHXL-CC2640R2 with no success...
There's a library example for Arduino which works fine. But when trying to port the library to CCS, I simply can't get it working...
The quantity reported by the sensor is always negative (which doesn't make sense).
This is my code:
#define SHT31_SENSOR_ADDR 0x44 #define SHT31_MEAS_HIGHREP 0x2400 #define SHT31_SOFTRESET 0x30A2
void *mainThread(void *arg0) { unsigned int i; float humidity, temp; uint8_t txBuffer[2]; uint8_t rxBuffer[6]; uint16_t ST, SRH; I2C_Handle i2c; I2C_Params i2cParams; I2C_Transaction i2cTransaction; /* Call driver init functions */ Display_init(); GPIO_init(); I2C_init(); /* Configure the LED pin */ GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW); /* Open the HOST display for output */ display = Display_open(Display_Type_UART, NULL); if (display == NULL) { while (1); } /* Turn on user LED */ GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON); Display_printf(display, 0, 0, "Starting the SHT31-D Sensor (Temp & Humidity)\n"); /* Create I2C for usage */ I2C_Params_init(&i2cParams); i2cParams.bitRate = I2C_100kHz; //400kHz i2c = I2C_open(Board_I2C_TMP, &i2cParams); if (i2c == NULL) { Display_printf(display, 0, 0, "Error Initializing I2C\n"); while (1); //return false; } else { txBuffer[0]=SHT31_SOFTRESET >>8; txBuffer[1]=SHT31_SOFTRESET & 0xFF; i2cTransaction.slaveAddress = SHT31_SENSOR_ADDR; i2cTransaction.writeCount = sizeof(txBuffer); i2cTransaction.writeBuf = txBuffer; sleep(0.5); Display_printf(display, 0, 0, "I2C Initialized!\n"); //return true; } /* Take 20 samples and print them out onto the console */ for (i = 0; i< 20; i++){ txBuffer[0] = SHT31_MEAS_HIGHREP >> 8; //0x2400 txBuffer[1] = SHT31_MEAS_HIGHREP & 0xFF; i2cTransaction.slaveAddress = SHT31_SENSOR_ADDR; i2cTransaction.writeCount = sizeof(txBuffer); i2cTransaction.writeBuf = txBuffer; //Wire.write(SHT31_MEAS_HIGHREP >> 8); i2cTransaction.readCount = sizeof(rxBuffer); i2cTransaction.readBuf = rxBuffer; sleep(0.5); if(I2C_transfer(i2c, &i2cTransaction)){ ST = rxBuffer[0]; ST <<= 8; ST |= rxBuffer[1]; if (rxBuffer[2] != crc8(rxBuffer, 2)) i=20; //return false; SRH = rxBuffer[3]; SRH <<= 8; SRH |= rxBuffer[4]; if (rxBuffer[5] != crc8(rxBuffer+3, 2)) i=20;//return false; double stemp = ST; stemp *= 175; stemp /= 0xffff; stemp = -45 + stemp; Display_printf(display, 0, 0, "Temperature %u *C = %d \n", i, stemp); double shum = SRH; shum *= 100; shum /= 0xFFFF; Display_printf(display, 1, 0, "Humidity %u %%RH = %d \n", i, shum); } else { Display_printf(display, 0, 0, "I2C Bus fault\n"); } /* Sleep for 1 second */ sleep(1); } /* Deinitialized I2C */ I2C_close(i2c); Display_printf(display, 0, 0, "I2C closed!\n"); return (NULL); }
The sensor-board connections:
SCL = DIO04
SDA = DIO05
Thanks for the help!