治標治本,徹底解決AVR微控制器EEPROM資料丟失問題
阿新 • • 發佈:2019-01-06
/*******************************************************************
*函式名稱:unsigned char DataRecover(void)
*函式功能:檢查EEPROM資料是否被破壞,如果被破壞了,作資料恢復
*輸入引數:無
*返回引數:操作成功否?,成功:VALID,失敗:INVALID
*編寫作者:my_avr
*編寫時間:2007年8月21日
*相關說明:
********************************************************************/
uint8_t DataRecover(void)
{
unsigned char i;
unsigned char temp;
unsigned char page;
unsigned int invalidpage[3];
unsigned int validpage;
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) //三個備份都被破壞了
{
return INVALID; //資料完全無效了
}
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)
{
return VALID;
}
return INVALID;
}
*函式名稱:unsigned char DataRecover(void)
*函式功能:檢查EEPROM資料是否被破壞,如果被破壞了,作資料恢復
*輸入引數:無
*返回引數:操作成功否?,成功:VALID,失敗:INVALID
*編寫作者:my_avr
*編寫時間:2007年8月21日
*相關說明:
********************************************************************/
uint8_t DataRecover(void)
{
unsigned char i;
unsigned char temp;
unsigned char page;
unsigned int invalidpage[3];
unsigned int validpage;
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) //三個備份都被破壞了
{
return INVALID; //資料完全無效了
}
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)
{
return VALID;
}
return INVALID;
}