AT24C128EEPROM页读写程序问题,望高手指点!
最近,我在做一个12864文本编辑器实验,费了很大努力,24C02页读写已经成功了,进行到第二步,换上AT24C128做页读写的尝试,碰到了问题。不是我不愿意自己钻研,可以参考的资料太少了,才上这向各位高手求教。下面是我写的两个程序,敬请指教!/*******************************************************************/
//
// 本程序目标:测试所有页连续读写,测试已成功
// 使用的EEPROM型号:24C02
//
/*******************************************************************/
#include"STC15.H"
#define ADDR24C02 0x50
#define W 0
#define R 1
sbit SDA=P0^0;
sbit SCL=P0^1;
sbit RS=P4^1;
sbit RW=P4^2;
sbit E=P4^4;
sbit CS1=P4^5;
sbit CS2=P3^7;
sbit RST=P3^6;
unsigned char A2A1A0=0x01;
unsigned char code Text_Code[256]=
{
/*-- 文字: 失 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xBF,0xDF,0xE7,0xE9,0xEF,0xEF,0x00,0xEF,0xEF,0xEF,0xEF,0xEF,0xFF,0xFF,0xFF,
0xFF,0xFE,0x7E,0xBE,0xDE,0xEE,0xF2,0xFC,0xFA,0xF6,0xCE,0x9E,0x3E,0xBE,0xFE,0xFF,
/*-- 文字: 败 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xFF,0x01,0xFD,0x0D,0xFD,0x01,0xDF,0x07,0xE8,0xED,0xEF,0x0F,0xEF,0xEF,0xFF,
0xFF,0xBF,0xCE,0xF3,0xFC,0xFB,0x46,0xAF,0xDF,0xEC,0xF3,0xEC,0x9F,0x3F,0xBF,0xFF,
/*-- 文字: 是 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x7F,0x7F,0x7F,0x7F,0x41,0x55,0x55,0x55,0x55,0x55,0x55,0x41,0x7F,0x7F,0x7F,0xFF,
0xFF,0xBF,0xDF,0xEF,0xF1,0xEF,0xDF,0xC0,0xBB,0xBB,0xBB,0xBB,0xBB,0xBF,0xBF,0xFF,
/*-- 文字: 成 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xFF,0x07,0xB7,0xB7,0xB7,0x37,0xF7,0x00,0xF7,0xF6,0xF5,0x37,0x77,0xF7,0xFF,
0xBF,0xCF,0xF0,0xFF,0xF7,0xAF,0xB0,0xDF,0xEF,0xF4,0xF3,0xED,0xDE,0xBF,0x0F,0xFF,
/*-- 文字: 功 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xFB,0xFB,0xFB,0x03,0xFB,0xEB,0xEB,0xEF,0x6F,0x80,0xEF,0xEF,0x0F,0xFF,0xFF,
0xFB,0xF3,0xFB,0xFB,0xFC,0xBD,0xDD,0xEE,0xF3,0xDC,0xDF,0x9F,0xDF,0xE0,0xFF,0xFF,
/*-- 文字: 之 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xF7,0xF7,0xF7,0xF7,0xF7,0xF6,0xF5,0xF3,0x77,0xB7,0xD7,0xE3,0xF7,0xFF,0xFF,
0xDF,0x9F,0xCF,0xF7,0xE7,0xDB,0xDB,0xDD,0xBE,0xBF,0xBF,0xBF,0xBF,0xBF,0xBF,0xFF,
/*-- 文字: 母 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xBF,0xBF,0xBF,0x01,0xBD,0xBD,0xB5,0x8D,0xBD,0xBD,0xBD,0x01,0xBF,0xBF,0xBF,0xFF,
0xFF,0xFF,0xFF,0xF0,0xF7,0xF7,0xF6,0xF1,0xF7,0xB7,0x77,0x80,0xF7,0xF7,0xFF,0xFF,
/*-- 文字: 。 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xE7,0xDB,0xDB,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char xdata Text_Buf[256];
void Init_IO();
void Init_IIC();
void Init_LCD();
void IIC_Start();
void IIC_Stop();
void Addr_Dive(unsigned char addr_dive,bit opr);
void Addr_Word(unsigned int addr_word);
void Read_Pages(unsigned char addr_dive,unsigned int addr_word,unsigned char *text_buf);
void Write_Pages(unsigned char addr_dive,unsigned int addr_word,unsigned char *text_code);
unsigned char Read_Byte();
void Write_Byte(unsigned char byte);
bit Read_ACK();
void Write_ACK();
void Write_NACK();
void Delay_uS();
void Clear_Screen();
void Start_Line_Set(unsigned char Line);
void Addr_Page_Set(unsigned char Page);
void Addr_Col_Set(unsigned char Col);
void Display_Texts(unsigned char *text_code,unsigned char page,unsigned char col,unsigned char num);
void Wcom(unsigned char com);
void WDat(unsigned char Dat);
void Ready();
void Delay(unsigned int t);
void main()
{
Init_IO();
Init_IIC();
Init_LCD();
Clear_Screen();
Write_Pages(ADDR24C02,0x00,Text_Code);
Display_Texts(Text_Code,1,0,8);
Read_Pages(ADDR24C02,0x00,Text_Buf);
Display_Texts(Text_Buf,4,0,8);
while(1);
}
void Init_IO()
{
P2M1=0x00;
P2M0=0x00;
P3M1=0x00;
P3M0=0x00;
P4M1=0x00;
P4M0=0x00;
}
void Init_IIC()
{
P0M1=0x03;
P0M0=0x03;
SCL=0;
SDA=1;
}
void Init_LCD()
{
RST=0;
Delay(600);
RST=1;
CS1=1;
CS2=1;
Wcom(0x3F);
}
void IIC_Start()
{
SDA=1;
SCL=1;
Delay_uS();
SDA=0;
Delay_uS();
SCL=0;
Delay_uS();
}
void IIC_Stop()
{
SCL=0;
SDA=0;
Delay_uS();
SCL=1;
Delay_uS();
SDA=1;
Delay_uS();
}
void Addr_Dive(unsigned char addr_dive,bit opr)
{
bit ack=0;
static unsigned char i=0;
if(opr)
{
Write_Byte((addr_dive<<1)|(A2A1A0<<1)|R);
}
else
{
Write_Byte((addr_dive<<1)|(A2A1A0<<1)|W);
i++;
}
do
{
ack=Read_ACK();
}
while(ack);
}
void Addr_Word(unsigned int addr_word)
{
bit ack=0;
Write_Byte(addr_word);
do
{
ack=Read_ACK();
}
while(ack);
}
void Read_Pages(unsigned char addr_dive,unsigned int addr_word,unsigned char *text_buf)
{
unsigned char i,j;
IIC_Start();
Addr_Dive(addr_dive,W);
Addr_Word(addr_word); //每页8Byte
IIC_Start();
Addr_Dive(addr_dive,R);
for(i=0;i<32;i++) //32页
{
for(j=0;j<8;j++) //读一页
{
if(i==31&&j==7)
{
*text_buf=Read_Byte();
Write_NACK();
}
else
{
*text_buf=Read_Byte();
text_buf++;
Write_ACK();
}
}
}
IIC_Stop();
}
void Write_Pages(unsigned char addr_dive,unsigned int addr_word,unsigned char *text_code)
{
unsigned char i,j;
for(i=0;i<32;i++)
{
IIC_Start();
Addr_Dive(addr_dive,W);
Addr_Word(addr_word+i*8); //
for(j=0;j<8;j++)
{
Write_Byte(*text_code);
text_code++;
while(Read_ACK());
}
IIC_Stop();
Delay(4500);
}
}
unsigned char Read_Byte()
{
unsigned char byte=0x00;
unsigned char i;
SCL=0;
SDA=1;
for(i=0;i<8;i++)
{
SCL=1;
Delay_uS();
byte=byte<<1;
if(SDA==1)
{
byte=byte|1;
}
else
{
byte=byte|0;
}
SCL=0;
}
return(byte);
}
void Write_Byte(unsigned char byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCL=0;
if(byte&(0x80>>i))
{
SDA=1;
}
else
{
SDA=0;
}
Delay_uS();
SCL=1;
Delay_uS();
SCL=0;
}
}
bit Read_ACK()
{
bit ack=0;
SCL=0;
SDA=1;
SCL=1;
Delay_uS();
if(SDA==1)
{
ack=1;
}
SCL=0;
return(ack);
}
void Write_ACK()
{
SCL=0;
SDA=0;
Delay_uS();
SCL=1;
Delay_uS();
SCL=0;
}
void Write_NACK()
{
SCL=0;
SDA=1;
Delay_uS();
SCL=1;
Delay_uS();
SCL=0;
}
void Delay_uS()
{
;
}
void Clear_Screen()
{
unsigned char Page,Col,Start_Line=0,Start_Col=0;
Start_Line_Set(Start_Line);
for(Page=0;Page<8;Page++)
{
Addr_Page_Set(Page);
Addr_Col_Set(Start_Col);
for(Col=0;Col<64;Col++)
{
WDat(0xFF);
}
}
}
void Start_Line_Set(unsigned char Line)
{
Wcom(0xC0|Line);
}
void Addr_Page_Set(unsigned char Page)
{
Wcom(0xB8|Page);
}
void Addr_Col_Set(unsigned char Col)
{
Wcom(0x40|Col);
}
void Display_Texts(unsigned char *text_code,unsigned char page,unsigned char col,unsigned char num)
{
unsigned char i,j,k;
unsigned char *ptr=text_code;
Start_Line_Set(0);
for(i=0;i<num;i++)
{
if(i<4)
{
CS1=1;
CS2=0;
}
else
{
CS1=0;
CS2=1;
}
for(j=0;j<2;j++)
{
Addr_Page_Set(page+j);
Addr_Col_Set(col+16*i);
for(k=0;k<16;k++)
{
WDat(*ptr);
ptr++;
}
}
}
}
void Wcom(unsigned char com)
{
RS=0;
RW=0;
E=1;
Delay(500);
P2=com;
E=0;
}
void WDat(unsigned char Dat)
{
RS=1;
RW=0;
E=1;
Delay(500);
P2=Dat;
E=0;
}
void Delay(unsigned int t)
{
while(t--);
}
这是上面源代码的实验结果:
这个实验是成功了。
下面是换上AT24C128的源代码和实验结果:
/*******************************************************************/
//
// 本程序目标:测试所有页连续读写,测试未成功
// 使用的EEPROM型号是AT24C128
//
/*******************************************************************/
#include"STC15.H"
#define ADDR24C128 0x50
#define W 0
#define R 1
sbit SDA=P0^0;
sbit SCL=P0^1;
sbit RS=P4^1;
sbit RW=P4^2;
sbit E=P4^4;
sbit CS1=P4^5;
sbit CS2=P3^7;
sbit RST=P3^6;
unsigned char A1A0=0x01;
unsigned char code Text_Code[256]=
{
/*-- 文字: 失 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xBF,0xDF,0xE7,0xE9,0xEF,0xEF,0x00,0xEF,0xEF,0xEF,0xEF,0xEF,0xFF,0xFF,0xFF,
0xFF,0xFE,0x7E,0xBE,0xDE,0xEE,0xF2,0xFC,0xFA,0xF6,0xCE,0x9E,0x3E,0xBE,0xFE,0xFF,
/*-- 文字: 败 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xFF,0x01,0xFD,0x0D,0xFD,0x01,0xDF,0x07,0xE8,0xED,0xEF,0x0F,0xEF,0xEF,0xFF,
0xFF,0xBF,0xCE,0xF3,0xFC,0xFB,0x46,0xAF,0xDF,0xEC,0xF3,0xEC,0x9F,0x3F,0xBF,0xFF,
/*-- 文字: 是 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x7F,0x7F,0x7F,0x7F,0x41,0x55,0x55,0x55,0x55,0x55,0x55,0x41,0x7F,0x7F,0x7F,0xFF,
0xFF,0xBF,0xDF,0xEF,0xF1,0xEF,0xDF,0xC0,0xBB,0xBB,0xBB,0xBB,0xBB,0xBF,0xBF,0xFF,
/*-- 文字: 成 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xFF,0x07,0xB7,0xB7,0xB7,0x37,0xF7,0x00,0xF7,0xF6,0xF5,0x37,0x77,0xF7,0xFF,
0xBF,0xCF,0xF0,0xFF,0xF7,0xAF,0xB0,0xDF,0xEF,0xF4,0xF3,0xED,0xDE,0xBF,0x0F,0xFF,
/*-- 文字: 功 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xFB,0xFB,0xFB,0x03,0xFB,0xEB,0xEB,0xEF,0x6F,0x80,0xEF,0xEF,0x0F,0xFF,0xFF,
0xFB,0xF3,0xFB,0xFB,0xFC,0xBD,0xDD,0xEE,0xF3,0xDC,0xDF,0x9F,0xDF,0xE0,0xFF,0xFF,
/*-- 文字: 之 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xF7,0xF7,0xF7,0xF7,0xF7,0xF6,0xF5,0xF3,0x77,0xB7,0xD7,0xE3,0xF7,0xFF,0xFF,
0xDF,0x9F,0xCF,0xF7,0xE7,0xDB,0xDB,0xDD,0xBE,0xBF,0xBF,0xBF,0xBF,0xBF,0xBF,0xFF,
/*-- 文字: 母 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xBF,0xBF,0xBF,0x01,0xBD,0xBD,0xB5,0x8D,0xBD,0xBD,0xBD,0x01,0xBF,0xBF,0xBF,0xFF,
0xFF,0xFF,0xFF,0xF0,0xF7,0xF7,0xF6,0xF1,0xF7,0xB7,0x77,0x80,0xF7,0xF7,0xFF,0xFF,
/*-- 文字: 。 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xE7,0xDB,0xDB,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char xdata Text_Buf[4*64];
void Init_IO();
void Init_IIC();
void Init_LCD();
void IIC_Start();
void IIC_Stop();
void Addr_Dive(unsigned char addr_dive,bit opr);
void Addr_Word(unsigned int addr_word);
void Read_Pages(unsigned char addr_dive,unsigned int addr_word,unsigned char *text_buf);
void Write_Pages(unsigned char addr_dive,unsigned int addr_word,unsigned char *text_code);
unsigned char Read_Byte();
void Write_Byte(unsigned char byte);
bit Read_ACK();
void Write_ACK();
void Write_NACK();
void Delay_uS();
void Clear_Screen();
void Start_Line_Set(unsigned char Line);
void Addr_Page_Set(unsigned char Page);
void Addr_Col_Set(unsigned char Col);
void Display_Texts(unsigned char *text_code,unsigned char page,unsigned char col,unsigned char num);
void Wcom(unsigned char com);
void WDat(unsigned char Dat);
void Ready();
void Delay(unsigned long t);
void main()
{
Init_IO();
Init_IIC();
Init_LCD();
Clear_Screen();
Write_Pages(ADDR24C128,0x00,Text_Code);
Display_Texts(Text_Code,1,0,8);
Read_Pages(ADDR24C128,0x00,Text_Buf);
Display_Texts(Text_Buf,4,0,8);
while(1);
}
void Init_IO()
{
P2M1=0x00;
P2M0=0x00;
P3M1=0x00;
P3M0=0x00;
P4M1=0x00;
P4M0=0x00;
}
void Init_IIC()
{
P0M1=0x03;
P0M0=0x03;
SCL=0;
SDA=1;
}
void Init_LCD()
{
RST=0;
Delay(600);
RST=1;
CS1=1;
CS2=1;
Wcom(0x3F);
}
void IIC_Start()
{
SDA=1;
SCL=1;
Delay_uS();
SDA=0;
Delay_uS();
SCL=0;
Delay_uS();
}
void IIC_Stop()
{
SCL=0;
SDA=0;
Delay_uS();
SCL=1;
Delay_uS();
SDA=1;
Delay_uS();
}
void Addr_Dive(unsigned char addr_dive,bit opr)
{
bit ack=0;
if(opr)
{
Write_Byte((addr_dive<<1)|(A1A0<<1)|R);
}
else
{
Write_Byte((addr_dive<<1)|(A1A0<<1)|W);
}
do
{
ack=Read_ACK();
}
while(ack);
}
void Addr_Word(unsigned int addr_word)
{
bit ack=0;
Write_Byte(addr_word);
do
{
ack=Read_ACK();
}
while(ack);
}
void Read_Pages(unsigned char addr_dive,unsigned int addr_word,unsigned char *text_buf)
{
unsigned char i,j,k=0;
unsigned char addr_word_1,addr_word_2;
addr_word_1=addr_word>>8;
addr_word_2=addr_word;
IIC_Start();
Addr_Dive(addr_dive,W);
Addr_Word(addr_word_1); //每页64Byte
Addr_Word(addr_word_2);
IIC_Start();
Addr_Dive(addr_dive,R);
for(i=0;i<4;i++) //4页
{
for(j=0;j<64;j++) //读一页
{
if(i==3&&j==63)
{
*text_buf=Read_Byte();
Write_NACK();
}
else
{
k++;
if(k<=256)
{
*text_buf=Read_Byte();
text_buf++;
Write_ACK();
}
else
{
*text_buf='\0';
text_buf++;
}
}
}
}
IIC_Stop();
}
void Write_Pages(unsigned char addr_dive,unsigned int addr_word,unsigned char *text_code)
{
unsigned char i,j,k=0;
unsigned char addr_word_1,addr_word_2;
for(i=0;i<4;i++)
{
addr_word=addr_word+64*i;
addr_word_1=addr_word>>8;
addr_word_2=addr_word;
IIC_Start();
Addr_Dive(addr_dive,W);
Addr_Word(addr_word_1);
Addr_Word(addr_word_2);
for(j=0;j<64;j++)
{
k++;
if(k<=256)
{
Write_Byte(*text_code);
text_code++;
while(Read_ACK());
}
else
{
Write_Byte('\0');
while(Read_ACK());
}
}
IIC_Stop();
Delay(20000);
}
}
unsigned char Read_Byte()
{
unsigned char byte=0x00;
unsigned char i;
SCL=0;
SDA=1;
for(i=0;i<8;i++)
{
SCL=1;
Delay_uS();
byte=byte<<1;
if(SDA==1)
{
byte=byte|1;
}
else
{
byte=byte|0;
}
SCL=0;
}
return(byte);
}
void Write_Byte(unsigned char byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCL=0;
if(byte&(0x80>>i))
{
SDA=1;
}
else
{
SDA=0;
}
Delay_uS();
SCL=1;
Delay_uS();
SCL=0;
}
}
bit Read_ACK()
{
bit ack=0;
SCL=0;
SDA=1;
SCL=1;
Delay_uS();
if(SDA==1)
{
ack=1;
}
SCL=0;
return(ack);
}
void Write_ACK()
{
SCL=0;
SDA=0;
Delay_uS();
SCL=1;
Delay_uS();
SCL=0;
}
void Write_NACK()
{
SCL=0;
SDA=1;
Delay_uS();
SCL=1;
Delay_uS();
SCL=0;
}
void Delay_uS()
{
;
}
void Clear_Screen()
{
unsigned char Page,Col,Start_Line=0,Start_Col=0;
Start_Line_Set(Start_Line);
for(Page=0;Page<8;Page++)
{
Addr_Page_Set(Page);
Addr_Col_Set(Start_Col);
for(Col=0;Col<64;Col++)
{
WDat(0xFF);
}
}
}
void Start_Line_Set(unsigned char Line)
{
Wcom(0xC0|Line);
}
void Addr_Page_Set(unsigned char Page)
{
Wcom(0xB8|Page);
}
void Addr_Col_Set(unsigned char Col)
{
Wcom(0x40|Col);
}
void Display_Texts(unsigned char *text_code,unsigned char page,unsigned char col,unsigned char num)
{
unsigned char i,j,k;
unsigned char *ptr=text_code;
Start_Line_Set(0);
for(i=0;i<num;i++)
{
if(i<4)
{
CS1=1;
CS2=0;
}
else
{
CS1=0;
CS2=1;
}
for(j=0;j<2;j++)
{
Addr_Page_Set(page+j);
Addr_Col_Set(col+16*i);
for(k=0;k<16;k++)
{
WDat(*ptr);
ptr++;
}
}
}
}
void Wcom(unsigned char com)
{
RS=0;
RW=0;
E=1;
Delay(500);
P2=com;
E=0;
}
void WDat(unsigned char Dat)
{
RS=1;
RW=0;
E=1;
Delay(500);
P2=Dat;
E=0;
}
void Delay(unsigned long t)
{
while(t--);
}
不管我怎样调整内部写周期的时间,结果就是和照片上的情形一样。
网上找不到什么免费的资料来看,手上也没有什么书籍可以借鉴,也是上这里来向有成功经验的网友指点,望各位不吝指教!谢谢!!!祝工作顺利!!!!!!财源滚滚!!!!!!!!阖家幸福!!!!!!!!!!
[此贴子已经被作者于2020-5-17 23:54编辑过]