44 lines
1.5 KiB
C
44 lines
1.5 KiB
C
#include "htu21d.h"
|
|
|
|
#define HTU21D_REG_MEASURE_TEMP_HOLD 0xE3
|
|
#define HTU21D_REG_MEASURE_TEMP_NOHOLD 0xF3
|
|
|
|
#define HTU21D_REG_MEASURE_HUMID_HOLD 0xE5
|
|
#define HTU21D_REG_MEASURE_HUMID_NOHOLD 0xF5
|
|
|
|
#define HTU21D_REG_READ_USER_REG 0xE7
|
|
#define HTU21D_REG_WRITE_USER_REG 0xE6
|
|
|
|
#define HTU21D_REG_SOFT_RESET 0xFE
|
|
|
|
#define HTU21D_ERROR_CHECK(x) \
|
|
if (x != HTU21D_OK) return HTU21D_ERROR
|
|
|
|
htu21d_ret_t htu21d_init(htu21d_t *htu) {
|
|
uint8_t tx_buf = HTU21D_REG_SOFT_RESET;
|
|
HTU21D_ERROR_CHECK(htu->cb.write_cb(htu->user_data, &tx_buf, 0x01));
|
|
return HTU21D_OK;
|
|
}
|
|
|
|
htu21d_ret_t htu21d_measure(htu21d_t *htu, htu21d_result_t *result) {
|
|
uint8_t tx_buf = HTU21D_REG_MEASURE_TEMP_NOHOLD;
|
|
uint8_t rx_data[3];
|
|
|
|
HTU21D_ERROR_CHECK(htu->cb.write_cb(htu->user_data, &tx_buf, 0x01));
|
|
HTU21D_ERROR_CHECK(htu->cb.delay_cb(htu->user_data, 50));
|
|
HTU21D_ERROR_CHECK(htu->cb.read_cb(htu->user_data, rx_data, 0x03));
|
|
|
|
uint16_t raw_data = (rx_data[0] << 8U) | (rx_data[1] & 0xFC);
|
|
result->temperature = (float)raw_data / 65536.0 * 175.72 - 46.85;
|
|
|
|
tx_buf = HTU21D_REG_MEASURE_HUMID_NOHOLD;
|
|
|
|
HTU21D_ERROR_CHECK(htu->cb.write_cb(htu->user_data, &tx_buf, 0x01));
|
|
HTU21D_ERROR_CHECK(htu->cb.delay_cb(htu->user_data, 50));
|
|
HTU21D_ERROR_CHECK(htu->cb.read_cb(htu->user_data, rx_data, 0x03));
|
|
|
|
raw_data = (rx_data[0] << 8U) | (rx_data[1] & 0xFC);
|
|
result->humidity = (float)raw_data / 65536.0 * 125.0 - 6.0;
|
|
|
|
return HTU21D_OK;
|
|
} |