Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions general/include/p3t1755.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@

#ifndef p3t1755_H
#define p3t1755_H
#include <stdint.h>
#include <math.h>
#include <stdint.h>

// 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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
#endif
19 changes: 15 additions & 4 deletions general/src/p3t1755.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

#include "p3t1755.h"
#include "c_utils.h"
#include "u_tx_debug.h"
#include <stdint.h>

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,
Expand All @@ -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];
Expand All @@ -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;
}

Expand Down Expand Up @@ -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));
}
}
Loading