• PicŪ Basic


  • MCP9800 Temperature Sensor Interface

    The MCP9800 digital temperature sensor converts temperatures between -55°C and +125°C. It provides an accuracy of ą1°C (maximum) from -10°C to +85°C. Interfacing to it is very straightforward via an I2C bus.

    The MCP9800 datasheet can be downloaded from here: MCP9800.pdf

    A suitable circuit for the MCP9800 temperature sensor is shown below:



























    Click here for a larger image


    The Proton code for the interface is shown below:
    Code:
    '
    ' Interface to an MCP9800 temperature sensor
    ' Transmits the temperature (in degrees Centigrade) to the serial terminal
    '
    ' For the Amicus18 board, which uses a PIC18F25K20 device operating at 64MHz
    ' Written by Les Johnson 06-01-2016
    '
        Include "Amicus18.inc"                                      ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. An Amicus18 board
        Declare Float_Display_Type = Fast                           ' Set for faster and more accurate floating point displaying
    
        Declare SDA_Pin PORTB.1                                     ' Setup the I2C SDA pin
        Declare SCL_Pin PORTB.0                                     ' Setup the I2C SCL pin
    '
    ' Create some constants for the MCP9800 device
    '
    $define cMCP9800_ADDRESS_WRITE       $90
    $define cMCP9800_ADDRESS_READ        $91
    $define cMCP9800_ALERT_ACTIVE_LOW    $00
    $define cMCP9800_ALERT_ACTIVE_HIGH   $04
    $define cMCP9800_COMP_MODE           $00
    $define cMCP9800_CONFIGURATION       $01
    $define cMCP9800_HYSTERESIS          $02
    $define cMCP9800_INT_MODE            $02
    $define cMCP9800_LIMITSET            $03
    $define cMCP9800_ONESHOT_OFF         $00
    $define cMCP9800_ONESHOT_ON          $80
    $define cMCP9800_SHUTDOWN_OFF        $00
    $define cMCP9800_SHUTDOWN_ON         $01
    $define cMCP9800_TEMPERATURE         $00
    $define cMCP9800_TSET_DEFAULT        $50
    $define cMCP9800_CONFIG_BITS         $64
    
    $define cMCP9800_RES_9bit            $00
    $define cMCP9800_RES_10bit           $20
    $define cMCP9800_RES_11bit           $40
    $define cMCP9800_RES_12bit           $60
    $define cMCP9800_FAULT_QUEUE_0       $00
    $define cMCP9800_FAULT_QUEUE_2       $08
    $define cMCP9800_FAULT_QUEUE_4       $10
    $define cMCP9800_FAULT_QUEUE_6       $18
    
    Symbol cMCP9800 = cMCP9800_ONESHOT_OFF | cMCP9800_RES_12bit | cMCP9800_FAULT_QUEUE_0 | cMCP9800_ALERT_ACTIVE_LOW | cMCP9800_COMP_MODE | cMCP9800_SHUTDOWN_OFF
    '
    ' Create some variables
    '
        Dim MCP_fTemperature As Float                               ' Holds the temperature
        Dim MCP_wRawData As Word                                    ' Used for reading data from the device
    
    '-------------------------------------------------------------------------------
        GoTo Main                                                   ' Jump over the subroutines
    '-------------------------------------------------------------------------------
    ' Setup the MCP9800 device
    ' Input     : None
    ' Output    : None
    ' Notes     : None
    '
    $define MCP9800_Init() GoSub _MCP9800_Init
    
    _MCP9800_Init:
        BusOut cMCP9800_ADDRESS_WRITE, [cMCP9800_CONFIGURATION, cMCP9800]
        BusOut cMCP9800_ADDRESS_WRITE, [cMCP9800_TEMPERATURE]
        Return
    
    '--------------------------------------------------------------------------------
    ' Interface to an MCP9800 I2C temperature sensor
    ' Input     : None
    ' Output    : pResult holds the temperature (in degrees Centigrade)
    ' Notes     : None
    '
    $define MCP9800_GetTemperature(pResult) '
        GoSub _MCP9800_ReadTemp             '
        pResult = MCP_fTemperature
    
    _MCP9800_ReadTemp:
        BusIn cMCP9800_ADDRESS_READ,[MCP_wRawData.Byte0, MCP_wRawData.Byte1]
        MCP_fTemperature = MCP_wRawData.SByte0
    
        If MCP_wRawData.15 = 1 Then
            MCP_fTemperature = MCP_fTemperature + 0.5
        EndIf
        If MCP_wRawData.14 = 1 Then
            MCP_fTemperature = MCP_fTemperature + 0.25
        EndIf
        If MCP_wRawData.13 = 1 Then
            MCP_fTemperature = MCP_fTemperature + 0.125
        EndIf
        If MCP_wRawData.12 = 1 Then
            MCP_fTemperature = MCP_fTemperature + 0.0625
        EndIf
        Return
    
    '--------------------------------------------------------------------------------
    ' The main program loop starts here
    '
    Main:
        MCP9800_Init()
    
        While
            MCP9800_GetTemperature(MCP_fTemperature)
    
            HRSOut "Temperature = ", Dec2 MCP_fTemperature, 13
            DelayMS 500
        Wend
    Below is the program running within the Proteus simulator:


    The above simulation files can be downloaded from here:
    MCP9800_Proteus_Design.zip


  • Recent Activity

    towlerg-21522

    Mysterious PORTB problem

    Thread Starter: xldaedalus

    I'm using Proton+ to develop firmware for a product with switches. The MCU is an 18F26K22. Most of the switches reside on PORTB. I am NOT using a...

    towlerg Yesterday, 15:45 Go to last post
    towlerg-21522

    PIC18F1330 PLL problem

    Thread Starter: rcurl

    I'm just starting out on a new project that uses a PIC18F1330. I haven't used this specific chip before, so I began by trying to blink an LED just...

    towlerg Yesterday, 19:19 Go to last post
    kuhrig-602

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    kuhrig Today, 10:33 Go to last post
    palamont-28794

    MID$ Problem with a PIC18F25K22

    Thread Starter: palamont

    Hi everybody, I encounter a strange problem : I have a string of 230 characters. This string is divided into 10 segments of 23 characters, with a...

    palamont Today, 10:08 Go to last post