avr eeprom保護(hù)方法
編譯環(huán)境:WinAVR-20060421
基本思路:每份寫到EEPRM的數(shù)據(jù),都做三個(gè)備份,每個(gè)備份的數(shù)據(jù)都做CRC16校驗(yàn),只要系統(tǒng)運(yùn)行中出錯(cuò),錯(cuò)誤地修改了EEPROM數(shù)據(jù),
EEPROMSave.h
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
EEPROMSave.c
unsigned
{
unsigned
data
eeprom_busy_wait();
data
return
}
uint16_t
{
uint16_t
data
eeprom_busy_wait();
data
return
}
void
{
eeprom_busy_wait();
eeprom_write_byte(address,data);
}
void
{
eeprom_busy_wait();
eeprom_write_word(address,data);
}
void
{
unsigned
for
{
EepromWriteByte((unsigned
buff++;
}
}
unsigned
{
unsigned
unsigned
crc
ref_crc
for
{
crc
for
{
if
{
crc
}
else
{
crc
}
}
pdata
}
ref_crc
ref_crc
pdata
ref_crc
if
{
return
}
else
{
return
}
}
unsigned
{
unsigned
unsigned
crc
for
{
crc
for
{
if
{
crc
}
else
{
crc
}
}
pdata
}
EepromWriteByte(pdata,(uint8_t)
pdata
EepromWriteByte(pdata,(uint8_t)
pdata
if
{
return
}
else
{
return
}
}
uint8_t
{
if
{
return
}
return
}
uint8_t
{
unsigned
unsigned
unsigned
unsigned
unsigned
invalidpage[0]
invalidpage[1]
invalidpage[2]
validpage
temp
page
if
{
validpage
}
else
{
invalidpage[page]
page
}
if
{
validpage
}
else
{
invalidpage[page]
page
}
if
{
validpage
}
else
{
invalidpage[page]
page
}
if
{
return
}
while
{
for
{
temp
EepromWriteByte((uint8_t
}
}
if
{
return
}
return
}
使用方法(三個(gè)備份):
1、定義一個(gè)數(shù)組:EEPROMData[EepromPageSize-2]
2、要保存數(shù)據(jù)時(shí),先把數(shù)據(jù)放到數(shù)組中,然后調(diào)用EepromWriteBlock()函數(shù),把這個(gè)數(shù)組的數(shù)據(jù)寫進(jìn)EEPROM,三個(gè)備份要寫三次。
3、寫完了之后,調(diào)用CheckWriteCRC()函數(shù),該函數(shù)會(huì)計(jì)算出當(dāng)前備份的CRC16檢驗(yàn)數(shù)據(jù)并寫到EEPROM備份的尾部,有多少個(gè)備份就要調(diào)用多少次。
4、至此,數(shù)據(jù)的備份工作已經(jīng)完成。
5、校驗(yàn)數(shù)據(jù)(一般在復(fù)位后運(yùn)行),執(zhí)行CheckAllPage()函數(shù),若通過(guò)了,則EEPROM數(shù)據(jù)沒(méi)有問(wèn)題,否則要運(yùn)行DataRecover()函數(shù),對(duì)損壞的備份進(jìn)行修復(fù)
------------------修改原因:修改變量的定義形式
評(píng)論