<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > avr eeprom保護方法

avr eeprom保護方法

作者: 時(shí)間:2016-11-23 來(lái)源:網(wǎng)絡(luò ) 收藏
在項目中復制出來(lái)的程序,使用時(shí)可能有些地方需要修改。
編譯環(huán)境:WinAVR-20060421+AVRStudio4.12.498ServicePack4


基本思路:每份寫(xiě)到EEPRM的數據,都做三個(gè)備份,每個(gè)備份的數據都做CRC16校驗,只要系統運行中出錯,錯誤地修改了EEPROM數據,
那么根據校驗字節就知道哪個(gè)備份的數據被修改了,然后用正確的備份覆蓋出錯的備份,達到數據恢復的目的。


EEPROMSave.h文件:



#defineEepromPageSize 64 //頁(yè)容量定義

#defineEepromPage0Addr 0x0000 //各個(gè)頁(yè)的其始地址定義
#defineEepromPage1Addr (EepromPage0Addr+EepromPageSize)
#defineEepromPage2Addr (EepromPage1Addr+EepromPageSize)
#defineEepromPage3Addr (EepromPage2Addr+EepromPageSize)
#defineEepromPage4Addr (EepromPage3Addr+EepromPageSize)
#defineEepromPage5Addr (EepromPage4Addr+EepromPageSize)
#defineEepromPage6Addr (EepromPage5Addr+EepromPageSize)
#defineEepromPage7Addr (EepromPage6Addr+EepromPageSize)



#defineVALID 0x01
#defineINVALID 0x00



EEPROMSave.c文件:


unsignedcharEepromReadByte(unsignedchar*address)
{
unsignedchardata;

data=0;

eeprom_busy_wait();
data=eeprom_read_byte(address);

returndata;
}


uint16_tEepromReadWord(uint16_t*address)
{
uint16_tdata;

data=0;

eeprom_busy_wait();
data=eeprom_read_word(address);

returndata;
}


voidEepromWriteByte(unsignedchar*address,unsignedchardata)
{
eeprom_busy_wait();
eeprom_write_byte(address,data);
}


voidEepromWriteWord(unsignedint*address,unsignedintdata)
{
eeprom_busy_wait();
eeprom_write_word(address,data);
}


voidEepromWriteBlock(unsignedchar*buff,unsignedchar*address,unsignedcharn)
{
unsignedchari;

for(i=0;i<n;i++)
{
EepromWriteByte((unsignedchar*)(address+i),*buff);

buff++;
}
}


unsignedcharEepromCheck(unsignedchar*pdata,unsignedcharpacksize)
{
unsignedchari,j;
unsignedintcrc,ref_crc;

crc=0;
ref_crc=0;

for(i=0;i<(packsize-2);i++)
{
crc=crc^((uint16_t)EepromReadByte(pdata)<<8);

for(j=0;j<8;j++)
{
if(crc&0x8000)
{
crc=(crc<<1)^0x1021;
}
else
{
crc=crc<<1;
}
}

pdata++;
}

ref_crc=(uint16_t)EepromReadByte(pdata);
ref_crc=ref_crc<<8;
pdata++;
ref_crc|=(uint16_t)EepromReadByte(pdata);

if(crc==ref_crc)
{
returnVALID;
}
else
{
returnINVALID;
}
}


unsignedcharCheckWriteCRC(unsignedchar*pdata,unsignedcharpacksize)
{
unsignedchari,j;
unsignedintcrc;

crc=0;

for(i=0;i<(packsize-2);i++)
{
crc=crc^((uint16_t)EepromReadByte(pdata)<<8);

for(j=0;j<8;j++)
{
if(crc&0x8000)
{
crc=(crc<<1)^0x1021;
}
else
{
crc=crc<<1;
}
}

pdata++;
}

EepromWriteByte(pdata,(uint8_t)(crc>>8));
pdata++;
EepromWriteByte(pdata,(uint8_t)crc);
pdata++;

if(EepromCheck((pdata-packsize),packsize))
{
returnVALID;
}
else
{
returnINVALID;
}
}


uint8_tCheckAllPage(void)
{
if((EepromCheck((unsignedchar*)EepromPage1Add,EepromPageSize)==VALID)
&&(EepromCheck((unsignedchar*)EepromPage2Add,EepromPageSize)==VALID)
&&(EepromCheck((unsignedchar*)EepromPage3Add,EepromPageSize)==VALID))
{
returnVALID;
}

returnINVALID;
}


uint8_tDataRecover(void)
{
unsignedchari;
unsignedchartemp;
unsignedcharpage;
unsignedintinvalidpage[3];
unsignedintvalidpage;

invalidpage[0]=0;
invalidpage[1]=0;
invalidpage[2]=0;
validpage=0;
temp=0;
page=0;

if(EepromCheck((uint8_t*)EepromPage1Add,EepromPageSize)==VALID)
{
validpage=EepromPage1Add;
}
else
{
invalidpage[page]=EepromPage1Add;
page++;
}

if(EepromCheck((uint8_t*)EepromPage2Add,EepromPageSize)==VALID)
{
validpage=EepromPage2Add;
}
else
{
invalidpage[page]=EepromPage2Add;
page++;
}

if(EepromCheck((uint8_t*)EepromPage3Add,EepromPageSize)==VALID)
{
validpage=EepromPage3Add;
}
else
{
invalidpage[page]=EepromPage3Add;
page++;
}

if(page==3) //三個(gè)備份都被破壞了
{
returnINVALID; //數據完全無(wú)效了
}

while((page--)>0) //數據恢復
{
for(i=0;i<EepromPageSize;i++)
{
temp=EepromReadByte((uint8_t*)(validpage+i));
EepromWriteByte((uint8_t*)(invalidpage[page]+i),temp);
}
}

if(CheckAllPage()==VALID)
{
returnVALID;
}

returnINVALID;
}



使用方法(三個(gè)備份):
1、定義一個(gè)數組:EEPROMData[EepromPageSize-2],數組定義為EepromPageSize-2是為了給每個(gè)備份留2個(gè)字節的校驗
2、要保存數據時(shí),先把數據放到數組中,然后調用EepromWriteBlock()函數,把這個(gè)數組的數據寫(xiě)進(jìn)EEPROM,三個(gè)備份要寫(xiě)三次。
3、寫(xiě)完了之后,調用CheckWriteCRC()函數,該函數會(huì )計算出當前備份的CRC16檢驗數據并寫(xiě)到EEPROM備份的尾部,有多少個(gè)備份就要調用多少次。
4、至此,數據的備份工作已經(jīng)完成。

5、校驗數據(一般在復位后運行),執行CheckAllPage()函數,若通過(guò)了,則EEPROM數據沒(méi)有問(wèn)題,否則要運行DataRecover()函數,對損壞的備份進(jìn)行修復






------------------修改原因:修改變量的定義形式


關(guān)鍵詞: avreeprom保護方

評論


技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>