Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

8051 C, read/write eeprom AT24C02

Status
Not open for further replies.

bitsurfer

Member level 3
Joined
Jul 19, 2012
Messages
56
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Croatia
Activity points
1,734
Hello,
I'm trying to do some read/write strings to/from eeprom but mostly unsuccessfully.
Here is code:
Code:
#include<AT89X52.H>

void delay1ms(unsigned int count);
void _nop(unsigned int count);
//---------------------------------------
void start_iic(void);
void stop_iic(void);
void write_iic(unsigned char Data);
unsigned char read_iic();
unsigned char read_byte(unsigned int addr);
void write_byte(unsigned int addr, unsigned char Data);
//---------------------------------------
unsigned char i;
sbit SDA = P3^5;
sbit SCL = P3^4;
//-----------------
sbit rs = P2^7;		
sbit rw = P2^6;
sbit ep = P2^5;
lcd_init();
lcd_clear();
bit lcd_busy();
lcd_wcmd(unsigned char cmd);
lcd_pos(unsigned char pos);
lcd_putchar(unsigned char dat);	
int o;
//---------------------------------------

void main(void)
{
int u;	 	 
unsigned char mystr[16] = "eeprom test";
unsigned char r_data[16];

	lcd_init(); 			
	lcd_clear();
	
	write_byte(0x01,mystr[0]); // in this case letter 'e'
	delay1ms(10);

	r_data[0] = read_byte(0x01);
	lcd_putchar(r_data[o]);
} 

//---------------------------------------
void start_iic(void)
{
    SDA = 1;
	SCL = 1;
	_nop(2);
	SDA = 0;
	_nop(2);
	SCL = 0;
	_nop(2);
}

void stop_iic(void)
{
	SDA = 0;	    	
	_nop(2);
	SCL = 1;
	_nop(2);
	SDA = 1;	
}
 
void write_iic(unsigned char Data)
{    
	for (i=0;i<8;i++)
	{
        if ((Data&0x80) == 0) SDA = 0; else SDA = 1;
		SCL=1;SCL=0;
		Data<<=1;
	}
  	SCL = 1; 
	_nop(2);
	SCL = 0;
  }
	
unsigned char read_iic()
{
    unsigned char Data=0;

    SDA = 1;	
	for (i=0;i<8;i++)
	{
		SCL   = 1;		
		Data  = (Data|SDA);
		Data<<= 1;		
		SCL   = 0;
		_nop(1);
	}
	SCL = 1;		
	_nop(2);
	SCL = 0;
	return Data;
}

unsigned char read_byte(unsigned int addr)
{
   	unsigned char Data;
	start_iic();
	write_iic(0xA0|((addr>>7)&0x0E));
	write_iic((unsigned char)addr&0xFF);
	start_iic();
	write_iic(0xA1);
	Data = read_iic();
	stop_iic();
   	return(Data);
}

void write_byte(unsigned int addr, unsigned char Data)
{
	start_iic();
	write_iic(0xA0|((addr>>7)&0x0E));
	write_iic((unsigned char)addr&0xFF);	// send address low	
	write_iic(Data);
	stop_iic();	
}

//-----------------------------------------------				 
void delay1ms(unsigned int count) 
{  
    unsigned int i;		       		
    while(count) 
	{
        i = 115; 
		while(i>0) i--;
        count--;
    }
}

void _nop(unsigned int count)
{  
	int t;
	for(t=0; t<count; t++);
}

//-----------------------------------------------
bit lcd_busy()
{							 
	bit result;
	rs = 0;
	rw = 1;
	ep = 1;
	result = (bit)(P0 & 0x80);
	ep = 0;
	return result;	
}

lcd_wcmd(unsigned char cmd)
{							
	while(lcd_busy());
	rs = 0;
	rw = 0;
	ep = 0;
	P0 = cmd;
	ep = 1;
	ep = 0;		
}

lcd_pos(unsigned char pos)
{							
	lcd_wcmd(pos|0x80);
}

lcd_putchar(unsigned char dat)	
{							 
	while(lcd_busy());
	rs = 1;
	rw = 0;
	ep = 0;
	P0 = dat;
	ep = 1;
	ep = 0;	
}

lcd_init()
{							 
	lcd_wcmd(0x38);
	delay1ms(1);
	lcd_wcmd(0x0c);			
	delay1ms(1);
	lcd_wcmd(0x06);			
	delay1ms(1);
}

lcd_clear()
{							 
	lcd_wcmd(0x01);
}

And with this code I can store and read single byte.
But when I try with char array then program dont work as expected.

Code:
	for (u=0; u<16; u++)
           {
		write_byte(0x01+u,mystr[u]);
	}
	
	delay1ms(10);

	for (u=0; u<16; u++)
	{
		r_data[u] = read_byte(0x01+u); 			
	}


		lcd_pos(0);
		o = 0;
		while(r_data[o]!= '\0')
		{						 
			lcd_putchar(r_data[o]);
			o++;
		}

Why I can't store/read/display whole string and how to get this working?
 

Well, I get "something" but still not expected result.
Now I have situation as follows:

Code:
getstring(unsigned int shift)
{
	int u;
	unsigned char getdata[16] = "                ";

	for (u=0; u<16; u++)
	{
		getdata[u] = read_byte((shift*16)+u); 			
		P1=getdata[u]; //to see what happens on led's
		delay1ms(100); //slowdown
		lcd_putchar(getdata[u]);
	}
}

setstring(unsigned int shift, unsigned char *mystr)
{
	int u;
	for (u=0; u<16; u++)
    {
		write_byte((shift*16)+u,mystr[u]);
	    delay1ms(1);
	}
}

void main(void)
{
	unsigned int shift=0;
	unsigned char mystr[16] =   "Hello world 8051";

	lcd_init(); 			
	lcd_clear();
	lcd_pos(0);

	setstring(shift, mystr);
	delay1ms(10);
	getstring(shift);
}

Bytes are written and same bytes are readded after shutdown.
But:
1) I can't read bytes in ascii. They looks like arabic letters and I expect latin sentence "Hello world 8051".
2) By adding variable "shift" I have idea to define memory location in eprom from which read/write will become.
This also seem's not work as expected.

Any help will be appreciated!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top