spi_temperature: Limit maximum temperature in MAX31865.calc_adc() to melting point of platinum (#6320)

Limit the maximum temperature in MAX31865.calc_adc() to the melting
point of platinum. Above this temperature the Callendar-Van Dusem
formula does not make sense. The default value for max_temp is
99999999.9 and at this temperature the result of this formula is
negative. This sets max_sample_value to 0 which causes the mcu
to shutdown.

Set max adc value to (1<<15)-1 .  This is needed because the max value of the adc register
of the MAX31865 is 0b1111 1111 1111 1110 which represents
32767 and not 32768.

Signed-off-by: David van 't Wout <github@yoctobyte.nl>
This commit is contained in:
DavidvtWout 2023-09-11 00:51:14 +02:00 committed by GitHub
parent 2acfa28294
commit 8ef0f7d7e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 1 deletions

View File

@ -320,9 +320,10 @@ class MAX31865(SensorBase):
def calc_adc(self, temp): def calc_adc(self, temp):
# Calculate relative resistance via Callendar-Van Dusen formula: # Calculate relative resistance via Callendar-Van Dusen formula:
# resistance = rtd_nominal_r * (1 + CVD_A * temp + CVD_B * temp**2) # resistance = rtd_nominal_r * (1 + CVD_A * temp + CVD_B * temp**2)
temp = min(temp, 1768.3) # Melting point of platinum
R_div_nominal = 1. + CVD_A * temp + CVD_B * temp * temp R_div_nominal = 1. + CVD_A * temp + CVD_B * temp * temp
adc = int(R_div_nominal / self.adc_to_resist_div_nominal + 0.5) adc = int(R_div_nominal / self.adc_to_resist_div_nominal + 0.5)
adc = max(0, min(MAX31865_ADC_MAX, adc)) adc = max(0, min(MAX31865_ADC_MAX - 1, adc))
adc = adc << 1 # Add fault bit adc = adc << 1 # Add fault bit
return adc return adc
def build_spi_init(self, config): def build_spi_init(self, config):