AT24C01-AT24C256通用源程序(C51)
扫描二维码
随时随地手机看文章
据说是AT24C01-AT24C256通用源程序,但按照我的经验,这个源程序仅适合AT24C04-AT24C256。
/*Sample:
/*main(){...
/*ReadMem(l,x,y);/*读。l为本次访问的器件号,在宏定义时分配不同芯片的不同
器件号,
x为本次欲读出的字节数;y为本次读数的起始地址*/
/*...
/*WriteMem(l,x,y);/*写。l为本次访问的器件号,在宏定义时分配不同芯片不同
的器件号,
x为本次欲写入的字节数;y为本次写数的起始地址,
注:当心页写入的地址规律,即页地址在同一页内滚动;否则会出现页溢出*/
/*}*/
#defineWait_1us{_nop_();_nop_();}
#defineWait_2us{Wait_1us;Wait_1us;}
#defineSla_Wr0xa0/*00000000*/
#defineSla_Rd0xa1/*00000001*/
#defineAT24C010x01
#defineAT24C01A0x02
#defineAT24C020x03
#defineAT24C040x04
#defineAT24C080x05
#defineAT24C160x06
#defineAT24C320x07
#defineAT24C640x08
#defineAT24C1280x09
#defineAT24C2560x0a
#defineAT24C5120x0b
#defineMem_SizeAT24C256//01/01A/02/04/08/16/32/64/128/256/512
#defineChip_10x00//本器件的器件地址,可以第一8个不同地址的芯片使之能用同一段程序进行读写
sbitbSda=P3^7;
sbitbScl=P3^6;
bitbScan_Err;
unsignedcharidatacShare_Buf[8]={0,1,2,3,4,5,6,7};//I2creadandwritebuffer
voidStart_I2c(void);
voidEnd_I2c(void);
unsignedcharScan_I2c();
voidSend_I2c(unsignedcharcData);
voidSend_Ack(void);
voidSend_Not_Ack(void);
voidWrite_Mem(unsignedcharcMem_Size,cChips_Address,cData_Size,unsignedint
cStart_Address);
voidRead_Mem(unsignedcharcMem_Size,cChips_Address,cData_Size,unsignedint
cStart_Address);
/************Name:ScanI2c***********
Function:read1bytefrom24C01/512
NO.:1
Runtime:79us1=4+2us0=5+1usf=111kbps/83.3Kbps
**********FunctionStart************/
voidStart_I2c(void)
{
unsignedchar/*idata*/i;
bSda=bScl=1;
for(i=0;i<2;i++);//wait7us
bSda=0;
for(i=0;i<2;i++);//wait7us
bScl=0;
}
/**********FunctionEnd*************/
/************Name:ScanI2c***********
Function:read1bytefrom24C01/512
NO.:1
Runtime:79us1=4+2us0=5+1usf=111kbps/83.3Kbps
**********FunctionStart************/
voidEnd_I2c(void)
{
unsignedchar/*idata*/i;
bSda=0;
bScl=1;
for(i=0;i<2;i++);//wait7us
bSda=1;
}
/**********FunctionEnd*************/
/************Name:ScanI2c***********
Function:read1bytefrom24C01/512
NO.:1
Runtime:79us1=3/5us0=1/5usf=250/100kbps
**********FunctionStart************/
unsignedcharScan_I2c()
{
unsignedchar/*idata*/i,j;//IICdata
bSda=1;
j=0;
for(i=0;i<8;i++)
{
bScl=1;
j<<=1;
Wait_2us;
j|=bSda;
//Scan0Scan1
bScl=0;
Wait_2us;
Wait_2us;
}
returnj;
}
/**********FunctionEnd*************/
/************Name:SendI2c***********
Function:Send1byteto24C01/512
NO.:2
Runtime:82us1=1/3us0=3usf=250/100kbps
**********FunctionStart************/
voidSend_I2c(unsignedcharcData)
{
unsignedchar/*idata*/i;
for(i=0;i<8;i++)
{
bSda=(bit)(cData&0x80);
bScl=1;
cData<<=1;
Wait_2us;
Wait_2us;
bScl=0;
Wait_2us;
}
bSda=bScl=1;//检测ACK
for(i=0;(bSda==1)&&(bScan_Err==0);i++)
{
if(i==30)
bScan_Err=1;//如果101us没有响应就错误
}
bScl=0;
}
/**********FunctionEnd************/
/************Name:SendAck***********
Function:Sendack
NO.:3
Runtime:10us+4us
**********FunctionStart************/
voidSend_Ack(void)
{
unsignedchar/*idata*/i;
bSda=0;
bScl=1;
for(i=0;i<2;i++);//wait8us
bScl=0;
bSda=1;
}
/**********FunctionEnd*************/
/************Name:SendNotAck***********
Function:sendnotack
NO.:4
Runtime:10us+4us
**********FunctionStart************/
voidSend_Not_Ack(void)
{
unsignedchar/*idata*/i;
bScl=bSda=1;
for(i=0;i<2;i++);//wait8us
bScl=0;
}
/**********FunctionEnd*************/
/************Name:WriteMem***********
Function:writedatato24C01/512
NO.:5
Runtime:80us(1byte)*cData_Size+
**********FunctionStart************/
voidWrite_Mem(unsignedcharcMem_Size,cChips_Address,cData_Size,unsignedintcStart_Address)
{
unsignedchar/*idata*/i,cAddress[2];//0-L;1-H
bScan_Err=0;
cAddress[0]=cStart_Address;
cStart_Address>>=8;
cAddress[1]=cStart_Address;
i=Sla_Wr;
i|=cChips_Address;
if(cMem_Size==AT24C256)
i&=0xf7;
Start_I2c();
Send_I2c(i);//SLA:1010xxx0
if(cMem_Size==AT24C256)//1-16用1个字节地址,32-512用2个字节地址,匹配地址AT24C32、64
Send_I2c(cAddress[1]);//先发送高字节地址
Send_I2c(cAddress[0]);//如果是双字节地址则为低地址否则为高地址
for(i=0;(bScan_Err==0)&&(i!=cData_Size);i++)
Send_I2c(cShare_Buf[i]);//写一个字节数据
End_I2c();
}
/**********FunctionEnd*************/
/************Name:ReadMem***********
Function:Readdatafrom24C01/512
NO.:6
Runtime:80us(1byte)*cData_Size+
**********FunctionStart************/
voidRead_Mem(unsignedcharcMem_Size,cChips_Address,cData_Size,unsignedintcStart_Address)
{
unsignedchar/*idata*/i,cAddress[2];
bScan_Err=0;