张明峰 :对现在有些年轻人的学习方法和效率感到悲哀!
扫描二维码
随时随地手机看文章
读写片内EEPROM数据区,每一本相关的数据手册都有详细的例程,抄上去就可以了。如果连抄上也不行,那你还有什么是可以的呢?
什么都想要现成的,自己一点主观的进取心都没有。你要用C写程序,这当然是好事,可是连现成的汇编语句都无法用C语言复述一遍,那你还能用C写出什么有用的代码?
自己如果不清楚,就不要瞎说。什么“不是将wr置1,写完后会自动将wr置1”:事实正好相反。
可能又要说是初学之类的借口了。那反问一句:这象认真学习的模样吗?
算了,附上一些代码吧,仅供参考。
//=============================================================
//Write a byte of data into EEPROM memory
//Input:
// eeData - data to be written
// eeAddr - EEPROM memory address
//Return:
// 0 - successful
// 1 - data error
// 2 - time out
//=============================================================
unsigned char EE_Write(void)
{
unsigned char tmpData;
EEIF = 0;
EEDATA = eeData; //data to write
EEADR = eeAddr; //adress to write
EEPGD = 0; //point to EE area
WREN = 1; //EE write enabled
GIE = 0; //no interrupt following
EECON2 = 0x55; //password 55
EECON2 = 0xaa; //password AA
WR = 1; //internal EE write cycle begin now
WREN = 0; //EE write can be disabled
GIE = 1; //interrupt could be enabled
timer = 10; //for backgound time-out control
do {
asm("CLRWDT");
} while (EEIF==0 && timer!=0); //wait for EE write completed or time-out
if (EEIF==1 && timer!=0) { //EE write completed normally
tmpData = eeData;
EE_Read(); //read back the written data
if (tmpData == eeData) //verify
return(0); //data is good
else
return(1); //data is corrupted
} else
return(2); //WW write time out
}
//=============================================================
//Read a byte of data from EEPROM memory
//Input:
// eeAddr - EEPROM memory address
//Output
// eeData - data read
//=============================================================
void EE_Read(void)
{
EEDATA = 0xff;
EEADR = eeAddr;
EEPGD = 0;
RD = 1;
eeData = EEDATA;
}