本帖最后由 小夏天的大西瓜 于 2023-2-16 22:05 编辑
3 操作EEPROM前,清WDT
在对EEPROM的操作前,清WDT,保证操作期间不溢出
4 抗干扰
同时,为了抗干扰,防止误操作,EEPROM编程可以参考如下例程:
uchar ssp_flag;
voidEEPromSectorErase(UCHAR nAddrH)// 扇区擦除
{
ssp_flag= 0xA5;
_push_(IEN0);//中断控制压栈
IEN0&=0x7F;//关总中断
FLASHCON = 0x01; //访问EEPROM区
RSTSTAT = 0; //清WDT
XPAGE= nAddrH<<1 ;
IB_CON1 = 0xE6;
IB_CON2 = 0x05;
IB_CON3 = 0x0A;
IB_CON4 = 0x09;
if(ssp_flag!=0xA5)//增加flag判断,增强抗干扰
goto Error;
IB_CON5 = 0x06;
_nop_();
_nop_();
_nop_();
Error:
ssp_flag= 0;
IB_CON1= 0x00;
IB_CON2= 0x00;
IB_CON3= 0x00;
IB_CON4= 0x00;
IB_CON5= 0x00;
FLASHCON= 0x00;//切回FLASH区
_pop_(IEN0);//恢复总中断
}
voidEEPromByteProgram(UCHAR nAddrH,UCHAR nAddrL, UCHAR nData) // 扇区编程
{
ssp_flag= 0x5A;
_push_(IEN0); //中断控制压栈
IEN0&=0x7F;//关总中断
FLASHCON= 0x01; //访问EEPROM区
RSTSTAT = 0; //清WDT
XPAGE= nAddrH;
IB_OFFSET= nAddrL;
IB_DATA= nData; // 烧写内容
IB_CON1 = 0x6E;
IB_CON2 = 0x05;
IB_CON3 = 0x0A;
IB_CON4 = 0x09;
if(ssp_flag!=0x5A) //增加flag判断,增强抗干扰
goto Error;
IB_CON5 = 0x06;
_nop_();
_nop_();
_nop_();
_nop_();
Error:
ssp_flag= 0;
IB_CON1= 0x00;
IB_CON2= 0x00;
IB_CON3= 0x00;
IB_CON4= 0x00;
IB_CON5= 0x00;
FLASHCON= 0x00; //切回FLASH区
_pop_(IEN0); //恢复总中断
|