[Moved] problem in saving data to eeprom

Status
Not open for further replies.

adnansarwar2050

Member level 2
Joined
Oct 12, 2011
Messages
52
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,288
Location
Islamabad
Visit site
Activity points
1,589
i am using 24c02 eeprom for the first time. i have coded for writing and reading data but the data i am reading is not matching with that i have written. Please check my code where is the mistake. i am using 89c51.

Code:
#include <AT89x51.h>
#include<intrins.h>
sbit sda = P1^0;
sbit clk = P1^1;
int i;


void EEPROM_Write(char address,char dat)
	{
		char device = 0xA0;
		sda = 1;
		clk = 1;
		_nop_();
		sda = 0;
		clk = 0;

		for (i = 0; i<8; i++)
			{
				clk = 0;
				if(device & 0x80)
					sda = 1;
				else
					sda = 0;
				clk = 1;
				device = device << 1;
			}

		 clk = 0;
		 _nop_();
		 clk = 1;
		 _nop_();

		 if(!sda)
		 {
		 	clk = 0;
			_nop_();
			clk = 1;
			_nop_();
			clk = 0;


			for (i = 0; i<8; i++)
			{
				clk = 0;
				if(address & 0x80)
					sda = 1;
				else
					sda = 0;
				clk = 1;
				address = address << 1;
			}

			if(!sda)
		 		{
		 			clk = 0;
					_nop_();
					clk = 1;
					_nop_();
					clk = 0;
					P2 = 0x29;

					for (i = 0; i<8; i++)
						{
							clk = 0;
							if(dat & 0x80)
								sda = 1;
							else
								sda = 0;
							clk = 1;
							dat = dat << 1;
						}

					 	if(!sda)
					 		{
					 			clk = 0;
								_nop_();
								clk = 1;
								_nop_();
								clk = 0;
							}

						sda = 0;
						_nop_();	
						clk = 1;
						sda = 1;
						_nop_();
						clk = 0;
						_nop_();
						_nop_();
						_nop_();
						_nop_();


				}
		 }
	
	}




char EEPROM_Read(char address)
	{
		char device = 0xA1;
		char readvalue;
		sda = 1;
		clk = 1;
		_nop_();
		sda = 0;
		clk = 0;

		for (i = 0; i<8; i++)
			{
				clk = 0;
				if(device & 0x80)
					sda = 1;
				else
					sda = 0;
				clk = 1;
				device = device << 1;
			}

		 clk = 0;
		 _nop_();
		 clk = 1;
		 _nop_();

		 if(!sda)
		 {
		 	clk = 0;
			_nop_();
			clk = 1;
			_nop_();
			clk = 0;


			for (i = 0; i<8; i++)
			{
				clk = 0;
				if(address & 0x80)
					sda = 1;
				else
					sda = 0;
				clk = 1;
				address = address << 1;
			}

			if(!sda)
		 		{
		 			clk = 0;
					_nop_();
					clk = 1;
					_nop_();
					clk = 0;
					
					

					for (i = 0; i<8; i++)
						{	
							clk=1;
							_nop_();
							if(sda == 1)
								readvalue++;
							clk=0;
							readvalue = readvalue<<1;
						}

					 	if(!sda)
					 		{
					 			clk = 0;
								_nop_();
								clk = 1;
								_nop_();
								clk = 0;
							}
					
						sda = 0;
						_nop_();	
						clk = 1;
						sda = 1;
						_nop_();
						clk = 0;
				}
		 }

		 return (readvalue);
	}





void main ()
	{
		char address = 0x05;
		char dat = 0x68;
		char read;
		P0 = 0;
		P1 = 0;
		P2 = 0;
		P3 = 0;
		
		EEPROM_Write(address,dat);
		read = EEPROM_Read(address);

		if (read == dat)
			P1_5 = 1;
		while (1)
			{
				
			}

	}
 
Last edited by a moderator:

Re: problem in saving data to eeprom

Hey, I realise that this post is almost two years old but maybe if there's still someone looking for an answer.

If you look at the datasheet of that EEPROM,
http://www.atmel.com/Images/doc0180.pdf
on p. 5 it describes a write cycle time. After you write to it you need to leave it alone for 5 ms at least before you try to read, otherwise the operation doesn't happen correctly.

Regards,
James
 

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…