;Linearisation Algorithm for Thermistor sensors.
;Language: Venom-SC
;Author: Karl Lam 15/02/2007
;Copyright Micro-Robotics Ltd 2007.

;Find the temperature of a Thermistor Sensor that is used in a simple
;potential divider circuit, with a bias resistor in the top leg and
;the sensor in the bottom leg.
;The divider is energised by the reference voltage of the ADC, which can
;simply be the supply voltage since the reading is ratiometric.
;The divided voltage is fed directly to the ADC input.
;There may be lead resistance in the thermistor leads that needs taking
;into account, but 10 Ohms of lead res will only make ~20mK temp inacuracy at 25C.


;Linearisation is done using Log formula, with constants used here that meet
; data for a Betatherm 10K Ohm nom. thermistor.
;The bias resistor is 10K 0.1%, and the ADC is 12-bit.

;Contact M-R for constants for other devices.



#DEFINE BIAS_RES 10000.0
#DEFINE LA_VALUE (-3.7183);Thermistor constant
#DEFINE B_VALUE 3854.67 ;Thermistor constant
#DEFINE ZERO_C 273.15 ;0C in Kelvin
#DEFINE ADC_MAX 4096 ;ADC max reading - 12-bit here.

TO temp(adc)
  LOCAL res
  IF adc = 0 adc := 1 ;prevent math error.
  res := BIAS_RES * (ADC_MAX / (ADC_MAX - adc) - 1.0) ;Find the resistance of the sensor.
  RETURN B_VALUE / (LOG (res) - LA_VALUE) - ZERO_C ;Find Degrees C from resistance.
END