Reading temperature from MCP9801 via I2C with 8051

Thread Starter

Cable Guy

Joined Mar 23, 2012
3
I am trying to communicate to a MCP9801 temperature sensor via I2C using a 8051 microcontroller.

I wrote a routine to set the resolution in the config register to 0.0625 C and then read a byte from the temperature register in an array:

Rich (BB code):
#include <I2C.h>

void main()
{
        unsigned int i;
	unsigned char a[8];

	while(1)
	{

                I2CInit();
		I2CStart();

		I2CSend(0x90); //device adress: 1001 00 0 - write operation
		I2CAck();

		I2CSend(0x01); //pointer for selecting config register
		I2CAck();

		I2CSend(0x60); //value to write in config register
		I2CAck();
	
		I2CStop();

		I2CStart();
		I2CSend(0x90); // dummy write command - for selecting the temperature register
		I2CAck();
		
		I2CSend(0x02); //pointer for selecting the temperature register
		I2CAck();

		I2CSend(0x91); //read command
		I2CAck();

                for(i=0;i<8;i++) //reading 8 bits from the hysteresis register into unsigned char array
		{
			a=I2CRead();
			if(i==7)
				I2CNak();
			else I2CAck();
		}// for

		I2CStop();

	}//while(1)
}//main



When I look in the I2C debugger terminal in Proteus VSM simulator, this is what I see:

http://imageshack.us/photo/my-images/814/simulation.jpg



I don't see the value in the temperature register (0x1B) anywhere.

And the coresponding error message is : [MCP980X] Pointer Invalid -> most probably it refers to the temperature register pointer, but what is going on there is a dummy write - here is the extract from the MCP9801 datasheet showing how to read from the temperature register:

http://imageshack.us/photo/my-images/51/readingdiagram.jpg

And here is the content of the I2C header I am using (program written in Keil):

Rich (BB code):
#include <reg51.h>

sbit SDA=P0^0;
sbit SCL=P0^1;

void I2CInit()
{
	SDA=1;
	SCL=1;
}//I2CInit

void I2CStart()
{
	SCL = 1;
	SDA = 0;
	SCL = 0;
}//I2CStart

void I2CRestart()
{
	SCL = 0;
	SDA = 1;
	SCL = 1;
	SDA = 0;
}//I2CRestart

void I2CStop()
{
	SCL = 0; 
	SDA = 0;
	SCL = 1;
	SDA = 1;
}//I2CStop

void I2CAck()
{
	SDA = 0;
	SCL = 1;
	SCL = 0;
	SDA = 1;
}//I2CAck

void I2CNak()
{
	SDA = 1;
	SCL = 1;
	SCL = 0;
}//I2CNak

void I2CSend(unsigned char Data)
{
	unsigned char i;

	for(i=0;i<8;i++)
	{
		SCL = 0;

		if((Data&0x80)==0)
			SDA=0;
		else SDA = 1;

		SCL=1;
		Data<<=1;
	}//for

	SCL = 0;
	SDA = 1;
}//I2CSend

unsigned char I2CRead()
{
	unsigned char i, Data=0;

	for(i=0;i<8;i++)
	{
		SCL=0;
		SCL=1;

		if(SDA)
			Data|=1;
		Data<<=1;
	}//for

	SCL = 0;
	SDA = 1;

	return Data;
}//I2CRead

Also here is the MCP9801 datasheet for reference: http://www.datasheetcatalog.org/datasheet2/d/0jah5j3yagu1fft7wczs570sskfy.pdf

What is it I am missing?

Thank you for having the patience to go trough such a long post.
 
Last edited by a moderator:

Thread Starter

Cable Guy

Joined Mar 23, 2012
3
I have figure out my problem; anyone is free to laugh at my code, since I can't seem to be able to edit the previous message.:)

I will be posting my complete thermostat project (C code and simulation screenshot) when it will be complete.
 
Top