Code to read a MCP9701 temperature sensor

General discussion on mikroBasic.
Post Reply
Author
Message
Niki
Posts: 181
Joined: 09 Feb 2005 23:03
Location: UK

Code to read a MCP9701 temperature sensor

#1 Post by Niki » 10 Feb 2006 03:19

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.

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#2 Post by xor » 10 Feb 2006 04:10

Keeping up the good work I see Niki.....nice job!
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Niki
Posts: 181
Joined: 09 Feb 2005 23:03
Location: UK

#3 Post by Niki » 10 Feb 2006 12:13

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 :)

Post Reply

Return to “mikroBasic General”