From 6f92021f09b57fe788113dbf52540be2b1c95c32 Mon Sep 17 00:00:00 2001 From: Andrew de Quincey Date: Sat, 15 May 2021 18:27:15 +0100 Subject: [PATCH] add line frequency setting and expose avgsel value --- config.py | 11 +++++++++++ lib/max31856.py | 11 +++++++---- lib/oven.py | 6 ++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/config.py b/config.py index 059d6dd..7e76785 100644 --- a/config.py +++ b/config.py @@ -129,3 +129,14 @@ honour_theromocouple_short_errors = True # If you suffer from the high temperature kiln issue and have set honour_theromocouple_short_errors to False, # you will likely need to increase this (eg I use 40) temperature_average_samples = 5 + +# Thermocouple AC frequency filtering - set to True if in a 50Hz locale, else leave at False for 60Hz locale +ac_freq_50hz = False + +# MAX31856 avgsel -- number of samples averaged on-chip prior to returning result. Values: +# 0: 1 sample +# 1: 2 samples +# 2: 4 samples +# 3: 8 samples +# 4: 16 samples +max31856_avgsel = 0 diff --git a/lib/max31856.py b/lib/max31856.py index f0fd4ef..d0bc2be 100644 --- a/lib/max31856.py +++ b/lib/max31856.py @@ -89,7 +89,7 @@ class MAX31856(object): MAX31856_S_TYPE = 0x6 # Read S Type Thermocouple MAX31856_T_TYPE = 0x7 # Read T Type Thermocouple - def __init__(self, tc_type=MAX31856_S_TYPE, units="c", avgsel=0x0, software_spi=None, hardware_spi=None, gpio=None): + def __init__(self, tc_type=MAX31856_S_TYPE, units="c", avgsel=0x0, ac_freq_50hz=False, software_spi=None, hardware_spi=None, gpio=None): """ Initialize MAX31856 device with software SPI on the specified CLK, CS, and DO pins. Alternatively can specify hardware SPI by sending an @@ -100,6 +100,7 @@ class MAX31856(object): MAX31856.MAX31856_X_TYPE. avgsel (1-byte Hex): Type of Averaging. Choose from values in CR0 table of datasheet. Default is single sample. + ac_freq_50hz: Set to True if your AC frequency is 50Hz, Set to False for 60Hz, software_spi (dict): Contains the pin assignments for software SPI, as defined below: clk (integer): Pin number for software SPI clk cs (integer): Pin number for software SPI cs @@ -112,6 +113,8 @@ class MAX31856(object): self.tc_type = tc_type self.avgsel = avgsel self.units = units + self.noConnection = self.shortToGround = self.shortToVCC = self.unknownError = False + # Handle hardware SPI if hardware_spi is not None: self._logger.debug('Using hardware SPI') @@ -132,10 +135,12 @@ class MAX31856(object): self._spi.set_mode(1) self._spi.set_bit_order(SPI.MSBFIRST) + self.cr0 = self.MAX31856_CR0_READ_CONT | (1 if ac_freq_50hz else 0) self.cr1 = ((self.avgsel << 4) + self.tc_type) # Setup for reading continuously with T-Type thermocouple - self._write_register(self.MAX31856_REG_WRITE_CR0, self.MAX31856_CR0_READ_CONT) + self._write_register(self.MAX31856_REG_WRITE_CR0, 0) + self._write_register(self.MAX31856_REG_WRITE_CR0, self.cr0) self._write_register(self.MAX31856_REG_WRITE_CR1, self.cr1) @staticmethod @@ -300,5 +305,3 @@ class MAX31856(object): def get(self): celcius = self.read_temp_c() return getattr(self, "to_" + self.units)(celcius) - - diff --git a/lib/oven.py b/lib/oven.py index 4d790b7..9011330 100644 --- a/lib/oven.py +++ b/lib/oven.py @@ -118,8 +118,10 @@ class TempSensorReal(TempSensor): 'do': config.gpio_sensor_data, 'di': config.gpio_sensor_di } self.thermocouple = MAX31856(tc_type=config.thermocouple_type, - software_spi = sofware_spi, - units = config.temp_scale + software_spi = software_spi, + units = config.temp_scale, + avgsel = config.max31856_avgsel, + ac_freq_50hz = config.ac_freq_50hz, ) def run(self):