原创 调试通过的SST89E564RD的内部EEPROM的读写程序

2006-12-30 01:39 5087 9 9 分类: MCU/ 嵌入式
调试通过的SST89E564RD的内部EEPROM的读写程序


/*  89E564 Extensions */
sfr IPA   = 0xF8;
sfr IEA   = 0xE8;
sfr SADEN = 0xA9;
sfr SPSR  = 0xAA;
sfr AUXR1 = 0xA2;
sfr AUXR  = 0x8E;
sfr WDTD  = 0x85;
sfr SPDR  = 0x86;
sfr SFCF  = 0xB1;
sfr SFCM  = 0xB2;
sfr SFAL  = 0xB3;
sfr SFAH  = 0xB4;
sfr SFDT  = 0xB5;
sfr SFST  = 0xB6;
sfr IPH   = 0xB7;
sfr SPCR  = 0xD5;
sfr IPAH  = 0xF7;


//块擦除
void Block_Erase (unsigned int);
//扇区擦除
void Sector_Erase(unsigned int);
//字节编程
void Byte_Program(unsigned int, unsigned char);
//扇区拷贝
void Sector_Copy(unsigned int, unsigned int);
//字节校验
unsigned char Byte_Verify(unsigned int);
//等待IAP结束
unsigned char ready(void);


//块擦除
void Block_Erase (unsigned int DataAddr) using 0
{
  SFCF |= IAP_EN_;//启动IAP
  SFAH = DataAddr / 256;//扇区地址高字节
  SFAL = DataAddr;//扇区地址低字节
  SFDT = 0x55;
  SFCM = SFCM_BE;//写入块擦除命令字
  if (ready() == 0) {
    while(1);
  }
}

//扇区擦除
void Sector_Erase (unsigned int DataAddr) using 0
{
  SFCF |= IAP_EN_;//启动IAP
  SFAH = DataAddr / 256;//扇区地址高字节
  SFAL = DataAddr;//扇区地址低字节
  SFCM = SFCM_SE;//写入扇区擦除命令字
  if (ready() == 0) {
    while(1);
  }
}

//字节编程
void Byte_Program (unsigned int DataAddr, unsigned char DataByte) using 0
{
  SFCF |= IAP_EN_;//启动IAP
  SFAH = DataAddr >> 8;//要编程地址高字节
  SFAL = DataAddr;//要编程地址低字节
  SFDT = DataByte;//要编程的数据
  SFCM = SFCM_PB;//写入字节编程命令字
  if (ready() == 0) {
    while(1);
  }
}

//字节校验
unsigned char Byte_Verify(unsigned int DataAddr) using 0
{
unsigned char DataByte;
  SFCF |= IAP_EN_;//启动IAP
  SFAH = DataAddr >> 8;//要编程地址高字节
  SFAL = DataAddr;//要编程地址低字节
  SFDT = DataByte;//要编程的数据
  SFCM = SFCM_VB;//写入字节校验命令字
  DataByte = SFDT;
  SFCF &= ~IAP_EN_;//关闭IAP
  SFDT = 0;
  return DataByte;
}

void Sector_Copy(unsigned int DataAddr0, unsigned int DataAddr1) using 0
{
unsigned char i, ch;
  if ((DataAddr0 >= 0xC000) && (DataAddr1 >= 0xC000)) {//必须为FLASH块1
    if ((DataAddr0 & 0xff80) != (DataAddr1 & 0xff80)) {//没个扇区64个字节
      DataAddr0 &= 0xff80;//取扇区内64个字节的首地址
      DataAddr1 &= 0xff80;//取扇区内64个字节的首地址
      Sector_Erase(DataAddr0);//删除目的扇区
      for (i = 0; i < 128; i ++) {//拷贝扇区内的64个字节
        ch = Byte_Verify(DataAddr1 + i);//读源扇区数据
        Byte_Program(DataAddr0 + i, ch);//写目的扇区数据
      }
    }
  }
}


//等待IAP结束
unsigned char ready(void) using 0
{
unsigned long TimeOut = 0;
unsigned char error = 0;
  while(TimeOut ++ < 100000) {
    if ((SFST & Flash_busy_) == 0) {
      error ++;
      break;
    }
  }
  SFCF &= ~IAP_EN_;//关闭IAP
  SFDT = 0;
  return error;
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
9
关闭 站长推荐上一条 /3 下一条