Leo Hendrawan

Problem with EEPROM writing 9S12DP256B

Discussion created by Leo Hendrawan on Mar 5, 2006
Latest reply on Aug 25, 2006 by Jeff Smith
Hi all,

I am using 9S12DP256B microcontroller, and I am using the ICC12 compiler, I have some problem for making some code about EEPROM writing.

do we have to erase the section (address) before we program(write) the eeprom?

this is my code:

// undefined EEPROM address and data register
#define EADDRHI _P(0x0118) // EEPROM address register high byte
#define EADDRLO _P(0x0119) // EEPROM address register low byte
#define EDATAHI _P(0x011A) // EEPROM data register high byte
#define EDATALO _P(0x011B) // EEPROM data register low byte

// ESTAT register
#define CBEIF 0x80
#define CCIF 0x40
#define PVIOL 0x20
#define ACCERR 0x10

.......................

BOOL write_EEPROM(unsigned int addr,unsigned int data)
{
// check pending access error or protection violation
if((ESTAT & ACCERR) || (ESTAT & PVIOL))
return FALSE;

// check for empty buffer
while(!(ESTAT & CBEIF));

// set address
EADDRHI = (char) (addr>>8);
EADDRLO = (char) addr;
// set data
EDATAHI = (char) (data>>8);
EDATAHI = (char) data;

// write program command
ECMD = PROGRAM_CMD;

// launch the command by writing '1' (clearing) the CBEIF flag
ESTAT |= CBEIF;

if(ESTAT & ACCERR)
LED_ON(5); // debug
else
LED_OFF(5); // debug

if(ESTAT & PVIOL)
LED_ON(4); // debug
else
LED_OFF(4); // debug

// check for empty buffer
while(!(ESTAT & CBEIF));

// wait for CCIF
while(!(ESTAT & CCIF));

return TRUE;
}

.......................


I followed the instruction from the EETS4K Block User Guide V02.07, did i miss something? I think I've set the ECLKDIV value correctly since I can do read operation.

Thank you very much for any help,

regards,

Outcomes