; Code to read a thermocouple input. ;*** Test code *** To init Make tc Thermocouple(TypeK_V2TCoefs_0C_500C) End To main Every 1000 Print tc.temperature, CR End ;*** Test code END *** ; A Class to read a thermocouple. ; Note: this Class assumes 5624 Hardware or similar. ; The 5624 hardware limits this to one channel. ; Please contact Micro-Robotics for multiple channel designs. Class Thermocouple TCanalogue Any AutoDestruct; Analogue input object to read cold junction. Thermistor Any AutoDestruct; Thermistor input controller. V2TCoefs_0_500 Array ; Linearisation polynomial coeffs. CJinput Any AutoDestruct ; Cold Junction analogue input object. ;Convert the cold junction measured temp into a voltage so ; we can add it to TC voltage. To ColdJuncTmV(T) ;Accurate to ~0.1C in the range 10C - 40C Return 0.0405 * T - 0.0125 End ; Convert a mV figure into a temperature using a polynomial. To Tpolynomial(mV) Local sum := 0.0 Repeat V2TCoefs_0_500.Count sum := sum + V2TCoefs_0_500.(Index0) * (mV^Index0) Return sum End ; Read and return the thermocouple temperature. To Temperature Local raw := TCanalogue.Value Local mv := raw * 2048.0/(2^(17+3)) Local Tcj := Thermistor.Temperature Local mVcj := ColdJuncTmV(Tcj) Return Tpolynomial(mV+mVcj) End ; Parameters: ; V2TCoefs_0_500: polynomial coeffs for the type of thermocouple materials. ; - only valid in the range 0C-500C currently. To Initialise(V2TCoefs_0_500) TCanalogue := New MCP3421_18bit(208); MCP3421 normal address. CJinput := New Analogue(0,1,64,0) ; AD7998, I2CBus #1, normal address, input #0. Thermistor := New ThermistorInput(CJinput, 10000, 3854.67) This.V2TCoefs_0_500 := V2TCoefs_0_500 End End ; A class to read a thermistor connecter 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 ;Class to operate the MCP3421 Sigma-Delta ADC ; at 18-bit resolution. Class MCP3421_18bit Global net Address Int 8 ;Read and return the raw ADC value from the MCP3421. To Value Local result net.Lock result := net.Get(Address,3) net.UnLock ;Sign extension: If result And $800000 result := result Or $FFFC0000 Return result End ;Address: the I2C Address - usually 208. ;[Config] - optional configuration byte. To Initialise(Address) Local config := %00011111 ;Continuous, 18-bit, x8 gain. This.Address := Address net.Lock net.Put(Address, config) net.UnLock End End ;Voltage to Temperatre Polynomial coefficients for Type K thermocouples ; for range 0C - 500C. Array TypeK_V2TCoefs_0C_500C (Float) 0.000000E+00, 2.508355E+01, 7.860106E-02, -2.503131E-01, 8.315270E-02, -1.228034E-02, 9.804036E-04, -4.413030E-05, 1.057734E-06, -1.052755E-08, End