AT24C01-AT24C256通用源程序(C51)
據(jù)說是AT24C01-AT24C256通用源程序,但按照我的經(jīng)驗(yàn),這個(gè)源程序僅適合AT24C04-AT24C256。
/*Sample:
/*main(){...
/*ReadMem(l,x,y);/*讀。l為本次訪問的器件號,在宏定義時(shí)分配不同芯片的不同
器件號,
x為本次欲讀出的字節(jié)數(shù);y為本次讀數(shù)的起始地址*/
/*...
/*WriteMem(l,x,y);/*寫。l為本次訪問的器件號,在宏定義時(shí)分配不同芯片不同
的器件號,
x為本次欲寫入的字節(jié)數(shù);y為本次寫數(shù)的起始地址,
注:當(dāng)心頁寫入的地址規(guī)律,即頁地址在同一頁內(nèi)滾動(dòng);否則會(huì)出現(xiàn)頁溢出*/
/*}*/
#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個(gè)不同地址的芯片使之能用同一段程序進(jìn)行讀寫
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沒有響應(yīng)就錯(cuò)誤
}
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個(gè)字節(jié)地址,32-512用2個(gè)字節(jié)地址,匹配地址AT24C32、64
Send_I2c(cAddress[1]);//先發(fā)送高字節(jié)地址
Send_I2c(cAddress[0]);//如果是雙字節(jié)地址則為低地址否則為高地址
for(i=0;(bScan_Err==0)&&(i!=cData_Size);i++)
Send_I2c(cShare_Buf[i]);//寫一個(gè)字節(jié)數(shù)據(jù)
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;