标题: 想请教strongchen一个EEPROM的擦写问题 [打印本页]
作者: tiaodekeyi 时间: 2006-10-17 14:31 标题: 想请教strongchen一个EEPROM的擦写问题
想请教strongchen一个EEPROM的擦写问题,我的芯片是S12DP256B
下面是我写的程序:
ECLKDIV=0x4B;
any=(volatile unsigned int*)(0x0800);
while(!(ECLKDIV & 0x80)){}
while(!(ESTAT & 0x80)){}
while(!(EPROT & 0x80)){}
*any=0xff;
ECMD=0x41;
ESTAT|=0x80;
while(!(ESTAT & 0x40)){}
eear=(volatile unsigned int*)(0x0405);
while(!(ECLKDIV & 0x80)){}
while(!(ESTAT & 0x80)){}
while(!(EPROT & 0x80)){}
*eear=0x0302;
ECMD=0x20;
ESTAT|=0x80;
while(!(ESTAT & 0x40)){}
我怎么老是写进去没有什么作用啊?
在地址0x0405里面总是FF,根本没有写进去,想请教一下是什么原因?
作者: strongchen 时间: 2006-10-17 16:27
请参考一下用CodeWarrior的PE自动生成的代码是如何写一个字节的:
/*
** ===================================================================
** Method : WriteWord (bean IntEEPROM)
**
** Description :
** The method writes the word data to an EEPROM memory.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static byte WriteWord(IEE1_TAddress AddrRow,word Data16)
{
if (ESTAT_CBEIF == 0) { /* Is command buffer full ? */
return ERR_BUSY; /* If yes then error */
}
/* ESTAT: PVIOL=1,ACCERR=1 */
ESTAT = 48; /* Clear error flags */
*(volatile word *)(AddrRow) = Data16; /* Array address and program data */
/* ECMD: ??=0,CMDB6=0,CMDB5=1,??=0,??=0,CMDB2=0,??=0,CMDB0=0 */
ECMD = 32; /* Word program command */
ESTAT_CBEIF = 1; /* Clear flag command buffer empty */
if ((ESTAT_PVIOL == 1)||(ESTAT_ACCERR == 1)) { /* Is protection violation or acces error detected ? */
return ERR_NOTAVAIL; /* If yes then error */
}
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : WriteSector (bean IntEEPROM)
**
** Description :
** The method writes the dword sector data to an EEPROM memory.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static byte WriteSector(IEE1_TAddress AddrSec,dword Data32)
{
if (ESTAT_CBEIF == 0) { /* Is command buffer full ? */
return ERR_BUSY; /* If yes then error */
}
/* ESTAT: PVIOL=1,ACCERR=1 */
ESTAT = 48; /* Clear error flags */
*(volatile word *)(AddrSec) = (word)(Data32 >> 16); /* Array address and program data - higher part */
/* ECMD: ??=0,CMDB6=1,CMDB5=1,??=0,??=0,CMDB2=0,??=0,CMDB0=0 */
ECMD = 96; /* Sector modify command */
ESTAT_CBEIF = 1; /* Clear flag command buffer empty */
if ((ESTAT_PVIOL == 1)||(ESTAT_ACCERR == 1)) { /* Is protection violation or acces error detected ? */
return ERR_NOTAVAIL; /* If yes then error */
}
while (ESTAT_CBEIF == 0) {} /* Wait to buffer empty */
return WriteWord(AddrSec + 2,(word)Data32); /* Write lower part */
}
/*
** ===================================================================
** Method : IEE1_SetByte (bean IntEEPROM)
**
** Description :
** Method writes a given byte to a specified address in
** EEPROM.
** Parameters :
** NAME - DESCRIPTION
** Addr - Address to EEPROM
** Data - Data to write
** Returns :
** --- - Error code, possible codes:
** - ERR_OK
** - ERR_SPEED
** - ERR_BUSY
** - ERR_VALUE
** - ERR_NOTAVAIL
** - ERR_RANGE
** ===================================================================
*/
byte IEE1_SetByte(IEE1_TAddress Addr,byte Data)
{
union {
byte b[4];
word w[2];
long l;
} backup;
byte idx;
if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) { /* Is given address out of EEPROM area array ? */
return ERR_RANGE; /* If yes then error */
}
if ((ESTAT & 192) != 192) { /* Is reading from EEPROM possible? */
return ERR_BUSY; /* If no then error */
}
backup.l = *(volatile dword *)(Addr & 0xFFFC); /* Load sector to variable backup */
backup.b[Addr&0x0003] = Data; /* Store data to variable backup */
Addr &= 0xFFFE; /* Aligned address */
if (*(volatile word *)(Addr) == 0xFFFF) { /* Is given EEPROM row erased ? */
idx = (byte) ((Addr & 0x0002) >> 1); /* Word index in sector */
return (WriteWord(Addr,backup.w[idx])); /* Write new content */
}
else { /* Is given address non-erased ? */
return (WriteSector(Addr & 0xFFFC,(dword)backup.l)); /* If yes then write new content */
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) |
Powered by Discuz! 7.0.0 |