diff --git a/general/include/p3t1755.h b/general/include/p3t1755.h index 7ecf3b3f..c62fbb41 100644 --- a/general/include/p3t1755.h +++ b/general/include/p3t1755.h @@ -5,18 +5,22 @@ #ifndef p3t1755_H #define p3t1755_H -#include #include +#include // REGISTERS -// Temperature register: contains two 8-bit data bytes; to store the measured Temp data. +// Temperature register: contains two 8-bit data bytes; to store the measured +// Temp data. #define p3t1755_TEMPERATURE 0x00 // read only -// Configuration register: contains a single 8-bit data byte; to set the device operating condition +// Configuration register: contains a single 8-bit data byte; to set the device +// operating condition #define p3t1755_CONFIGURATION 0x01 -// T_low register: Hysteresis register, it contains two 8-bit data bytes to store the hysteresis T_low limit; default = 75 °C. +// T_low register: Hysteresis register, it contains two 8-bit data bytes to +// store the hysteresis T_low limit; default = 75 °C. #define p3t1755_T_LOW 0x02 -// T_high register: Overtemperature shut down threshold register, it contains two 8-bit data bytes to -// store the overtemperature shutdown T_high limit; default = 80 °C. +// T_high register: Overtemperature shut down threshold register, it contains +// two 8-bit data bytes to store the overtemperature shutdown T_high limit; +// default = 80 °C. #define p3t1755_T_HIGH 0x03 // TEMPERATURE REGISTER FORMAT @@ -61,6 +65,8 @@ typedef struct { ReadPtr read; } p3t1755_t; +float p3t1755_raw_to_celsius(uint16_t raw); + void p3t1755_init(p3t1755_t *p3t, WritePtr write, ReadPtr read, uint16_t dev_addr); @@ -90,4 +96,4 @@ int p3t1755_read_low_temp(p3t1755_t *p3t, float *temp_c); int p3t1755_set_high_temp(p3t1755_t *p3t, float temp_c); int p3t1755_set_low_temp(p3t1755_t *p3t, float temp_c); -#endif \ No newline at end of file +#endif diff --git a/general/src/p3t1755.c b/general/src/p3t1755.c index 3dbccf1e..a8945f98 100644 --- a/general/src/p3t1755.c +++ b/general/src/p3t1755.c @@ -5,13 +5,15 @@ #include "p3t1755.h" #include "c_utils.h" +#include "u_tx_debug.h" +#include void p3t1755_init(p3t1755_t *p3t, WritePtr write, ReadPtr read, uint16_t dev_addr) { p3t->write = write; p3t->read = read; - p3t->dev_addr = dev_addr; + p3t->dev_addr = dev_addr << 1; } int p3t1755_write_reg(p3t1755_t *p3t, uint16_t reg, uint8_t *data, @@ -26,6 +28,15 @@ int p3t1755_read_reg(p3t1755_t *p3t, uint16_t reg, uint8_t *data, return p3t->read(p3t->dev_addr, reg, data, length); } +inline float p3t1755_raw_to_celsius(uint16_t raw) +{ + PRINTLN_INFO("raw temp: %d", raw); + if (raw & 1 << 11) { // Check if sign bit is set |-> t<0 + return -(raw * p3t1755_TEMP_RESOLUTION); + } else + return (raw * p3t1755_TEMP_RESOLUTION); +} + int p3t1755_read_temperature(p3t1755_t *p3t, float *temp_c) { uint8_t temp_reg[2]; @@ -36,8 +47,8 @@ int p3t1755_read_temperature(p3t1755_t *p3t, float *temp_c) return status; } - *temp_c = p3t1755_RAW_TO_CELSIUS( - uint8_to_uint16(temp_reg[0], temp_reg[1])); + *temp_c = p3t1755_raw_to_celsius( + uint8_to_uint16(temp_reg[0] >> 4, temp_reg[1] >> 4)); return status; } @@ -225,4 +236,4 @@ int p3t1755_set_low_temp(p3t1755_t *p3t, float temp_c) return p3t1755_write_reg(p3t, p3t1755_T_LOW, temp_data, sizeof(temp_data)); -} \ No newline at end of file +}