Interfacing AT89S52 with AT24C64 EEPROM using I2C

Status
Not open for further replies.

rajesh279

Junior Member level 3
Joined
Jun 11, 2009
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,521
Hi All,
I am interfacing AT89S52 with AT24C64 EEPROM using I2C protocol. My purpose is to send a value, say "1" to a memory location and readback the same value.
But the problem I am facing is that : whatever value I am sending to the memory location, while reading, I am always getting 0.
Can some one please help me to sort out this problem?
I know I am doing some mistakes in my code, but not able to solve it.
Also, I written two different Read and Write function, but I am not sure which one will work.

Following is my code:

Code:
#include <AT89X52.H>  // Header file for IO operations
#include "intrins.h"

/* AT24C64 Memory*/
#define SDA P3_7
#define SCL P3_5
#define WP  P3_4

bit ack_bit;
unsigned char output;

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

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

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

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

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

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

unsigned char I2CSend1(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;
         }
         SCL = 0;
	     ack_bit = SDA;
		 SCL = 1;
		 SCL = 0;
         //SDA = 1;
	
		 return !ack_bit;
}

void I2CSend(unsigned char value)	//send byte serially
{ 
	unsigned int i;
	unsigned char send;
	send=value;
	for(i=0;i<8;i++)
	{
		SDA=send/128;			//extracting MSB
		send=send<<1;			//shiftng left
		SCL=1;
		_nop_();
		_nop_();
		SCL=0;
	}
   ack_bit=SDA;					//reading acknowledge
   SDA=0;
}

unsigned char I2CRead1()			//reading from EEPROM serially
{
	unsigned int i,reead;

	SDA=1;
	reead=0;
	for(i=0;i<8;i++)
	{
		reead=reead<<1;
		SCL=1;
		_nop_();
		_nop_();
		if(SDA==1)
			reead++;
		SCL=0;
	}
	SDA=0;
	return reead;				//Returns 8 bit data here
}	

unsigned char I2CRead(){
        unsigned char i, Data=0;
        for(i=0;i<8;i++){
                SCL = 0;
                SCL = 1;
                if(SDA)
                        Data |=1;
                if(i<7)
						Data<<=1;
        }
        SCL = 0;
        SDA = 1;
        return Data;
}

void Save()					//save in EEPROM
{
	I2CStart();
	I2CSend(0xA0);						//device address
	I2CAck();
	I2CSend(0x11);						//word address
	I2CAck();
	I2CSend("1");							//send data
	I2CAck();
	I2CStop();							   
	if(ack_bit==0)
	{
		//
	}
	else
		//

	I2CAck();
}

void Read()
{
	I2CStart();
	I2CSend(0xA0);
	I2CAck();
	I2CSend(0x11);
	I2CAck();
	I2CStart();
	I2CSend(0xA1);					 //device address
	I2CAck();
	output=I2CRead(); //read the value
	I2CAck();
	I2CStop();
	I2CAck();
}


int main(void)
{

Save(); //first save the values in EEPROM

Read(); //Then read the values from EEPROM


}
 

Hi,
You should have to know that, your write code or read code which is properly work.So, that's way 1st you check your write code using some indication after write, then check write is properly done or not, if done then using this process check the read code. By this process you would define your problem, and will be able to solve.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…