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, especially with the Positron8 BASIC Compiler's, built in, I2C routines.

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

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

The Positron8 BASIC code for the interface is shown below:

' 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 for the Positron8 BASIC Compiler

'

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 a variable for the demo

'

Dim MCP_fTemperature As Float ' Holds the temperature


'-------------------------------------------------------------------------------

' Setup the MCP9800 device

' Input : None

' Output : None

' Notes : None

'

Proc MCP9800_Init()

BusOut cMCP9800_ADDRESS_WRITE, [cMCP9800_CONFIGURATION, cMCP9800]

BusOut cMCP9800_ADDRESS_WRITE, [cMCP9800_TEMPERATURE]

EndProc


'--------------------------------------------------------------------------------

' Interface to an MCP9800 I2C temperature sensor

' Input : None

' Output : Returnsthe temperature (in degrees Centigrade)

' Notes : None

'

Proc MCP9800_GetTemperature(), Float

Dim wRaw As Word ' Used for reading data from the device


BusIn cMCP9800_ADDRESS_READ,[wRaw.Byte0, wRaw.Byte1]

Result = wRaw.SByte0


If wRaw.15 = 1 Then

Result = Result + 0.5

EndIf

If wRaw.14 = 1 Then

Result = Result + 0.25

EndIf

If wRaw.13 = 1 Then

Result = Result + 0.125

EndIf

If wRaw.12 = 1 Then

Result = Result + 0.0625

EndIf

EndProc


'--------------------------------------------------------------------------------

' The main program loop starts here

' Read the temperature and send the ASCII value to a serial terminal

'

Main:

MCP9800_Init()


Do

MCP_fTemperature = MCP9800_GetTemperature()


HRSOutLn "Temperature = ", Dec2 MCP_fTemperature

DelayMS 500

Loop

Below is the program running within the Proteus simulator:

The above simulation files can be downloaded from here:

MCP9800_Proteus_Design.zip