AT89C51ED2 的EEPROM 讀寫操作總結
先引用網(wǎng)上有朋友問到的一個問題。
本文引用地址:http://butianyuan.cn/article/201611/316345.htm////////
下面程序?qū)憯?shù)據(jù)到EEPROM:
#defineEEPROM_VC_ADD 0X0100
adr =EEPROM_VC_ADD;
// ACC = c1;
while(EECON & 0x01);
EA = 0;
ACC = 44;
EECON = 0x02;
XBYTE[EEPROM_VC_ADD] = ACC;
// *(unsigned char xdata*)adr = ACC;
EECON = 0x00;
EA = 1;
下面程序讀數(shù)據(jù)從EEPROM:
while(EECON & 0x01);
EA = 0;
EECON = 0x02;
ACC = XBYTE[EEPROM_VC_ADD+1];
// ACC = *(unsigned char xdata*)adr;
EECON = 0x00;
EA = 1;
結果: 讀出的數(shù)據(jù)總是00;
哪位大俠能賜教。在此先謝了。
///////////////////////////////////////////////////////////
其實這個朋友的程序操作步驟是對。只是出現(xiàn)了一個小問題,在讀EEPROM的時候,不能將數(shù)據(jù)暫存在ACC中,因為在 執(zhí)行 ACC = XBYTE[EEPROM_VC_ADD+1]后,
EECON=0x00編譯出來是:
CLR A
MOV EECON,A
也就是ACC已經(jīng)被清零了,所以返回的值永遠是零。
下面我的一個測試程序。
#include "REG51xD2.h"
# include "ABSACC.H"
unsigned int xdata test1 _at_ 0x500;
unsigned long xdata test2 _at_ 0x6f0;
unsigned int test3;
unsigned long test4;
unsigned char temp;
unsigned char w_dat[10]={1,2,3,4,5,6,7,8,9,0};
unsigned char r_dat[10];
unsigned char R_EEPROM_BYTE(unsigned int addr)
{
bit ea_temp;
unsigned char dat;
while((EECON&0x01)!=0);//EEBUSY = 0? 等待
ea_temp = EA;
EA=0;
EECON = 0x02; //EEE=1; 使能EEPROM
dat = XBYTE[addr]; // Read DATA; //******不能暫存在ACC中。
EECON= 0x00; // 編譯出來是 CLR A;MOV EECON,A
EA=ea_temp;
return dat; //返回讀出值
}
void R_EEPROM_nBYTE(unsigned int saddr,unsigned char num)
{
unsigned char i;
for(i=0;i
r_dat[i]=R_EEPROM_BYTE(saddr++);
}
}
void W_EEPROM_BYTE(unsigned int addr ,unsigned char dat)
{
bit ea_temp;
while((EECON&0x01)!=0);
ea_temp=EA;
EA = 0;
EECON = 0x02; // EEE=1;
XBYTE[addr]=dat;
EECON = 0x00;
EA = ea_temp;
}
void W_EEPROM_nBYTE (unsigned char num,unsigned int saddr,unsigned char *s )
{
unsigned char i;
for(i=0;i
}
void main()
{
while(1)
{
AUXR = 0x10;
test1 = 0x1234;
test2 = 0x98765432;
test3=test1;
test4 =test2;
test1++;
test2++;
W_EEPROM_nBYTE(10,0x0000,w_dat);
R_EEPROM_nBYTE(0x0000,10);
}
}
評論