{{ Cold Junction Compensation and linearization, for type K thermocouple (c) 2011 EME Systems LLC, see end of code for terms of use. Given temperature of the reference junction, and microvolt reading from measurement thermocouple, returns readings in units of 0.1 degreee Celsius from -200 to +1370 Usage: method degC_to_uV(refTemp): -- reference temperature determines a corresponding microvolt offset from ice referenced type K thermocouple refuV := CJCK.degC_to_uV(refTemp) refTemp is in units of 0.1 degrees Celsius, i.e., 123 for 12.3 degrees Celsius refuV is in units of whole microvolts method compensate(refuV, hotuV): -- microvolts from measurement thermocouple and the above reference microvolts are entered to find the measurement temperature degC := CJCK.compensate(refuV, measuV) both voltages are in units of whole microvolts the result degC is in units of 0.1 degrees Celsius, i.e., 187 for 18.7 degrees Celsus. method temperature(refTemp, hotuV) -- Combines the above two into one call. degC := CJCK.temperature(refTemp, hotuV) --------------------------------------------- Versions: Version 1.3 TTA 11.15.11 -- changed over to a binary search for the lookdown, tested and debugged -- expanded the type k table to -200 degC Version 1.2 TTA 10/05/11 -- eliminated an uneeded methods now that the ADS1115 object returns microVolts directly -- renamed PUB coldTemp to PUB degC_to_uV -- extended T table to to -60 degC to +500 degC -- also added version for type K thermocouples, -100 to +1380 degC -- made compile time CONstants out of table size ENTRIES, number of negatives NEGS, and step size DELT -- simplified and tested the math, especially at negative temperature values. -- returns +/- 99999 for inputs beyond table (e.g., thermocouple not connected, open) Version 1.1 Tracy Allen, EME Systems 09/17/11 -- extended type T table to +500 degC, a little beyond its normal use, but just to be sure of a reading without overflow Version 1.0 Mike McDonald, EME Systems, 7/2010-8/2011 }} CON ' constants for the type K DATa table ENTRIES = 158 ' number of entries in the data table NEGS = 20 ' number of sub-zero values in the table DELTA = 100 ' temperature increment in table, in units of 0.1 degree, e.g. 100 for 10 degree steps LAST = ENTRIES - 1 ' entries are numbered from 0 to LAST NEGS1 = NEGS + 1 ' non-negative, including 0 degC { _clkmode = xtal1 + pll4x ' _xinfreq = 5_000_000 OBJ debug: "simpleDebug" PUB tester | refuV, hotuV debug.start(9600) waitcnt(clkfreq/2+cnt) debug.str(string(13, "here: ")) refuV := 500 hotuV := 4000 debug.dec(compensate(refuV,hotuV)) } PUB temperature(refTemp, hotuV) return compensate(degC_to_uV(refTemp), hotuV) PUB degC_to_uV(refTemp) | tempVal, diff {{ Calculates the uV output of an ideal type K thermocouple at the reference temperature via interpolation, The reference (cold junction) temperature should be supplied in units of 0.1 degC. The result is in units of microvolts }} tempVal := refTemp/DELTA ' lookup table has entries at DELT degC intervals, tupically 10 degC diff := K_uVolts[tempVal + NEGS1] - K_uVolts[tempVal + NEGS] ' where are we inside the interval? result := (K_uVolts[tempVal + NEGS] + ((diff*(refTemp//DELTA))/DELTA)) ' interpolate to microvolts PUB compensate(refuV, hotuV) | totaluV, count, difference, bips, spot, del {{enter with both the reference and working junction in microvolts return temperature in units of 0.1 degC}} totalUV := refuV + hotuV if totaluV < K_uVolts[0] 'out of range of the table return -9999 elseif totaluV > K_uVolts[LAST] return 9999 else bips := >| ENTRIES spot := (|< bips) / 2 del := spot/2 repeat ' this performs a lookdown and interpolation into the table if (totalUV > K_uVolts[spot]) spot += del <# ENTRIES elseif (totalUV < K_uVolts[spot]) spot -= del #> 0 else result := (spot - NEGS) * DELTA return del := del / 2 until del==0 if (totalUV > K_uVolts[spot]) spot++ difference := K_uVolts[spot] - K_uVolts[spot-1] ' have bracketing values for interpolation result:=((totaluV-K_uVolts[spot-1]) * DELTA) / difference + (spot - NEGS1) * DELTA DAT {Type K thermocouple table, 148 entries, from -200 degrees Celsius to +1370 degrees Celsius inclusive at 10 degree intervals an in units of microvolts wrt ice reference.} K_uVolts long -5891, -5730, -5550, -5354, -5141, -4913, -4669, -4411, -4138, -3852 long -3554, -3243, -2920, -2587, -2243, -1889, -1527, -1156, -778, -392 long 0, 397, 798, 1203, 1612, 2023, 2436, 2851, 3267, 3682 long 4096, 4509, 4920, 5328, 5735, 6138, 6540, 6941, 7340, 7739 long 8138, 8539, 8940, 9343, 9747, 10153, 10561, 10971, 11382, 11795 long 12209, 12624, 13040, 13457, 13874, 14293, 14713, 15133, 15554, 15975 long 16397, 16820, 17243, 17667, 18091, 18516, 18941, 19366, 19792, 20218 long 20644, 21071, 21497, 21924, 22350, 22776, 23203, 23629, 24055, 24480 long 24905, 25330, 25755, 26179, 26602, 27025, 27447, 27869, 28289, 28710 long 29129, 29548, 29965, 30382, 30798, 31213, 31628, 32041, 32453, 32865 long 33275, 33685, 34093, 34501, 34908, 35313, 35718, 36121, 36524, 36925 long 37326, 37725, 38124, 38522, 38918, 39314, 39708, 40101, 40494, 40885 long 41276, 41665, 42053, 42440, 42826, 43211, 43595, 43978, 44359, 44740 long 45119, 45497, 45873, 46249, 46623, 46995, 47367, 47737, 48105, 48473 long 48838, 49202, 49565, 49926, 50286, 50644, 51000, 51355, 51708, 52060 long 52410, 52759, 53106, 53451, 53795, 54138, 54479, 54819 CON {{ ┌──────────────────────────────────────────────────────────────────────────────────────┐ │ TERMS OF USE: MIT License │ ├──────────────────────────────────────────────────────────────────────────────────────┤ │Permission is hereby granted, free of charge, to any person obtaining a copy of this │ │software and associated documentation files (the "Software"), to deal in the Software │ │without restriction, including without limitation the rights to use, copy, modify, │ │merge, publish, distribute, sublicense, and/or sell copies of the Software, and to │ │permit persons to whom the Software is furnished to do so, subject to the following │ │conditions: │ │ │ │The above copyright notice and this permission notice shall be included in all copies │ │or substantial portions of the Software. │ │ │ │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, │ │INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A │ │PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT │ │HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION │ │OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ │SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ └──────────────────────────────────────────────────────────────────────────────────────┘ }}