/********************************write eeprom*******************************/
void write_eeprom(Byte write_page,Byte write_addr,Byte write_data)
{
EA=0;
FLASHCON=1;
XPAGE=write_page;
IB_OFFSET=write_addr;
IB_DATA=write_data;
IB_CON1=0X6E;
IB_CON2=0X05;
IB_CON3=0X0A;
IB_CON4=0X09;
IB_CON5=0X06;
_nop_();
_nop_();
_nop_();
_nop_();
XPAGE=0x00;
FLASHCON=0;
EA=1;
}
/********************************read eeprom********************************/
Byte read_eeprom(Byte read_page,Byte read_addr)
{
Byte temp;
EA=0;
FLASHCON=1;
XPAGE=read_page;
IB_OFFSET=read_addr;
temp=*(Byte code *)(read_addr);
_nop_();
_nop_();
_nop_();
_nop_();
XPAGE=0x00;
FLASHCON=0;
EA=1;
return temp;
}
/********************************erase eeprom*******************************/
void erase_eeprom(Byte erase_page)
{
EA=0;
FLASHCON=1;
XPAGE=erase_page;
IB_CON1=0XE6;
IB_CON2=0X05;
IB_CON3=0X0A;
IB_CON4=0X09;
IB_CON5=0X06;
_nop_();
_nop_();
_nop_();
_nop_();
XPAGE=0X00;
FLASHCON=0;
EA=1;
}
/********************************主函数*************************************/
void main(void)
{
CLKCON=0X20; //系统分频1/2=6M
P0CR&=0x0f; //P0.4,P0.5,P0.6,P0.7输入口
P0PCR|=0xf0; //输入口打开上拉电阻
init();
T4_init();
T5_init();
ADC_init();
if(P0_7==0)
{
while(!P0_7);
//erase_eeprom(0);
//write_eeprom(0,0,79);
//write_eeprom(0,1,80);
//write_eeprom(0,2,81);
erase_eeprom(1);
write_eeprom(1,3,82);
write_eeprom(1,4,83);
write_eeprom(1,5,84);
A=read_eeprom(0,0);
B=read_eeprom(0,1);
C=read_eeprom(0,2);
D=read_eeprom(1,3);
E=read_eeprom(1,4);
F=read_eeprom(1,5);
我先是写的扇区0的数据,写正常,读出也正常,然后关掉写,直接读扇区0的数据正常,即A=79,B=80,C=81,说明已经保存到eeprom里了,但是我写扇区1,D,E,F不是应该等于82,83,84吗,怎么实际读出来D,E,F却等于79,80,81呢 |