Proton BASIC Compiler - MAX

• ### Pic® Basic • # MAX31865 temperature sensor

The MAX31865 is a temperature sensor that outputs a resistance reading from a sensor.
In order to find the temperature represented by the resistance reading a user could use a table based on the datasheet example and interpolate between the table values or the user could use a simple formula to derive the temperature from the resistance. The latter is what is done in this test program.

The datasheet provides a formula that can be used to derive the resistance from the temperature but not the reverse. This example below shows how temperature can be obtained by manipulation of the Callendar-Van Dusen equation. Hopefully the comments explain things fully.
John Drew

Code:
```(* test program for the MAX31865 resistance to temperature conversion                         *)
(* this program uses the Callendar-Van Dusen equation: R(T) = R0(1 + aT + bT2 + c(T - 100)T3) *)
(* ignore last term as only relevant above 800degC                                            *)
(* re-arrange equation to bT2 + aT + (1-RT/R0) = 0 and solve the quadratic for T              *)
(* some mathematical gymnastics are needed. The minus root quadratic solution can be ignored  *)

Device 18F452

Declare Xtal 4                   'the following are the defaults, I put them in for understanding
Declare LCD_Type 0            'text type
Declare LCD_DTPin PORTD.4    'assigns data lines to D4..7
Declare LCD_ENPin PORTE.1    'enable pin
Declare LCD_RSPin PORTE.0    'RS line pin
Declare LCD_Interface 4          '4 or 8 line interface
Declare LCD_Lines 2            'lines in the display

Dim R0 As Float
Dim RT As Float
Dim Temperature As Float
Dim RatioR As Float
Dim InsideRoot As Float
Dim SquareRoot As Float
Dim Resistance As Float
Dim ResistanceBase

Symbol a = 0.00390830                           'a and b are the equation constants
Symbol a1 = 0.0000152748                        'a1 is "a" squared to save calculation
Symbol b = -0.0000005775

Proc Calibrate(), Float                         'put sensor in pure, crushed, melting ice
' read the resistance at 0degC
' save it in eedata or cdata
' assign it to ResistanceBase
EndProc

Proc GetTemp(RT As Float, R0 As Float), Float  're-arrange to put temperature on the left side
RatioR = 1 - (RT/R0)                       'and solve the quadratic
InsideRoot = a1 - (4 * b * RatioR)         'break up the calculation into smaller bits
SquareRoot = Sqr (InsideRoot)              'experience has shown this tactic retains accuracy
Result = (SquareRoot - a)/(2*b)            'result gives better than 0.1 resolution
EndProc

Main:
'read ResistanceBase from eedata and Resistance from the MAX31865
Resistance     = 134.71       'value comes from the MAX. TEST value of 134.71 entered
ResistanceBase = 100          'value base resistance at 0degC from calibration
Print At 1,1,Dec GetTemp(Resistance, ResistanceBase), " Deg C "
End                               'from the table a resistance of 134.71 represents 90 degC
'the calculated temperature is 90.008 degC, a close match```