基于AVR單片機的AT24C01-512eeprom讀寫程序
針對AT24Cxx系列eeprom存儲器,寫的時候有越頁功能,不用考慮頁邊界,I2C用軟件模擬實現(xiàn),完善中…#define SDA1() PORTC|=1<
void WriteStart(void)
{
SDA1();
Delay(e2ptime);//該函數(shù)為軟件延時函數(shù),根據(jù)需要自己去寫吧
SCL1();
Delay(e2ptime);
SDA0();
Delay(e2ptime);
SCL0();
}
//輸出ACK
void WACK(unsigned char a)
{
SCL0();
Delay(e2ptime);
SDA0();//
if(a)SDA1();
Delay(e2ptime);
SCL1();
Delay(e2ptime);
SCL0();
}
//讀ACK
unsigned char RACK(void)
{
unsigned char temp=4;
SCL0();
Delay(e2ptime);
SDAin();//改成輸入
SDA1();//加上拉
SCL1();
Delay(e2ptime);
while(RSDA()&&temp)temp--;
SCL0();
SDAout();
if(temp)return(0);
else return(1);
}
//寫一個字節(jié)
unsigned char WriteByte(unsigned char a)
{
unsigned char i;
SCL0();
for(i=0x80;i;i>>=1)
{
Delay(e2ptime);
if(a&i)SDA1();
else SDA0();
Delay(e2ptime);
SCL1();
Delay(e2ptime);
SCL0();
Delay(e2ptime);
}
return(RACK());
}
//停止
void Stop(void)
{
SCL0();
Delay(e2ptime);
SDA0();//
Delay(e2ptime);
SCL1();
Delay(e2ptime);
SDA1();
// Delay(e2ptime);
// SCL0();
}
//讀一個字節(jié)數(shù)據(jù)
char ReadByte(void)
{
unsigned char temp,i;
SCL0();
SDAin();
SDA1();
for(i=0,temp=0;i<8;i++)
{
temp<<=1;
SCL1();
Delay(e2ptime);
if(RSDA())temp|=1;
SCL0();
Delay(e2ptime);
}
SDAout();
return(temp);
}
/*
AT24C01 為128字節(jié)(0x00-0x7f)
AT24C02 為256字節(jié)(0x00-0xff)
AT24C04 為512字節(jié)(0x000-0x1ff)
AT24C08 為1024字節(jié)(0x000-0x3ff)
AT24C16 為2048字節(jié)(0x000-0x7ff)
AT24C64 為8192字節(jié)(0x0000-0x1fff)
AT24C128 為16384字節(jié)(0x00-0x7ff)
AT24C256 容量為32768字節(jié)(0x0000-0x7fff)
AT24C512 容量為65536字節(jié)(0x0000-0xffff)
*/
//寫入數(shù)據(jù),器件型號model,器件地址DevAddr,開始地址addr,數(shù)據(jù)*P_data,數(shù)據(jù)長度num
void AT24CxxWrite(unsigned int model,unsigned char DevAddr,unsigned int addr,unsigned char *P_data,unsigned char num)
{
unsigned char i,temp,*p,ICerror=0,page_size;
unsigned long next_page_start_addr;
if(model<2)page_size=8;
else if(model<32)page_size=16;
else if(model<128)page_size=32;
else if(model<512)page_size=64;
else page_size=128;
next_page_start_addr=model;
next_page_start_addr<<=7;//總?cè)萘?br />while(next_page_start_addr)
{
next_page_start_addr-=page_size;
if(next_page_start_addr<=addr){next_page_start_addr+=page_size;break;}
}
Wagain:
p=P_data;
i=num;//數(shù)據(jù)個數(shù)
if(ICerror<10)ICerror++;
else goto Wstop;
SDAout();
WriteStart();//啟動
temp=0xa0+(DevAddr<<1);
if(model<16) temp+=((addr>>7)&0x0e);//器件24c32以下地址高位
if(WriteByte(temp))//頭字節(jié)
goto Wagain;
if(model>16)//寫器件24c32以上地址高位
{
if(WriteByte(addr>>8))//
goto Wagain;
}
if(WriteByte(addr&0xff))//寫地址低位
goto Wagain;
while(i)//寫入數(shù)據(jù)i個
{
i--;
if(WriteByte(*(p++)))goto Wagain;
addr++;
if(addr==next_page_start_addr)break;
}
Wstop:
Stop();
if(i)//寫到了頁末,重新開始寫新一頁
{
Delay(100);
num=i;
P_data=p;
next_page_start_addr+=page_size;
if(addr==model*128){addr=0;next_page_start_addr=page_size;}
goto Wagain;
}
}
//讀數(shù)據(jù),器件型號model,器件地址DevAddr,地址addr,存放到 *P_data,數(shù)據(jù)長度num
unsigned char AT24CxxRead(unsigned int model,unsigned char DevAddr,unsigned int addr,unsigned char *P_data,unsigned char num)
{
unsigned char temp,*p,ICerror=0;
Ragain:
if(ICerror<10)ICerror++;
else goto Rstop;
SDAout();
p=P_data;
WriteStart();//
temp=0xa0+(DevAddr<<1);
if(model<16) temp+=((addr>>7)&0x0e);//器件24c32以下地址高位
if(WriteByte(temp))//;//頭字節(jié)
goto Ragain;
if(model>16)//器件24c32以上地址高位
{
if(WriteByte(addr>>8))//
goto Ragain;
}
if(WriteByte(addr&0xff))//
goto Ragain;
Ragain2:
WriteStart();//
if(WriteByte(temp|1))//
goto Ragain2;
while(num--)
{
*(p++)=ReadByte();//
if(num)WACK(0);
else WACK(1);
}
Rstop:
Stop();//
return(ICerror);
}main(){unsigned char card[20],addr,temp;……AT24CxxWrite(256,3,addr,&Card[0],12);//at24c256,a1,a0引腳都的接高電平,片內(nèi)地址為addr為起始地址,從數(shù)組card[]中取12字節(jié)數(shù)據(jù)存到存儲器中
temp=AT24CxxRead(256,3,addr,&Card[0],12);//at24c256,a1,a0引腳都的接高電平,片內(nèi)地址為addr為起始地址,從存儲器中取數(shù)到數(shù)組card[]中。返回讀操作是否出錯信息到temp。