基于AVR单片机的AT24C01-512eeprom读写程序
针对AT24Cxx系列eeprom存储器,写的时候有越页功能,不用考虑页边界,I2C用软件模拟实现,完善中…#define SDA1() PORTC|=1<#define SDA0() PORTC&=~(1<#define SDAout() DDRC|=1<#define SDAin() DDRC&=~(1<【基于AVR单片机的AT24C01-512eeprom读写程序】#define RSDA() PINC&(1<#define SCL1() PORTC|=1<#define SCL0() PORTC&=~(1<#define e2ptime 20//起始
void WriteStart(void)
{
SDA1();
Delay(e2ptime);//该函数为软件延时函数,根据需要自己去写吧
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);
}
//写一个字节
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();
}
//读一个字节数据
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字节(0x00-0x7f)
AT24C02 为256字节(0x00-0xff)
AT24C04 为512字节(0x000-0x1ff)
AT24C08 为1024字节(0x000-0x3ff)
AT24C16 为2048字节(0x000-0x7ff)
AT24C32 为4096字节(0x000-0xfff)
AT24C64 为8192字节(0x0000-0x1fff)
AT24C128 为16384字节(0x00-0x7ff)
AT24C256 容量为32768字节(0x0000-0x7fff)
AT24C512 容量为65536字节(0x0000-0xffff)
*/
//写入数据,器件型号model,器件地址DevAddr,开始地址addr,数据*P_data,数据长度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;//总容量
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;//数据个数
if(ICerror<10)ICerror++;
else goto Wstop;
SDAout();
WriteStart();//启动
temp=0xa0+(DevAddr<<1);
if(model<16) temp+=((addr>>7)&0x0e);//器件24c32以下地址高位
if(WriteByte(temp))//头字节
goto Wagain;
if(model>16)//写器件24c32以上地址高位
{
if(WriteByte(addr>>8))//
goto Wagain;
}
if(WriteByte(addr&0xff))//写地址低位
goto Wagain;
while(i)//写入数据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=http://www.dg8.com.cn/tool/p;
next_page_start_addr+=page_size;
if(addr==model*128){addr=0;next_page_start_addr=page_size;}
goto Wagain;
}
}
//读数据,器件型号model,器件地址DevAddr,地址addr,存放到 *P_data,数据长度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))//;//头字节
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引脚都的接高电平,片内地址为addr为起始地址,从数组card[]中取12字节数据存到存储器中
temp=AT24CxxRead(256,3,addr,&Card[0],12);//at24c256,a1,a0引脚都的接高电平,片内地址为addr为起始地址,从存储器中取数到数组card[]中 。返回读操作是否出错信息到temp 。
推荐阅读
- 51单片机之I^2C总线
- 卫生间装修设计的时尚简约
- 单片机小小常识
- WPF与51单片机之间的串口通信
- 基于51单片机的实时操作系统的实现
- 单片机解密技术大全
- 19264液晶屏与单片机驱动
- 基于IAP和Keil MDK的远程升级设计
- 51单片机学习笔记:步进电机控制,正转反转等
- 单片机程序仿真器仿真正确烧录后不能正常运行