MC9S12XHZ512 EEPROM operation problems!Thanks very much.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MC9S12XHZ512 EEPROM operation problems!Thanks very much.

652 Views
secess
Contributor I

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);    
}

Labels (1)
0 Kudos
3 Replies

396 Views
kef
Specialist I

I'm not sure what's your problem, but I don't see where you are clearing or analysing ESTAT error flags. It might be that some not related part of your software writes to flash array and as a result ACCERR or PVIOL error flag is set, inhibiting furter EEPROM programming until you clear these flags.

 

Two comments about your code.

 

    EPROT_EPOPEN=1;                 // EEPROM没有保护,可以进行编程与擦除 
    EPROT_EPDIS=1;                  // EEPROM保护禁止
^^ In datasheet it is said: "The EPOPEN and EPDIS bits can only be written to the protected state" . So lines above have no effect. Also, since it s some kind of write once bits, it makes sense to write all EPROT bits at once, not separately.

 

   ESTAT |= 0x80;                  // 清命令缓冲标志,开始执行命令

^^ It is not right way to clear CBEIF flag. Oring ESTAT with something, you may write ones not only to CBEIF flag, but also to other ESTAT flags. Consider the case when both CBEIF and ACCERR bits are set. 0x90 | 0x80 = 0x90. Since ACCERR is set, command won't be launched. Instead of command launch you would get ACCERR bit cleared and CBEIF bit still set, indicating idle state of EEPROM. You then may try to read error bits and get nothing, everything is alright, but data bot written. Right way to clear CBEIF is

  ESTAT = ESTAT_CBEIF_MASK;

or

  ESTAT &= ESTAT_CBEIF_MASK; // 0x90/*CBEIF and ACCERR*/ & 0x80/*CBEIF*/ = 0x80/*CBEIF*/

 

0 Kudos

396 Views
secess
Contributor I

Sorry to reply so late. I am very glad that the problem has been solved and thank you for your advice.

I remove the two lines: EPROT_EPOPEN=1;EPROT_EPDIS=1;

The results are correct. OMG, to excited. Thanks a lot.

0 Kudos

396 Views
secess
Contributor I

Thanks for your advise. I will read the datasheet again and try to find the solution.

0 Kudos