bitsurfer
Member level 3
Hello,
I'm trying to do some read/write strings to/from eeprom but mostly unsuccessfully.
Here is code:
And with this code I can store and read single byte.
But when I try with char array then program dont work as expected.
Why I can't store/read/display whole string and how to get this working?
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?