From 5bec618d09db040f9d7c3a881c4fef7853b9f832 Mon Sep 17 00:00:00 2001 From: Diego Solano Date: Sat, 9 Dec 2023 14:58:08 -0800 Subject: [PATCH 1/5] Added high resolution temp reading --- adafruit_max31856.py | 52 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 867b20e..342e1fa 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -164,8 +164,9 @@ def __init__( spi: SPI, cs: DigitalInOut, # pylint: disable=invalid-name thermocouple_type: int = ThermocoupleType.K, + baudrate: int = 500000, ) -> None: - self._device = SPIDevice(spi, cs, baudrate=500000, polarity=0, phase=1) + self._device = SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=1) # assert on any fault self._write_u8(_MAX31856_MASK_REG, 0x0) @@ -253,6 +254,27 @@ def unpack_temperature(self) -> float: return temp_float + def read_high_res_temp_degC(self) -> float: + # Per datasheet, temperature resolution in °C per LSB + resolution = 0.0078125 + + # Read the temperature registers + raw_bytes = self._read_sequential_registers(_MAX31856_LTCBH_REG, 3) + # Extract individual bytes from the byte array + high_byte = raw_bytes[0] # First byte + mid_byte = raw_bytes[1] # Second byte + low_byte = raw_bytes[2] # Third byte + + # Combine the bytes into a single 19-bit value + combined = (high_byte << 11) | (mid_byte << 3) | (low_byte >> 5) + + # Adjust for two's complement (sign extension for negative values) + if combined & 0x40000: # Check if 19th bit is set (negative temperature) + combined = combined - 0x80000 + + # Convert to temperature using the resolution + return combined * resolution + @property def reference_temperature(self) -> float: """Wait to retrieve temperature of the cold junction in degrees Celsius. (read-only)""" @@ -363,6 +385,21 @@ def initiate_one_shot_measurement(self) -> None: # write it back with the new values, prompting the sensor to perform a measurement self._write_u8(_MAX31856_CR0_REG, conf_reg_0) + def start_autoconverting(self) -> None: # pylint: disable=no-self-use + """Starts autoconverting temperature measurements. + The sensor will perform a measurement every ~100ms. + """ + # read the current value of the first config register + conf_reg_0 = self._read_register(_MAX31856_CR0_REG, 1)[0] + + # and the complement to guarantee the oneshot bit is unset + conf_reg_0 &= ~_MAX31856_CR0_1SHOT + # or the autoconvert bit to ensure it is set + conf_reg_0 |= _MAX31856_CR0_AUTOCONVERT + + # write it back with the new values, prompting the sensor to perform a measurement + self._write_u8(_MAX31856_CR0_REG, conf_reg_0) + @property def oneshot_pending(self) -> bool: """A boolean indicating the status of the one-shot flag. @@ -386,6 +423,19 @@ def _read_register(self, address: int, length: int) -> bytearray: device.readinto(self._BUFFER, end=length) return self._BUFFER[:length] + def _read_sequential_registers(self, start_addr, num_registers=3): + """ + Read a sequence of `num_registers` registers, starting from `start_addr`. + """ + assert num_registers >= 1, "Number of registers to read must be at least 1" + buf = bytearray(num_registers) + with self._device as device: + # Send read command and start address + device.write(bytearray([start_addr & 0x7F])) + # Read the specified number of registers into the buffer + device.readinto(buf) + return buf + def _write_u8(self, address: int, val: int) -> None: # Write an 8-bit unsigned value to the specified 8-bit address. with self._device as device: From 0969002963cde83c7b48db009786d4a904342af1 Mon Sep 17 00:00:00 2001 From: Diego Solano Date: Sat, 9 Dec 2023 15:13:15 -0800 Subject: [PATCH 2/5] updated constructor to properly set baudrate --- adafruit_max31856.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 342e1fa..6507d41 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -45,7 +45,7 @@ except ImportError: from ustruct import unpack -__version__ = "0.0.0+auto.0" +__version__ = "0.11.7" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX31856.git" # Register constants @@ -124,8 +124,7 @@ class MAX31856: :param ~microcontroller.Pin cs: The pin used for the CS signal. :param ~adafruit_max31856.ThermocoupleType thermocouple_type: The type of thermocouple.\ Default is Type K. - :param ~int sampling: Number of samples to be averaged [1,2,4,8,16] - :param ~bool filter_50hz: Filter 50Hz mains frequency instead of 60Hz + :param ~int baudrate: The SPI baudrate. Default is 500000. **Quickstart: Importing and using the MAX31856** @@ -255,6 +254,11 @@ def unpack_temperature(self) -> float: return temp_float def read_high_res_temp_degC(self) -> float: + """Reads 19-bit temperature data from the sensor and returns it in degrees Celsius. + + Returns: + float: temperature in degrees Celsius + """ # Per datasheet, temperature resolution in °C per LSB resolution = 0.0078125 @@ -423,7 +427,7 @@ def _read_register(self, address: int, length: int) -> bytearray: device.readinto(self._BUFFER, end=length) return self._BUFFER[:length] - def _read_sequential_registers(self, start_addr, num_registers=3): + def _read_sequential_registers(self, start_addr, num_registers=3) -> bytearray: """ Read a sequence of `num_registers` registers, starting from `start_addr`. """ From 9d010b9275e88129bc9e4494705f55c144ba4999 Mon Sep 17 00:00:00 2001 From: Diego Solano Date: Sat, 9 Dec 2023 15:20:51 -0800 Subject: [PATCH 3/5] rename function to pass linting --- adafruit_max31856.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 6507d41..1498f01 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -253,9 +253,11 @@ def unpack_temperature(self) -> float: return temp_float - def read_high_res_temp_degC(self) -> float: + def read_high_res_temp(self) -> float: """Reads 19-bit temperature data from the sensor and returns it in degrees Celsius. + Reading must have already been initiated via `initiate_one_shot_measurement` or `start_autoconverting`. + Returns: float: temperature in degrees Celsius """ From fc46752d67924e25b05b805f0e40db0e887aa03e Mon Sep 17 00:00:00 2001 From: Diego Solano Date: Sat, 9 Dec 2023 15:23:05 -0800 Subject: [PATCH 4/5] fixing line too long --- adafruit_max31856.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 1498f01..34236b2 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -256,7 +256,8 @@ def unpack_temperature(self) -> float: def read_high_res_temp(self) -> float: """Reads 19-bit temperature data from the sensor and returns it in degrees Celsius. - Reading must have already been initiated via `initiate_one_shot_measurement` or `start_autoconverting`. + Reading must have already been initiated via: + `initiate_one_shot_measurement` or `start_autoconverting`. Returns: float: temperature in degrees Celsius From 764aec4f2bbe4c776e0ec69b116999b98984dfd0 Mon Sep 17 00:00:00 2001 From: Diego Solano Date: Mon, 4 Mar 2024 13:14:25 -0800 Subject: [PATCH 5/5] Restored adafruit_max31856.py version --- adafruit_max31856.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_max31856.py b/adafruit_max31856.py index 34236b2..4427d4b 100644 --- a/adafruit_max31856.py +++ b/adafruit_max31856.py @@ -45,7 +45,7 @@ except ImportError: from ustruct import unpack -__version__ = "0.11.7" +__version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX31856.git" # Register constants