Skip to content

Commit e11e8ee

Browse files
Feature/p3t1755 (#379)
Co-authored-by: Caio DaSilva <dasilva.cai@northeastern.edu>
1 parent 7967fa7 commit e11e8ee

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

general/include/p3t1755.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55

66
#ifndef p3t1755_H
77
#define p3t1755_H
8-
#include <stdint.h>
98
#include <math.h>
9+
#include <stdint.h>
1010

1111
// REGISTERS
12-
// Temperature register: contains two 8-bit data bytes; to store the measured Temp data.
12+
// Temperature register: contains two 8-bit data bytes; to store the measured
13+
// Temp data.
1314
#define p3t1755_TEMPERATURE 0x00 // read only
14-
// Configuration register: contains a single 8-bit data byte; to set the device operating condition
15+
// Configuration register: contains a single 8-bit data byte; to set the device
16+
// operating condition
1517
#define p3t1755_CONFIGURATION 0x01
16-
// T_low register: Hysteresis register, it contains two 8-bit data bytes to store the hysteresis T_low limit; default = 75 °C.
18+
// T_low register: Hysteresis register, it contains two 8-bit data bytes to
19+
// store the hysteresis T_low limit; default = 75 °C.
1720
#define p3t1755_T_LOW 0x02
18-
// T_high register: Overtemperature shut down threshold register, it contains two 8-bit data bytes to
19-
// store the overtemperature shutdown T_high limit; default = 80 °C.
21+
// T_high register: Overtemperature shut down threshold register, it contains
22+
// two 8-bit data bytes to store the overtemperature shutdown T_high limit;
23+
// default = 80 °C.
2024
#define p3t1755_T_HIGH 0x03
2125

2226
// TEMPERATURE REGISTER FORMAT
@@ -61,6 +65,8 @@ typedef struct {
6165
ReadPtr read;
6266
} p3t1755_t;
6367

68+
float p3t1755_raw_to_celsius(uint16_t raw);
69+
6470
void p3t1755_init(p3t1755_t *p3t, WritePtr write, ReadPtr read,
6571
uint16_t dev_addr);
6672

@@ -90,4 +96,4 @@ int p3t1755_read_low_temp(p3t1755_t *p3t, float *temp_c);
9096
int p3t1755_set_high_temp(p3t1755_t *p3t, float temp_c);
9197
int p3t1755_set_low_temp(p3t1755_t *p3t, float temp_c);
9298

93-
#endif
99+
#endif

general/src/p3t1755.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
#include "p3t1755.h"
77
#include "c_utils.h"
8+
#include "u_tx_debug.h"
9+
#include <stdint.h>
810

911
void p3t1755_init(p3t1755_t *p3t, WritePtr write, ReadPtr read,
1012
uint16_t dev_addr)
1113
{
1214
p3t->write = write;
1315
p3t->read = read;
14-
p3t->dev_addr = dev_addr;
16+
p3t->dev_addr = dev_addr << 1;
1517
}
1618

1719
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,
2628
return p3t->read(p3t->dev_addr, reg, data, length);
2729
}
2830

31+
inline float p3t1755_raw_to_celsius(uint16_t raw)
32+
{
33+
PRINTLN_INFO("raw temp: %d", raw);
34+
if (raw & 1 << 11) { // Check if sign bit is set |-> t<0
35+
return -(raw * p3t1755_TEMP_RESOLUTION);
36+
} else
37+
return (raw * p3t1755_TEMP_RESOLUTION);
38+
}
39+
2940
int p3t1755_read_temperature(p3t1755_t *p3t, float *temp_c)
3041
{
3142
uint8_t temp_reg[2];
@@ -36,8 +47,8 @@ int p3t1755_read_temperature(p3t1755_t *p3t, float *temp_c)
3647
return status;
3748
}
3849

39-
*temp_c = p3t1755_RAW_TO_CELSIUS(
40-
uint8_to_uint16(temp_reg[0], temp_reg[1]));
50+
*temp_c = p3t1755_raw_to_celsius(
51+
uint8_to_uint16(temp_reg[0] >> 4, temp_reg[1] >> 4));
4152
return status;
4253
}
4354

@@ -225,4 +236,4 @@ int p3t1755_set_low_temp(p3t1755_t *p3t, float temp_c)
225236

226237
return p3t1755_write_reg(p3t, p3t1755_T_LOW, temp_data,
227238
sizeof(temp_data));
228-
}
239+
}

0 commit comments

Comments
 (0)