張明峰 :對現(xiàn)在有些年輕人的學習方法和效率感到悲哀!
讀寫片內(nèi)EEPROM數(shù)據(jù)區(qū),每一本相關(guān)的數(shù)據(jù)手冊都有詳細的例程,抄上去就可以了。如果連抄上也不行,那你還有什么是可以的呢?
什么都想要現(xiàn)成的,自己一點主觀的進取心都沒有。你要用C寫程序,這當然是好事,可是連現(xiàn)成的匯編語句都無法用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;
}