Code: Select all
sub function Sample_MCP9701(dim PORT as byte) as byte
' Samples MCP9701 128 times and returns temp in degrees celcius (0 - 125)
dim MCP9701_SampleAvg as longint
dim MCP9701_Loop as byte
dim MCP9701_ADC as word
MCP9701_SampleAvg = 0
for MCP9701_Loop = 1 to 128
MCP9701_ADC = ADC_Read(PORT)
if (MCP9701_ADC < 92) then
MCP9701_ADC = 0
else
MCP9701_ADC = MCP9701_ADC - 92 ' (0 C)
end if
MCP9701_ADC = MCP9701_ADC >> 2 ' not needed for 8 bit ADC
MCP9701_SampleAvg = MCP9701_SampleAvg + MCP9701_ADC
next MCP9701_Loop
MCP9701_SampleAvg = MCP9701_SampleAvg >> 7 ' /128
result = MCP9701_SampleAvg
end sub
The 9701 is a cheap, tiny (2mm!) linear active thermister & this mB function makes it fairly accurate by taking 128 samples and returning an average.