;======================================================= #If 1 ; Use the Class-based version of this code. ; Change 1 to 0 for other version. ;======================================================= ; This is a user-defined Class to control thermistors. ; Author: Karl Lam 28/11/2012 ; Copyright Micro-Robotics Ltd 2012. ; A class to read a thermistor connected to a ; voltage divider and ADC input. ; The thermistor is the bottom leg of the divider. Class ThermistorInput #Define ZERO_C 273.15 ;0C in Kelvin Bias Float LogA Float Beta Float ADC_FS Int AnInput Any ; Read and return the thermistor temperature. To Temperature Local resistance, adc_reading adc_reading := AnInput.Value If adc_reading = 0 adc_reading := 1 ;prevent math error. resistance := Bias * (ADC_FS / (ADC_FS - adc_reading) - 1.0) ;Find the resistance of the sensor. Return Beta / (Log (resistance) - LogA) - ZERO_C ;Find Degrees C from resistance. End ; Parameters: ; AnInput: an analogue input object that reads the thermistor. ; R25: the 25'C resistance of the thermistor. ; Beta: Thermistor characteristic value. ; [Bias]: The bias resistor used. Defaults to R25. ; [ADCbits]: The no. of bits of resolution of the ADC - defaults to 12-bit. To Initialise(AnInput, R25, Beta, [Bias, ADCbits]) If Bias = 0 Bias := R25 If ADCbits = 0 ADCbits := 12 This.Bias := Bias As Float This.LogA := Log(R25) - (Beta/(ZERO_C + 25)) This.Beta := Beta This.ADC_FS := 2^ADCbits This.AnInput := AnInput End End ; Test Code: To Init Make a Analogue($30) Make t ThermistorInput(a,10000,3854.67) End To main Every 1000 Print t.Temperature, CR End ; Test code end. ;======================================================= #Else ; Use the procedure-based version of this code. ;======================================================= ;Linearisation Algorithm for Thermistor sensors. ;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 temperature(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 ; Test Code: To Init Make a Analogue($30) End To main Every 1000 Print temperature(a.Value), CR End ; Test code end. ;======================================================= #EndIf ;=======================================================