Page 1 of 1

Code to read a MCP9701 temperature sensor

Posted: 10 Feb 2006 03:19
by Niki
Here's a routine to read a MCP9701 sensor on any ADC port:

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.

Posted: 10 Feb 2006 04:10
by xor
Keeping up the good work I see Niki.....nice job!

Posted: 10 Feb 2006 12:13
by Niki
Hi xor

That's from my first attempt at an SMT board. If you prototype your own boards, lookup acetone toner transfer as it works faster than UV light boxes & is vastly more reliable than press'n'peel or other iron-on transfers :)