Hello Lundin,
Here is my code, I found it on this forum.
Write a byte works OK, so I simply changed the FCMD to ERASE_SECTOR.
Thanks,
David
#define EEPROM_BASE 0x1600 // Base address for EEPROM memory in a DZ32
#define EEPROM_ACCESS(x) *(unsigned char *)(x + EEPROM_BASE)
#define BYTE_PROGRAM 0x20
#define ERASE_SECTOR 0x40
// Code in Main Loop
EEPROM_Erase_Sector(0x00,0x00); // Erase Flash Sector
EEPROM_Write_Byte(0x00, Data); // Write the Data - unsigned char
// Function Implementations
void EEPROM_Erase_Sector( unsigned int address, unsigned char data)
{
void EEPROM_Write_Byte( unsigned int address, unsigned char data)
/* Writes a byte of data to the EEPROM address passed to it */
{
if (FSTAT_FACCERR) // If EEPROM access error flag is set
FSTAT_FACCERR = 1; // Then clear it
while(!FSTAT_FCBEF); // Make sure command buffer is empty
EEPROM_ACCESS(address) = data; // Write the data
FCMD = ERASE_SECTOR; //
_asm NOP; // Wait at least 4 cycles before clearing FSTAT_FCBEF
_asm NOP;
_asm NOP;
_asm NOP;
FSTAT = 0x80; // Clear command buffer empty flag
if (FSTAT_FACCERR) // If EEPROM access error flag is set
FSTAT_FACCERR = 1; // Then clear it
if (FSTAT_FPVIOL) // If EEPROM write error flag is set
FSTAT_FPVIOL = 1; // Then clear it
while (!FSTAT_FCCF); // Wait for all commands to complete
}
void EEPROM_Write_Byte( unsigned int address, unsigned char data)
/* Writes a byte of data to the EEPROM address passed to it */
{
if (FSTAT_FACCERR) // If EEPROM access error flag is set
FSTAT_FACCERR = 1; // Then clear it
while(!FSTAT_FCBEF); // Make sure command buffer is empty
EEPROM_ACCESS(address) = data; // Write the data
FCMD = BYTE_PROGRAM; // Flash/EEPROM command to program a byte (0x20)
_asm NOP; // Wait at least 4 cycles before clearing FSTAT_FCBEF
_asm NOP;
_asm NOP;
_asm NOP;
FSTAT = 0x80; // Clear command buffer empty flag
if (FSTAT_FACCERR) // If EEPROM access error flag is set
FSTAT_FACCERR = 1; // Then clear it
if (FSTAT_FPVIOL) // If EEPROM write error flag is set
FSTAT_FPVIOL = 1; // Then clear it
while (!FSTAT_FCCF); // Wait for all commands to complete
}