新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 治標(biāo)治本,徹底解決AVR單片機(jī)EEPROM數(shù)據(jù)丟失問(wèn)題

治標(biāo)治本,徹底解決AVR單片機(jī)EEPROM數(shù)據(jù)丟失問(wèn)題

作者: 時(shí)間:2016-12-01 來(lái)源:網(wǎng)絡(luò) 收藏
編譯環(huán)境:WinAVR-20060421 + AVR Studio 4.12.498 Service Pack 4
基本思路:每份寫(xiě)到EEPRM的數(shù)據(jù),都做三個(gè)備份,每個(gè)備份的數(shù)據(jù)都做CRC16校驗(yàn),只要系統(tǒng)運(yùn)行中出錯(cuò),錯(cuò)誤地修改了EEPROM數(shù)據(jù),
那么根據(jù)校驗(yàn)字節(jié)就知道哪個(gè)備份的數(shù)據(jù)被修改了,然后用正確的備份覆蓋出錯(cuò)的備份,達(dá)到數(shù)據(jù)恢復(fù)的目的。
EEPROMSave.h 文件:
/* EEPROM管理定義 */
#define EepromPageSize 64 //頁(yè)容量定義
#define EepromPage0Addr 0x0000 //各個(gè)頁(yè)的其始地址定義
#define EepromPage1Addr (EepromPage0Addr + EepromPageSize)
#define EepromPage2Addr (EepromPage1Addr + EepromPageSize)
#define EepromPage3Addr (EepromPage2Addr + EepromPageSize)
#define EepromPage4Addr (EepromPage3Addr + EepromPageSize)
#define EepromPage5Addr (EepromPage4Addr + EepromPageSize)
#define EepromPage6Addr (EepromPage5Addr + EepromPageSize)
#define EepromPage7Addr (EepromPage6Addr + EepromPageSize)
/*
最后兩個(gè)字節(jié)為CRC16校驗(yàn)碼,其余為數(shù)據(jù)
| 0 | 1 | 2 | |.......................| 61 | 62 | 63 |
Data Data...................Data.....CRCH CRCL
*/
#define VALID 0x01
#define INVALID 0x00
/*-----------------------------------------------------------------------------------------*/
EEPROMSave.c 文件:
/*******************************************************************
*函數(shù)名稱(chēng):EepromReadByte()
*函數(shù)功能:寫(xiě)一個(gè)Byte的數(shù)據(jù)進(jìn)EEPROM
*輸入?yún)?shù):address:地址
*返回參數(shù):從指定地址讀出來(lái)的數(shù)據(jù)
*編寫(xiě)作者:my_avr
*編寫(xiě)時(shí)間:2007年8月13日
*相關(guān)說(shuō)明:
********************************************************************/
unsigned char EepromReadByte(unsigned char *address)
{
unsigned char data;
data = 0;
eeprom_busy_wait();
data = eeprom_read_byte(address);
return data;
}
/*******************************************************************
*函數(shù)名稱(chēng):EepromReadWord();
*函數(shù)功能:寫(xiě)一個(gè)Word的數(shù)據(jù)進(jìn)EEPROM
*輸入?yún)?shù):address:地址
*返回參數(shù):從指定地址讀出來(lái)的數(shù)據(jù)
*編寫(xiě)作者:my_avr
*編寫(xiě)時(shí)間:2007年8月13日
*相關(guān)說(shuō)明:
********************************************************************/
uint16_t EepromReadWord(uint16_t *address)
{
uint16_t data;
data = 0;
eeprom_busy_wait();
data = eeprom_read_word(address);
return data;
}
/*******************************************************************
*函數(shù)名稱(chēng):EepromWriteByte()
*函數(shù)功能:寫(xiě)一個(gè)Byte的數(shù)據(jù)進(jìn)EEPROM
*輸入?yún)?shù):address:地址;data:數(shù)據(jù)
*返回參數(shù):無(wú)
*編寫(xiě)作者:my_avr
*編寫(xiě)時(shí)間:2007年8月13日
*相關(guān)說(shuō)明:
********************************************************************/
void EepromWriteByte(unsigned char *address,unsigned char data)
{
eeprom_busy_wait();
eeprom_write_byte(address,data);
}
/*******************************************************************
*函數(shù)名稱(chēng):EepromWriteWord()
*函數(shù)功能:寫(xiě)一個(gè)Word的數(shù)據(jù)進(jìn)EEPROM
*輸入?yún)?shù):address:地址;data:數(shù)據(jù)
*返回參數(shù):
*編寫(xiě)作者:my_avr
*編寫(xiě)時(shí)間:2007年8月13日
*相關(guān)說(shuō)明:
********************************************************************/
void EepromWriteWord(unsigned int *address,unsigned int data)
{
eeprom_busy_wait();
eeprom_write_word(address,data);
}

上一頁(yè) 1 2 3 4 5 6 7 下一頁(yè)

評(píng)論


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

關(guān)閉