您的位置 首页 编程

avr eeprom维护办法

在项目中复制出来的程序,使用时可能有些地方需要修改。编译环境:WinAVR-20060421+AVRStudio412498ServicePack4基本思路:每份写到EEPRM

在项目中仿制出来的程序,使用时或许有些当地需求修正。
编译环境:WinAVR-20060421+AVRStudio4.12.498ServicePack4

基本思路:每份写到EEPRM的数据,都做三个备份,每个备份的数据都做CRC16校验,只需体系运转中犯错,过错地修正了EEPROM数据,
那么依据校验字节就知道哪个备份的数据被修正了,然后用正确的备份掩盖犯错的备份,到达数据康复的意图。

EEPROMSave.h文件:

#defineEepromPageSize 64 //页容量界说

#defineEepromPage0Addr 0x0000 //各个页的其始地址界说
#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<(packsize2);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<(packsize2);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((pdatapacksize),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) //三个备份都被破坏了
{
returnINVALID; //数据彻底无效了
}

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;
}

使用方法(三个备份):
1、界说一个数组:EEPROMData[EepromPageSize-2],数组界说为EepromPageSize-2是为了给每个备份留2个字节的校验
2、要保存数据时,先把数据放到数组中,然后调用EepromWriteBlock()函数,把这个数组的数据写进EEPROM,三个备份要写三次。
3、写完了之后,调用CheckWriteCRC()函数,该函数管帐算出当时备份的CRC16查验数据并写到EEPROM备份的尾部,有多少个备份就要调用多少次。
4、至此,数据的备份作业现已完结。

5、校验数据(一般在复位后运转),履行CheckAllPage()函数,若通过了,则EEPROM数据没有问题,否则要运转DataRecover()函数,对损坏的备份进行修正

——————修正原因:修正变量的界说方式

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/biancheng/261657.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部