用下面的代码在
仿真器跟踪状态下读写正常,芯片独立运行时不能写入。请高手指点一下。谢谢
//CPU: STM8L101F3P6 主时钟设为2M
#define EEPROM_ADDR 0x9800
//---------------------------------------------------------------------
u8 Init_EEPROM(void)
{
u16 tc=0;
do
{
if(tc<0xffff) tc++; else return 1;
FLASH->DUKR=0xae;
FLASH->DUKR=0x56;
FLASH->CR2=0x00;
}while((FLASH->IAPSR 0x08)==0);
return 0;
}
//-----------------------------------------------------------------------
u8 EEPROM_Read(u16 Addr,u8 *RxBuffer,u8 Length)
{
u8 *EEP;
EEP=(u8 *)(EEPROM_ADDR+Addr);
if(Init_EEPROM()) return 1;
while(Length--){
*RxBuffer++=*EEP++;
}
FLASH->IAPSR = (uint8_t)FLASH_MemType_Data;//0xf7 lock
return 0;
}
//-----------------------------------------------------------------------
u8 EEPROM_Write(u16 Addr,u8 *RxBuffer,u8 Lenth)
{
u16 T=0;
u8 *EEP;
EEP=(u8 *)(EEPROM_ADDR+Addr);
if(Init_EEPROM()) return 1;
while(Lenth--){
*EEP++=*RxBuffer++;
T=0;
while((!(FLASH->IAPSR 0x04)) (T<0xFFFF)) T++;
if(T==0xFFFF)return 1;
}
FLASH->IAPSR = (uint8_t)FLASH_MemType_Data; // 0xf7 lock
return 0;
}
//-----------------------------------------------------------------------