49
49
_MAX31856_CR0_CJ = const (0x08 )
50
50
_MAX31856_CR0_FAULT = const (0x04 )
51
51
_MAX31856_CR0_FAULTCLR = const (0x02 )
52
+ _MAX31856_CR0_50HZ = const (0x01 )
52
53
53
54
_MAX31856_CR1_REG = const (0x01 )
54
55
_MAX31856_MASK_REG = const (0x02 )
76
77
_MAX31856_FAULT_OVUV = const (0x02 )
77
78
_MAX31856_FAULT_OPEN = const (0x01 )
78
79
80
+ _AVGSEL_CONSTS = {1 : 0x00 , 2 : 0x10 , 4 : 0x20 , 8 : 0x30 , 16 : 0x40 }
81
+
79
82
80
83
class ThermocoupleType : # pylint: disable=too-few-public-methods
81
84
"""An enum-like class representing the different types of thermocouples that the MAX31856 can
@@ -113,6 +116,8 @@ class MAX31856:
113
116
:param ~microcontroller.Pin cs: The pin used for the CS signal.
114
117
:param ~adafruit_max31856.ThermocoupleType thermocouple_type: The type of thermocouple.\
115
118
Default is Type K.
119
+ :param ~int sampling: Number of samples to be averaged [1,2,4,8,16]
120
+ :param ~bool filter_50hz: Filter 50Hz mains frequency instead of 60Hz
116
121
117
122
**Quickstart: Importing and using the MAX31856**
118
123
@@ -155,13 +160,64 @@ def __init__(self, spi, cs, thermocouple_type=ThermocoupleType.K):
155
160
self ._write_u8 (_MAX31856_CR0_REG , _MAX31856_CR0_OCFAULT0 )
156
161
157
162
# set thermocouple type
163
+ self ._set_thermocouple_type (thermocouple_type )
164
+
165
+ def _set_thermocouple_type (self , thermocouple_type : ThermocoupleType ):
158
166
# get current value of CR1 Reg
159
167
conf_reg_1 = self ._read_register (_MAX31856_CR1_REG , 1 )[0 ]
160
168
conf_reg_1 &= 0xF0 # mask off bottom 4 bits
161
169
# add the new value for the TC type
162
170
conf_reg_1 |= int (thermocouple_type ) & 0x0F
163
171
self ._write_u8 (_MAX31856_CR1_REG , conf_reg_1 )
164
172
173
+ @property
174
+ def averaging (self ) -> int :
175
+ """Number of samples averaged together in each result.
176
+ Must be 1, 2, 4, 8, or 16. Default is 1 (no averaging).
177
+ """
178
+ conf_reg_1 = self ._read_register (_MAX31856_CR1_REG , 1 )[0 ]
179
+ avgsel = conf_reg_1 & ~ 0b10001111 # clear bits other than 4-6
180
+ # check which byte this corresponds to
181
+ for key , value in _AVGSEL_CONSTS .items ():
182
+ if value == avgsel :
183
+ return key
184
+ raise KeyError (f"AVGSEL bit pattern was not recognised ({ avgsel :>08b} )" )
185
+
186
+ @averaging .setter
187
+ def averaging (self , num_samples : int ):
188
+ # This option is set in bits 4-6 of register CR1.
189
+ if num_samples not in _AVGSEL_CONSTS :
190
+ raise ValueError ("Num_samples must be one of 1,2,4,8,16" )
191
+ avgsel = _AVGSEL_CONSTS [num_samples ]
192
+ # get current value of CR1 Reg
193
+ conf_reg_1 = self ._read_register (_MAX31856_CR1_REG , 1 )[0 ]
194
+ conf_reg_1 &= 0b10001111 # clear bits 4-6
195
+ # OR the AVGSEL bits (4-6)
196
+ conf_reg_1 |= avgsel
197
+ self ._write_u8 (_MAX31856_CR1_REG , conf_reg_1 )
198
+
199
+ @property
200
+ def noise_rejection (self ) -> int :
201
+ """
202
+ The frequency (Hz) to be used by the noise rejection filter.
203
+ Must be 50 or 60. Default is 60."""
204
+ # this value is stored in bit 0 of register CR0.
205
+ conf_reg_0 = self ._read_register (_MAX31856_CR0_REG , 1 )[0 ]
206
+ if conf_reg_0 & _MAX31856_CR0_50HZ :
207
+ return 50
208
+ return 60
209
+
210
+ @noise_rejection .setter
211
+ def noise_rejection (self , frequency : int ):
212
+ conf_reg_0 = self ._read_register (_MAX31856_CR0_REG , 1 )[0 ]
213
+ if frequency == 50 :
214
+ conf_reg_0 |= _MAX31856_CR0_50HZ # set the 50hz bit
215
+ elif frequency == 60 :
216
+ conf_reg_0 &= ~ _MAX31856_CR0_50HZ # clear the 50hz bit
217
+ else :
218
+ raise ValueError ("Frequency must be 50 or 60" )
219
+ self ._write_u8 (_MAX31856_CR0_REG , conf_reg_0 )
220
+
165
221
@property
166
222
def temperature (self ):
167
223
"""Measure the temperature of the sensor and wait for the result.
@@ -186,7 +242,7 @@ def unpack_temperature(self) -> float:
186
242
187
243
@property
188
244
def reference_temperature (self ):
189
- """Wait to retreive temperature of the cold junction in degrees Celsius. (read-only)"""
245
+ """Wait to retrieve temperature of the cold junction in degrees Celsius. (read-only)"""
190
246
self ._perform_one_shot_measurement ()
191
247
return self .unpack_reference_temperature ()
192
248
0 commit comments