#define FLASHDATA_PAGE0 0x0000
#define FLASHDATA_PAGE1 0x0100
#define FLASHDATA_PAGE2 0x0200
#define FLASHDATA_PAGE3 0x0300
////////////////////////////
#define CONFIGADDRESS FLASHDATA_PAGE0 //eeprom 0页地址
#define CONFIGCOPYADDRESS FLASHDATA_PAGE1 //eeprom 1页地址
////IB_CON1的定义
#define ERASEPAGE 0xE6
#define WRITEPAGE 0x6E
static unsigned char ssp_flag;
//////擦除指定地址t_addr扇区的类容
void SspErase(unsigned int t_addr)
{
EA = 0;
XPAGE = (t_addr>>8) & 0x07;
FLASHCON=1;
if (ssp_flag==0x55)
{
IB_CON1 = ERASEPAGE;
IB_CON2 = 0x05;
IB_CON3 = 0x0A;
IB_CON4 = 0x09;
IB_CON5 = 0x06;
_nop_();
_nop_();
_nop_();
_nop_();
}
else
{
IB_CON2 = 0x00;
}
ssp_flag = 0;
FLASHCON=0;
XPAGE=0;
EA = 1;
}
///////////////////////////////////
void SspWrite(unsigned int addr,unsigned char dat)
{
EA=0;
IB_DATA = dat;
IB_OFFSET = addr&0xff;
XPAGE = ((addr>>8) & 7);
FLASHCON=1;
if(ssp_flag==0x55)
{
IB_CON1 = 0x6E;
IB_CON2 = 0x05;
IB_CON3 = 0x0A;
IB_CON4 = 0x09;
IB_CON5 = 0x06;
_nop_();
_nop_();
_nop_();
_nop_();
}
else
{
IB_CON2=0;
}
ssp_flag=0;
FLASHCON=0;
XPAGE=0;
EA = CY;
}
unsigned char SspRead(unsigned int addr)
{
unsigned char temp;
EA = 0;
FLASHCON=1;
temp=*(unsigned char code *)(addr);
FLASHCON=0;
EA = 1;
return(temp);
}
///////写num个字节的数据到扇区t_addr
void WriteEeprom(unsigned int t_addr,unsigned char num,unsigned char *ptr)
{
unsigned char loop;
ssp_flag = 0x55;
SspErase(t_addr);
for (loop=0; loop<num; loop++)
{
ssp_flag = 0x55;
SspWrite(t_addr, *ptr);
t_addr++;
ptr++;
}
}
///////从扇区地址t_addr内读出数据送缓冲区
void ReadEeprom(unsigned int t_addr,unsigned char num,unsigned char *ptr)
{
unsigned int loop;
for (loop=0; loop<num; loop++)
{
*ptr = SspRead(t_addr);
ptr++;
t_addr++;
}
}
////////////////////////
////返回1表示当前存储的数据出现错误,否则返回零
unsigned char CheckEeprom(unsigned int t_addr, unsigned int num, unsigned char xdata *ptr)
{
unsigned char da;
unsigned char res = 0;
unsigned int loop;
for (loop=0; loop<num; loop++)
{
da = SspRead(t_addr);
if (da != *ptr)
{
res = 1;
break;
}
t_addr++;
ptr++;
}
return(res);
}
////////////////////////
void WriteConfig(void)
{
unsigned char i;
/// unsigned char chksum;
unsigned char xdata *ptr;
config_data.flag_frist=0x55;
config_data.checksum = 0;
ptr = (unsigned char xdata*)&config_data;
for (i=0; i<(sizeof(config_data)-1); i++)
{
config_data.checksum += *(ptr+i);
}
////////
do
{////写原始页的数据
WriteEeprom(CONFIGADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data);
}while(CheckEeprom(CONFIGADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data));
do
{///写备份页的数据
WriteEeprom(CONFIGCOPYADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data);
}while(CheckEeprom(CONFIGCOPYADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data));
}
//返回1表示读取的配置不正确
//返回0表示读取的配置正确
unsigned char ReadConfig(void)
{
unsigned int i;
unsigned char chksum;
unsigned char xdata *ptr;
ReadEeprom(CONFIGADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data);
chksum = 0;
ptr = (unsigned char xdata *)&config_data;
for (i=0; i<(sizeof(config_data)-1); i++)
{
chksum += *(ptr+i);
}
if ((config_data.flag_frist==0x55) && (chksum = config_data.checksum))
{/////数据正确
if (CheckEeprom(CONFIGCOPYADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data))
{///检测到备份页的数据也不正确
WriteEeprom(CONFIGCOPYADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data);
}
return(0);
}
////原始页不正确,继续判断备份页
ReadEeprom(CONFIGCOPYADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data);
chksum = 0;
ptr = (unsigned char xdata *)&config_data;
for (i=0; i<sizeof(config_data); i++)
{
chksum += *(ptr+i);
}
if ((config_data.flag_frist==0x55) && (chksum = config_data.checksum))
{/////数据正确
WriteEeprom(CONFIGADDRESS, sizeof(config_data), (unsigned char xdata*)&config_data);
return(0);
}
return(1);
} |