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.
I'm trying to use temperature sensor SHT21 in Sensor Controller Studio.
Code:
i2cStart();
i2cTx(I2C_OP_WRITE | ALS_I2C_ADDR); //0x40<<1
i2cTx(TRIG_T_MEASUREMENT_HM); //0xe3
i2cStop();
fwDelayUs(100, FW_DELAY_RANGE_100_MS);
i2cStart();
i2cTx(I2C_OP_READ | ALS_I2C_ADDR);
if (state.i2cStatus == 0x0000) {
U16 resultH;
U16 resultL;
U16 CRC;
i2cRxAck(resultH);
i2cRxAck(resultL);
i2cRxNack(CRC);
output.value = (resultH << 8) | resultL;
}
i2cStop();
Sometimes (40% of samples) it returns right value, sometimes (40%) - 0, sometimes (20%) - random value.
Tried to play with Delays, RepeatedStart(), i2cStatus - no luck.
Same code with same sensor on Arduino board works fine.
What i'm doing wrong?
Hello Rustem,
If all the above fails can you connect a logic analyzer to the I2C lines and capture some traffic to further evaluate what is going wrong?
Hello, Eirik! Thank you for quick answer!
NoHoldMaster + increased Delay solves my problem!
Working code:
i2cStart();
i2cTx(I2C_OP_WRITE | ALS_I2C_ADDR); //0x40<<1
i2cTx(TRIG_T_MEASUREMENT); //0xf3
i2cStop();
fwDelayUs(100000, FW_DELAY_RANGE_100_MS);
i2cStart();
i2cTx(I2C_OP_READ | ALS_I2C_ADDR);
if (state.i2cStatus == 0x0000) {
U16 resultH;
U16 resultL;
U16 CRC;
i2cRxAck(resultH);
i2cRxAck(resultL);
i2cRxNack(CRC);
output.value = (resultH << 8) | resultL;
}
i2cStop();