Discussion in "Project Help" started by    jayachandra    Aug 2, 2014.
Sat Aug 02 2014, 09:50 am
#1
hello,
i have problem to program eeprom, im doing just saving the bit values in eeprom, but read and write function is nt working,
wher i make a mistake in this ?
in main program read and write functions i kept in comments /* read/write function*/
i'm using at89s51 controller, 24c16k eeprom with 12mhz crystal,
please help me to solve this
#include<reg51.h>
#include<intrins.h>
#include"pro_lcd.h"
#include"eprom.h"

 sbit inc=P2^0;    		//time increasing 
 sbit dec=P2^1;	   		//time discreasing
 sbit save=P2^2;	   	//save to eeprom 
 sbit mode=P2^3;	   	//mode selector

 sbit mode1=P3^1;	    //mode1 indication led
 sbit mode2=P3^2;	    //mode2 indication led
 
 void delay2();

unsigned int v1,v2;
unsigned int k[]={'0','1','2','3','4','5','6','7','8','9'};
unsigned char c1; 
unsigned int mode_val,r;

void main(void)
{	
	mode1=mode2=0;								//mode1,mode2 led's off	
     
	lcdinit();		    				
    lcdcmd(0x01);					
	lcdcmd(0x80);						
    lcddis("EEPROM TEST",11);
	for(c1=0;c1<55;c1++)				
	{ 	
		delay1(999);
	} 
	lcdcmd(0x01);
									
/*	mode_val=0x20;		
	high_add=0x00;
	low_add=0x20;   
	for(r=0;r<20;r++)
	{  
	   	write();			   //DUMMY WRITE
	}
	temp=1;
	mode_val=0x20;
	high_add=0x00;
	low_add=0x20;
	for(r=0;r<20;r++)
	{
      	read();				  //READ THE (mode_val) value from eeprom			    
	}  	   */	
								
waits:	if(mode==0)				 //to change mode
		{
			mode_val++;				// (mode_val=0 for MODE1)
			for(c1=0;c1<10;c1++)	// (mode_val=1 for MODE2)
			{ 	delay1(200);
			}if(mode_val>
=2) mode_val=0;

		/*	high_add=0x00;
			mode_val=0x20;   
			for(r=0;r<20;r++)
			{  
	   			write();		//WRITE (mode_val) value to eeprom
			}	*/		
		}
							 
		if(mode_val==0)				//entering MODE1
		{
		/*	high_add=0x00;
			low_add=mode_val;   
			for(r=0;r<20;r++)
			{  
			   	write();			   //DUMMY WRITE
			}
			temp=1;	 
			v1=0x40;
			high_add=0x00;
			low_add=mode_val;
			for(r=0;r<20;r++)
			{
		      	read();			   //READ THE (v1) value from eeprom					    
			}		*/   

			mode1=1;				//mode1 indecation led on
			mode2=0;				//mode2 indecation led off
			lcdcmd(0x01);			
	  		lcdcmd(0x80);		
			lcddis("VALUE1:",6);
			lcdcmd(0x8a);		
			lcddat(k[v1]);	

			while(1)
			{   					
				if(inc==0)				   
				{	lcdcmd(0x8a);
					lcdcmd(0x0f);
					v1++;
					if(v1>
=10)
					{
						v1=0;
					}
					lcdcmd(0x8a);
					lcdcmd(0x0e);
					lcddat(k[v1]);
					lcdcmd(0x8a);
					delay2();	
				}		
				if(dec==0)		   //value (discreasing)
				{  	v1--;
					if(v1>
=10)
					{
						v1=9;
					}
					lcdcmd(0x8a);
					lcdcmd(0x0e);
					lcddat(k[v1]);
					lcdcmd(0x8a);
					delay2();
				}	
			
			 if(save==0)				
			 {	lcdcmd(0x0c);
			 	lcdcmd(0xc0);
				lcddis("saved",5);
			/* 	v1=0x40;
				high_add=0x00;	 //WRITE the v1 value in EEPROM
				low_add=mode_val;   
				for(r=0;r<20;r++)
				{  
		   			write();			   
				}		*/														   							 		 
			 }				 		  
		  }
		}			
				delay1(999);				//normal delay
				delay1(999);
				delay1(999);
				delay1(999);
//***************************************************
		if(mode_val==1)			//if (mode_val==1)enter MODE2
		{						//read MODE2 value
		/*	v2=0x60;
			high_add=0x00;
			low_add=v1;   
			for(r=0;r<20;r++)
			{  
			   	write();			   //DUMMY WRITE
			}
			temp=1;
			v2=0x60;
			high_add=0x00;
			low_add=mode_val;
			for(r=0;r<20;r++)
			{
		      	read();			   //READ THE (v2) value from eeprom
					    
			}	*/		
					   	
				
			mode1=0;				//mode1 indecation led off
			mode2=1;				//mode2 indecation led on
			lcdcmd(0x01);			
	  		lcdcmd(0x80);		
			lcddis("VALUE2:",6);
			lcdcmd(0x8a);		
			lcddat(k[v2]);	
					
			while(1)
			{						   // (increasing)
				if(inc==0)				   
				{	lcdcmd(0x8a);
					lcdcmd(0x0f);
					v2++;
					if(v2>
=10)
					{
						v2=0;
					}
					lcdcmd(0x8a);
					lcdcmd(0x0e);
					lcddat(k[v2]);
					lcdcmd(0x8a);
					delay2();	
				}		
				if(dec==0)		  		// (discreasing)
				{  	v2--;
					if(v2>
=10)
					{
						v2=9;
					}
					lcdcmd(0x8a);
					lcdcmd(0x0e);
					lcddat(k[v2]);
					lcdcmd(0x8a);
					delay2();
				}	
		
				if(save==0)				
				{	lcdcmd(0x0c);
					lcdcmd(0xc0);
					lcddis("saved",5);
			/*	 	v2=0x60;
					high_add=0x00;  //WRITE v2 value in EEPROM
					low_add=0x60;   
					for(r=0;r<20;r++)
					{  
			   			write();			   
					}	*/											   							 		 
				}
			}			  

		}else goto waits;
}	// MAIN END

void delay2()
{
	unsigned int ja;
	for(c1=0;c1<10;c1++)
	{
		for(ja=0;ja<5535;ja++);
	}
}]
//**********************************************************
//****************eprom.h***********************************
[code=c][/eprom.h] 
 sbit sda=P3^4;
 sbit scl=P3^5;			
 
 sbit a7=ACC^7;		   //define ACC in bits
 sbit a6=ACC^6;
 sbit a5=ACC^5;
 sbit a4=ACC^4;
 sbit a3=ACC^3;
 sbit a2=ACC^2;
 sbit a1=ACC^1;
 sbit a0=ACC^0;	
 
 void start();
 void shout();
 void shin();
 void nak();

 void read();
 void write();
 unsigned char temp,n,high_add,low_add;

void write()
{
		start();
		ACC=0xa0;		//slave(device) address
	 	shout();		
		ACC=high_add;		//higher byte address
		shout();		
		ACC=low_add;			//lower byte address
		shout();		
		ACC=temp;
		shout();
}
//*********************************************************
void read()
{	 
		start();
		ACC=0xa0;		//slave(device) address with write bit =0
		shout();
		ACC=high_add;		//higher byte address
		shout();
		ACC=low_add;		//lower byte address
		shout();
		ACC=temp;
		shout();
		start();			//restart
		ACC=0xa1;		//slave(device) address with read bit =1
		shout();	
		shin();			
		nak();			
}
//******************************************************************
void start()
{
	sda=1;
	scl=1;		
		_nop_();
			sda=0;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
			scl=0;
			//c=0;
}
//**********************************************************
void shout()
{
				sda=a7;
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				sda=a6;
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				sda=a5;
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				sda=a4;
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;

				sda=a3;
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				sda=a2;
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				sda=a1;
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				sda=a0;
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
//* acknowlodgement		
				sda=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;			
} 
//**********************************************************
void shin()
{

		sda=1;	            //make scl=sda=1 while read
			_nop_();
			_nop_();
		scl=1;
			_nop_();
			_nop_();
		
				a7=sda;
				_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				a6=sda;
				_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				a5=sda;
				_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				a4=sda;
				_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				a3=sda;
				_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				a2=sda;
				_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				a1=sda;
				_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
				
				a0=sda;
				_nop_();
				scl=1;
			_nop_();
			_nop_();
			_nop_();
			_nop_();
			_nop_();
				scl=0;
}
//**********************************************************
void nak()
{
		sda=1;			// NAK bit
		_nop_();
		_nop_();		
		scl=1;			// raise clock
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
		scl=0;				// drop clock
		_nop_();
}

//***********************************************************
//***************pro_lcd.h************************************
[code=c][/pro_lcd.h]sbit rs=P1^0;
sbit en=P1^1;

sbit d4=P1^4;
sbit d5=P1^5;
sbit d6=P1^6;
sbit d7=P1^7;

sbit m0=B^0;
sbit m1=B^1;
sbit m2=B^2;
sbit m3=B^3;
sbit m4=B^4;
sbit m5=B^5;
sbit m6=B^6;
sbit m7=B^7;

void lcdinit();
void lcddat(unsigned char);
void lcdcmd(unsigned char);
void delay1(unsigned char);
void lcddis(unsigned char *s,unsigned char r);
unsigned char x,y,z;

void lcddis(unsigned char *s,unsigned char r)
{
	unsigned int w;
	for(w=0;w<r;w++)
	{
		lcddat(s[w]);
		delay1(50);
	}
}
void lcdinit()
{ 
	lcdcmd(0x03);
	delay1(100);
	lcdcmd(0x03);
	delay1(100);
	lcdcmd(0x02);
	delay1(100);
	lcdcmd(0x28);
	delay1(100);
	lcdcmd(0x06);
	delay1(100);
	lcdcmd(0x0c);
	delay1(100);
	lcdcmd(0x80);
	delay1(100);
	lcdcmd(0x01);
	delay1(100);
	lcdcmd(0xc0);
	delay1(100);
}
void lcddat(unsigned char dat)
{
	B=dat;
	rs=1;
	d4=m4;
	d5=m5;
	d6=m6;
	d7=m7;
	en=1;
	delay1();
	en=0;
	d4=m0;
	d5=m1;
	d6=m2;
	d7=m3;
	en=1;
	delay1();
	en=0;
}
void lcdcmd(unsigned char cmd)
{
	B=cmd;
	rs=0;
	d4=m4;
	d5=m5;
	d6=m6;
	d7=m7;
	en=1;
	delay1(2);
	en=0;
	d4=m0;
	d5=m1;
	d6=m2;
	d7=m3;
	en=1;
	delay1(2);
	en=0;
}

void delay1(unsigned char itime)
{
	unsigned int i,j ;
	for(i=0;i<itime;i++)
	for(j=0;j<10;j++);
}
//***********************************


[ Edited Mon Aug 04 2014, 10:59 am ]
Mon Aug 04 2014, 11:02 am
#2
First of all, It is NOT recommended to use ACC as a variable in keil C. The reason is accumulator is used by compiler for the generated code. So when you are using inside your program it may cause issues.

You can use i2c library available in our tutorial section and try to do read write again. There is also a sample code for RTC which will help you understand how to do read/write.
http://www.8051projects.net/microcontroller-tutorials/#interfacing
Mon Aug 04 2014, 12:15 pm
#3
thanq very much @ ajay for giving reply,
il check the above link. after that if i have any doubt il ask
thanq

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

ArnoldDiant
Fri Apr 26 2024, 03:53 am
RodneyKnorb
Thu Apr 25 2024, 07:08 pm
Williamjef
Thu Apr 25 2024, 02:08 pm
SamuelSmise
Thu Apr 25 2024, 09:56 am
DustinErele
Thu Apr 25 2024, 08:44 am
ztaletpzca
Wed Apr 24 2024, 11:19 pm
IrardlPex
Wed Apr 24 2024, 08:42 pm
Charlestehed
Wed Apr 24 2024, 05:20 pm