Question Details

No question body available.

Tags

c++ stm32 i2c

Answers (1)

Accepted Answer Available
Accepted Answer
September 9, 2025 Score: 9 Rep: 4,445 Quality: Expert Completeness: 80%
static constexpr uint8t REGTEMP = 0x00;

Based on datasheet page 10, there are two registers for reading the temperature measurement, register 0xE3 (with Holding the master during measurement, i.e. with clock stretching), or register 0xF3 without clock stretching. Your code is trying to read temperature measurement from register 0x00 which does not exist and therefore causing the device to return error. HTU21 register table With register 0xF3, the measurement takes up to 50ms to complete(see page 4 of the datasheet). The temperature data is returned in 3 bytes, first two bytes as the raw temperature data, plus one byte checksum, if you only read 2 bytes (which is acceptable), checksum will not be sent by the HTU21 (see page 11 of the datasheet).


static constexpr uint8t REGTEMPNOHOLD = 0xF3;
char msg[80]{0};

while (1) { uint8t cmd = REGTEMPNOHOLD; int ret = HALI2CMasterTransmit(&hi2c1, HTU21DADDR, &cmd, 1, HALMAXDELAY); if(ret != HALOK) { snprintf(msg, sizeof(msg), "Transmit status: %d\r\n", ret); } else { HALDelay(50); // as per datasheet page 4 uint8t rxdata[3]{0}; ret = HALI2CMasterReceive(&hi2c1, HTU21DADDR, rxdata, 3, HALMAXDELAY); if(ret != HALOK) snprintf(msg, sizeof(msg), "ERROR Rx %d\r\n", ret); else { int16t rawtemp = (rx_data[0]