Problem with EEPROM writing 9S12DP256B

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

Problem with EEPROM writing 9S12DP256B

2,266 Views
LBdgWgt
Contributor I
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,
Labels (1)
0 Kudos
1 Reply

337 Views
imajeff
Contributor III
While searching archives I found this unanswered question. I might as well post an answer...

======
You don't need EADDR.. registers. The user guide says, "In normal modes, all EADDRHI and EADDRLO bits read zero and are not writable." Instead you set it by writing the data to the actual EEPROM location as if it were RAM.

Data should be written as a 16-bit word, and make sure the address is aligned (even number). Since you are only passing a number for the address, you'll need to change it to pass the actual pointer.

So you might change something like:
BOOL write_EEPROM(unsigned int *addr,unsigned int data)
{
// set contents to write
*addr = data;

=======
Also I notice the line:
// launch the command by writing '1' (clearing) the CBEIF flag
ESTAT |= CBEIF;

That is not writing 1 to CBEIF, but more like writing 1 to every bit that reads a 1. While I don't know if that will cause a problem in this case, The "|=" has caused trouble in many cases like this. Instead, write

ESTAT = CBEIF; // writing '1' to CBEIF executes the command
0 Kudos