Dai Jie

MC9S12XHZ512 EEPROM operation problems!Thanks very much.

Discussion created by Dai Jie on Sep 18, 2011
Latest reply on Oct 8, 2011 by Dai Jie

I am using MC9S12XHZ512 EEPROM to record the data and through the codewarrior memory I find the data was saved in the Addr 0x0C00. However, when the power was off and then the power was on, the data was lost. I don't know why and I need help to give me some answers. I wonder whether the address is not the correct one. The part of code is list as follow:

 

#define EEPROM_ADDR_BASE ((volatile word *)0x0C00)
#define EEP_Word_Prog     0x20         // 字编程
#define EEP_Word_Erase    0x40         // 字擦除
#define EEP_Sect_Erase    0x41         // 扇区擦除        
#define EEP_Sect_Modify   0x60         // 扇区修改

 

extern word u16tmData;
void EEP_Wt_Word(word * Addr,word Data,byte OpType);
word EEP_Rd_Word(word * Addr) ;
void EEPROM_init(void);

 

 

/*************************************************                 
EEPROM模块初始化程序  
       
*************************************************/
void EEPROM_init(void)
{
    ECLKDIV=0x49;                   // 采用8分频,因为OSCLK=14.7456MHz                               
    ECNFG=0x00;                     // 禁止EEPROM相关操作的中断
    while(ECLKDIV_EDIVLD == 0);     // 等待时钟设置成功
    EPROT_EPOPEN=1;                 // EEPROM没有保护,可以进行编程与擦除 
    EPROT_EPDIS=1;                  // EEPROM保护禁止
}

/*************************************************               
写EEPROM程序       
Addr:  要操作的EEPROM偏移地址
Data:  要操作的EEPROM数据
OpType:操作命令类型  
                  
*************************************************/
void EEP_Wt_Word(word * Addr,word Data,byte OpType)
{
    DisableInterrupts;
    while(ESTAT_CBEIF == 0);        // 等待EEPROM命令缓冲区为空
    *Addr=Data;                     // 设置EEPROM的偏移地址
    ECMD=OpType;                    // 指令的类型
    ESTAT |= 0x80;                  // 清命令缓冲标志,开始执行命令
    while(ESTAT_CCIF == 0);         // 等待命令完成
    EnableInterrupts;
}

/*************************************************               
读EEPROM程序                 
Addr:  要操作的EEPROM偏移地址
返回值:读到的结果数据  
              
*************************************************/
word EEP_Rd_Word(word * Addr)
{
    word TempData;
    DisableInterrupts;
    TempData=*Addr;                 // 读EEPROM的偏移地址的数据
    return(TempData);    
}

Outcomes