//----------------------------------------------------------------------------------------------
unsigned char EEPromByteRead(unsigned char nAddrH,unsigned char nAddrL)//字节读
{
unsigned char nTemp ;
unsigned int nInt ;
unsigned char code *nAddr ;
FLASHCON = 1 ;
nInt = nAddrH ; //读取地址为[nAddrH,nAddrL]
nInt <<= 8 ;
nInt += nAddrL ;
nAddr = (unsigned char code *)(nInt);
nTemp = *nAddr ; //将[nAddrH,nAddrL]内数据存入nTemp变量
FLASHCON = 0 ; //将操作区域切换回Main Block
return (nTemp) ; //将读取值作为函数返回值
}
//----------------------------------------------------------------------------------------------
void EEPromByteProgram(unsigned char nAddrH,unsigned char nAddrL, unsigned char nData)//字节编程
{
EA=0;//关闭总中断
FLASHCON = 1 ;
XPAGE = nAddrH ; //编程地址为[nAddrH,nAddrL]
IB_OFFSET = nAddrL ;
IB_DATA = nData ; //写入值为nData
IB_CON1 = 0x6E ; //开始编程操作
IB_CON2 = 0x05 ;
IB_CON3 = 0x0A ;
IB_CON4 = 0x09 ;
//=====增加flag判断,加强抗干扰能力======
if(ssp_flag!=0x5A)
goto Error_pro;
IB_CON5 = 0x06 ;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
Error_pro:
ssp_flag = 0;
IB_CON1 = 0x00;
IB_CON2 = 0x00;
IB_CON3 = 0x00;
IB_CON4 = 0x00;
IB_CON5 = 0x00;
XPAGE=0;
FLASHCON = 0 ; //将编程区域切换回Main Block
EA=1;//使能总中断
}
//----------------------------------------------------------------------------------------------
void EEPromSectorErase(unsigned char nAddrH)// 扇区擦除
{
EA=0;//关闭总中断
FLASHCON = 1 ; //切换擦除区域到EEPROM区
XPAGE = nAddrH ; //扇区号为nAddrH
IB_CON1 = 0xE6 ; //开始扇区擦除操作
IB_CON2 = 0x05 ;
IB_CON3 = 0x0A ;
IB_CON4 = 0x09 ;
//=====增加flag判断,加强抗干扰能力======
if(ssp_flag!=0xA5)
goto Error_era;
IB_CON5 = 0x06 ;
_nop_() ;
_nop_() ;
_nop_() ;
_nop_() ;
_nop_();
_nop_();
_nop_();
_nop_();
Error_era:
ssp_flag = 0;
IB_CON1 = 0x00;
IB_CON2 = 0x00;
IB_CON3 = 0x00;
IB_CON4 = 0x00;
IB_CON5 = 0x00;
XPAGE=0;
FLASHCON = 0 ; //将操作区域切换回Main Block
EA=1;//使能总中断
}
主程序初始化测试eeprom代码如下
ssp_flag=0xA5;
EEPromSectorErase(0);
ssp_flag=0x5A;
EEPromByteProgram(0,0x01,0x66);
test=EEPromByteRead(0,0x01);
变量test时钟为0,真是搞不明白是那块有问题了,请使用过该单片机的大侠指点下,非常感谢啊 |