AT24C128 EEPROM的读写
扫描二维码
随时随地手机看文章
记录AT24C128 eeprom的多字节读写
#define_EEPROM_DEBUG_LEVEL0
#if_EEPROM_DEBUG_LEVEL==1
#defineEEPROM_DEBUG(fmt,args...)do{
printf(fmt,##args);
}while(0)
#elif_EEPROM_DEBUG_LEVEL==2
#defineEEPROM_DEBUG(fmt,args...)do{
printf("%s:line:%d->",__FUNCTION__,__LINE__);
printf(fmt,##args);
}while(0)
#elif_EEPROM_DEBUG_LEVEL==0
#defineEEPROM_DEBUG(fmt,args...)
#else
#defineEEPROM_DEBUG(fmt,args...)
#endif
[cpp]view plaincopy
#defineAT24C128_PAGE_SIZE64
#defineAT24C128_PAGE_COUNT256
#defineAT24C128_BYTE_SIZE16*1024
#defineAT24C128_WADDR0xA0
#defineAT24C128_RADDR0xA1
写时序:
///
////wrtieeeprombypage
////
intwriteE2promByPage(u32startaddr,void*data,u32len){
intretry=0,pageCnt=0,i=0,j=0,k=0;
u32temp1=0,temp2=0,temp3=0,start=0,startPage;
u8*pBuf;
pBuf=(u8*)data;
temp1=startaddr%AT24C128_PAGE_SIZE;
temp2=len%AT24C128_PAGE_SIZE;
temp3=len/AT24C128_PAGE_SIZE;
startPage=startaddr/AT24C128_PAGE_SIZE;
if(temp1==0){
if(temp2!=0)pageCnt=temp3+2;
elsepageCnt=temp3+1;
}
else
{
if(temp2!=0)pageCnt=temp3+3;
elsepageCnt=temp3+2;
}
if(pageCnt>AT24C128_PAGE_COUNT)return-1;
EEPROM_DEBUG("pageCnt%drn",pageCnt);
for(i=0;i //startsignal!!! while(SCCB_Start()==0){ if(retry>100){ EEPROM_DEBUG("startfailrn"); return-1; } retry++; } if(i==0){ start=startaddr; temp1=AT24C128_PAGE_SIZE-startaddr; } else{ start=(startPage+i)*AT24C128_PAGE_SIZE; temp1=AT24C128_PAGE_SIZE; } EEPROM_DEBUG("start:%drn",start); EEPROM_DEBUG("temp1:%drn",temp1); //writeslaveaddr SCCB_writeByte(AT24C128_WADDR); if(SCCB_Wait_Ack()==0){ EEPROM_DEBUG("writeaddrfailrn"); SCCB_Stop(); goto_end; } //writepagehighaddr SCCB_writeByte(start/256); if(SCCB_Wait_Ack()==0){ EEPROM_DEBUG("writehighpageaddrfailrn"); SCCB_Stop(); goto_end; } //writepagelowaddr SCCB_writeByte(start%256); if(SCCB_Wait_Ack()==0){ EEPROM_DEBUG("writelowpageaddrfailrn"); SCCB_Stop(); goto_end; } //writedate for(j=0;j SCCB_writeByte(pBuf[k]); //writeoperationneedslaveresponseack if(SCCB_Wait_Ack()==0){ EEPROM_DEBUG("writedatafailrn"); SCCB_Stop(); goto_end; } else{ k++; } if(k==len){ SCCB_Stop(); goto_end; } } EEPROM_DEBUG("page%dwritesuccessrn",i); //stop SCCB_Stop(); SCCB_Delay(SCL_TIME_US); } _end: return k;