eeprom读写程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include "EEPROM.h"
unsigned char EEPROM_Read(unsigned int uiAddress)
{
while(EECR & (1 << EEWE));
EEAR= uiAddress;
EECR |= (1 << EERE);
return EEDR;
}
unsigned char EEPROM_Read2(unsigned char uiAddress2,unsigned char uiAddress1)
{
while(EECR & (1 << EEWE));
EEARL = uiAddress1;
EEARH=uiAddress2;
EECR |= (1 << EERE);
return EEDR;
}
//==========================================
//写数据到E2P中设定的地址空间中
//
//==========================================
void EEPROM_Write(unsigned int uiAddress, unsigned char ucData)
{
while(EECR & (1 << EEWE));
EEAR = uiAddress;
EEDR = ucData;
EECR |= (1 << EEMWE);
EECR |= (1 << EEWE);
}
//=========================================
//循环往EEP中写数据
//需开辟11*10+4个存储区
//高4位固定地址,十位和个位为循环存储
//-11*10存储区的分配为十位在每11个存储区的第一个位置
//个位为每11个存储区的后10中的循环存储位置,
// 具体位置有十位的数值决定
//=========================================
void WriteTestCounterToEEP(unsigned long XCounter,unsigned int EEPAdr)
{
unsigned char ReadData[6];
unsigned long DataTemp;
unsigned char i;
unsigned char EEPAdrL,EEPAdrH;
EEPAdrH=((EEPAdr>>8) & (unsigned int)0xFF);
EEPAdrL=(EEPAdr & (unsigned int)0xFF);
DataTemp=XCounter;
for(i=0;i<6;i++)
ReadData[i]=0;
if(DataTemp>=1000000)
DataTemp=0;
ReadData[0]=DataTemp/100000;
DataTemp=DataTemp%100000;
ReadData[1]=DataTemp/10000;
DataTemp=DataTemp%10000;
ReadData[2]=DataTemp/1000;
DataTemp=DataTemp%1000;
ReadData[3]=DataTemp/100;
DataTemp=DataTemp%100;
ReadData[4]=DataTemp/10;
ReadData[5]=DataTemp%10;
if(ReadData[0]!=EEPROM_Read2(EEPAdrH,0x0C))
EEPROM_Write(EEPAdr+0x0C, ReadData[0]);
if(ReadData[1]!=EEPROM_Read2(EEPAdrH,0x0D))
EEPROM_Write(EEPAdr+0x0D, ReadData[1]);
if(ReadData[2]!=EEPROM_Read2(EEPAdrH,0x0E))
EEPROM_Write(EEPAdr+0x0E, ReadData[2]);
if(ReadData[3]!=EEPROM_Read2(EEPAdrH,0x0F))
EEPROM_Write(EEPAdr+0x0F, ReadData[3]);
if(ReadData[4]!=EEPROM_Read2(EEPAdrH,0x10+ ReadData[3]*11))
EEPROM_Write(EEPAdr+0x10+ ReadData[3]*11,ReadData[4]);
EEPROM_Write(EEPAdr+0x10+ ReadData[4]+1+ReadData[3]*11,ReadData[5]);
}
unsigned long ReadTestCounter(unsigned char AddrH)
{
unsigned long TR_APDU_Buffer[6];
unsigned char i;
unsigned long TestCoun=0;
TR_APDU_Buffer[0]=EEPROM_Read2(AddrH,0x0C);
TR_APDU_Buffer[1]=EEPROM_Read2(AddrH,0x0D);
TR_APDU_Buffer[2]=EEPROM_Read2(AddrH,0x0E);
TR_APDU_Buffer[3]=EEPROM_Read2(AddrH,0x0F);
TR_APDU_Buffer[4]=EEPROM_Read2(AddrH,0x10+TR_APDU_Buffer[3]*11);
TR_APDU_Buffer[5]=EEPROM_Read2(AddrH,0x10+TR_APDU_Buffer[3]*11+TR_APDU_Buffer[4]+1);
if(TR_APDU_Buffer[0]==0xFF)
{
for(i=0;i<6;i++) TR_APDU_Buffer[i]=0x00;
}
for(i=0;i<6;i++)
{
TestCoun= TestCoun*10+TR_APDU_Buffer[i];
}
//TestCoun=(TR_APDU_Buffer[0]*100000+TR_APDU_Buffer[1]*10000+TR_AP
DU_Buffer[2]*1000
//+TR_APDU_Buffer[3]*100+TR_APDU_Buffer[4]*10+TR_APDU_Buffer[5]);
return TestCoun;
}
/********************************************************************/
/********************************************************************/
//#include
#include
//#include
//#include
//#define uchar unsigned char
//#define uint unsigned int
//#define ulong unsigned long
unsigned char EEPROM_Read(unsigned int uiAddress);
unsigned char EEPROM_Read2(unsigned char uiAddress2,unsigned char uiAddress1);
void EEPROM_Write(unsigned int uiAddress, unsigned char ucData);
void WriteTestCounterToEEP(unsigned long XCounter,unsigned int EEPAdr);
unsigned long ReadTestCounter(unsigned char AddrH);