diff --git a/drivers/lpsxxx/lpsxxx.c b/drivers/lpsxxx/lpsxxx.c index 8b0bd4929d..7f84c4fd98 100644 --- a/drivers/lpsxxx/lpsxxx.c +++ b/drivers/lpsxxx/lpsxxx.c @@ -30,6 +30,7 @@ #include "periph/i2c.h" #include "lpsxxx.h" #include "lpsxxx_internal.h" +#include "macros/math.h" #define ENABLE_DEBUG 0 #include "debug.h" @@ -41,12 +42,14 @@ /** * @brief temperature base value and divider for norming temperature output + * + * @details temperature base is given in centi-degree-celsius */ #if MODULE_LPS331AP || MODULE_LPS25HB -#define TEMP_BASE (42.5f) +#define TEMP_BASE (4250U) /* = 42.5 C */ #define TEMP_DIVIDER (480U) #else -#define TEMP_BASE (0.0f) +#define TEMP_BASE (0U) #define TEMP_DIVIDER (100U) #endif @@ -128,8 +131,8 @@ int lpsxxx_init(lpsxxx_t *dev, const lpsxxx_params_t * params) int lpsxxx_read_temp(const lpsxxx_t *dev, int16_t *temp) { uint8_t tmp; - int16_t val = 0; - float res = TEMP_BASE; /* reference value -> see datasheet */ + int32_t val = 0; + uint16_t res = TEMP_BASE; /* reference value -> see datasheet */ i2c_acquire(DEV_I2C); if (i2c_read_reg(DEV_I2C, DEV_ADDR, LPSXXX_REG_TEMP_OUT_L, &tmp, 0) < 0) { @@ -147,13 +150,15 @@ int lpsxxx_read_temp(const lpsxxx_t *dev, int16_t *temp) i2c_release(DEV_I2C); val |= ((uint16_t)tmp << 8); - DEBUG("[lpsxxx] read_temp: raw data %08" PRIx32 "\n", (uint32_t)val); + DEBUG("[lpsxxx] read_temp: raw data %08" PRIx32 "\n", val); - /* compute actual temperature value in °C */ - res += ((float)val) / TEMP_DIVIDER; + /* convert val to c°C */ + val *= 100; - /* return temperature in c°C */ - *temp = (int16_t)(res * 100); + /* compute actual temperature value in c°C */ + res += DIV_ROUND(val, TEMP_DIVIDER); + + *temp = res; return LPSXXX_OK; }