ChckSum += SaveTmp[i]; //Calculate the cumulative sum
ChckSum ^= 0x5555; //Invert every other bit, avoid all 0s
SaveTmp[64] = (u8)(ChckSum >> 8);
SaveTmp[65] = (u8)ChckSum;
PassWord = D_PASSWORD; //given password
EEPROM_SectorErase(addr); //Erase a sector
PassWord = D_PASSWORD; //given password
EEPROM_write_n(addr, SaveTmp, 66); //write a sector
for(i=0; i<66; i++)
tmp[i] = 0; //clear buffer
PassWord = D_PASSWORD; //given password
EEPROM_read_n(addr,tmp,66); //read sector 0
for(i=0; i<66; i++) //data comparison
{
if(SaveTmp[i] != tmp[i])
return 1; //If there is an error in the data, return 1
}
return 0; // If the cumulative sum is right, return 0
}
/********************* main function *************************/
void main(void)
{
u8 i;
u8 status; //Statuc
UART1_config(); // select baud rate, 2: Use Timer2 as baud rate generator,
//Other values: Use Timer1 as baud rate generator,
EA = 1; //Enable CPU interrupt
PrintString1("STC8G-8H-8C familyMCU
用串口
1
测试
EEPROM
程序
!\r\n"); //UART1send a string
// Power on and read 3 sectors and verify, if there is a sector error, write
the correct sector into the wrong sector, if all 3 sectors are wrong, write the default value.
status = 0;
if(ReadRecord(0x0000) == 0) //read sector 0
{
status |= 0x01; //If it is correct, mark status.0=1
for(i=0; i<64; i++)
SaveTmp[i] = tmp[i]; //save data in write buffer
}
if(ReadRecord(0x0200) == 0) //Read sector 1
{
status |= 0x02; //If it is correct, mark status.1=1
for(i=0; i<64; i++)
SaveTmp[i] = tmp[i]; //save data in write buffer
}
if(ReadRecord(0x0400) == 0) //Read sector 2
{
status |= 0x04; //If it is correct, mark status.2=1
for(i=0; i<64; i++)
SaveTmp[i] = tmp[i]; //save data in write buffer
}
if(status == 0) //If all sectors are wrong, write default value
{