Skip to content

Commit 5bec618

Browse files
committed
Added high resolution temp reading
1 parent 05844e4 commit 5bec618

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

adafruit_max31856.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ def __init__(
164164
spi: SPI,
165165
cs: DigitalInOut, # pylint: disable=invalid-name
166166
thermocouple_type: int = ThermocoupleType.K,
167+
baudrate: int = 500000,
167168
) -> None:
168-
self._device = SPIDevice(spi, cs, baudrate=500000, polarity=0, phase=1)
169+
self._device = SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=1)
169170

170171
# assert on any fault
171172
self._write_u8(_MAX31856_MASK_REG, 0x0)
@@ -253,6 +254,27 @@ def unpack_temperature(self) -> float:
253254

254255
return temp_float
255256

257+
def read_high_res_temp_degC(self) -> float:
258+
# Per datasheet, temperature resolution in °C per LSB
259+
resolution = 0.0078125
260+
261+
# Read the temperature registers
262+
raw_bytes = self._read_sequential_registers(_MAX31856_LTCBH_REG, 3)
263+
# Extract individual bytes from the byte array
264+
high_byte = raw_bytes[0] # First byte
265+
mid_byte = raw_bytes[1] # Second byte
266+
low_byte = raw_bytes[2] # Third byte
267+
268+
# Combine the bytes into a single 19-bit value
269+
combined = (high_byte << 11) | (mid_byte << 3) | (low_byte >> 5)
270+
271+
# Adjust for two's complement (sign extension for negative values)
272+
if combined & 0x40000: # Check if 19th bit is set (negative temperature)
273+
combined = combined - 0x80000
274+
275+
# Convert to temperature using the resolution
276+
return combined * resolution
277+
256278
@property
257279
def reference_temperature(self) -> float:
258280
"""Wait to retrieve temperature of the cold junction in degrees Celsius. (read-only)"""
@@ -363,6 +385,21 @@ def initiate_one_shot_measurement(self) -> None:
363385
# write it back with the new values, prompting the sensor to perform a measurement
364386
self._write_u8(_MAX31856_CR0_REG, conf_reg_0)
365387

388+
def start_autoconverting(self) -> None: # pylint: disable=no-self-use
389+
"""Starts autoconverting temperature measurements.
390+
The sensor will perform a measurement every ~100ms.
391+
"""
392+
# read the current value of the first config register
393+
conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0]
394+
395+
# and the complement to guarantee the oneshot bit is unset
396+
conf_reg_0 &= ~_MAX31856_CR0_1SHOT
397+
# or the autoconvert bit to ensure it is set
398+
conf_reg_0 |= _MAX31856_CR0_AUTOCONVERT
399+
400+
# write it back with the new values, prompting the sensor to perform a measurement
401+
self._write_u8(_MAX31856_CR0_REG, conf_reg_0)
402+
366403
@property
367404
def oneshot_pending(self) -> bool:
368405
"""A boolean indicating the status of the one-shot flag.
@@ -386,6 +423,19 @@ def _read_register(self, address: int, length: int) -> bytearray:
386423
device.readinto(self._BUFFER, end=length)
387424
return self._BUFFER[:length]
388425

426+
def _read_sequential_registers(self, start_addr, num_registers=3):
427+
"""
428+
Read a sequence of `num_registers` registers, starting from `start_addr`.
429+
"""
430+
assert num_registers >= 1, "Number of registers to read must be at least 1"
431+
buf = bytearray(num_registers)
432+
with self._device as device:
433+
# Send read command and start address
434+
device.write(bytearray([start_addr & 0x7F]))
435+
# Read the specified number of registers into the buffer
436+
device.readinto(buf)
437+
return buf
438+
389439
def _write_u8(self, address: int, val: int) -> None:
390440
# Write an 8-bit unsigned value to the specified 8-bit address.
391441
with self._device as device:

0 commit comments

Comments
 (0)