EEPROM Read/Write AT89C51AC3

Hello,

I have the following code for writing/reading EEPROM of this device.

unsigned char eepromWrite (const unsigned int Address, unsigned char Data) {
    bit oldEA;
    unsigned char DataReadBack;

    if (Address <= 0x07ff)
    {
        oldEA = EA;
        EA = 0;

        EECON |= 0x02; // Enable EEPROM memory space
        XBYTE[Address] = Data;
        EECON &= 0xFD; // Disable EEPROM memory space
        EA = oldEA;

        EECON = 0x50; EECON = 0xA0;     // Programming...

        // wait programming...
        while(EECON & b00000001);

        // Byte re-read
        oldEA = EA;
        EA = 0;
        EECON |= 0x02; // Enable EEPROM memory space
        DataReadBack = XBYTE[Address];
        EECON &= 0xFD; // Disable EEPROM memory space
        EA = oldEA;
        if (DataReadBack != Data)
        {
            return(2);
        }
        return(0);
    }
    else
    {
        return(1);
    }
}

unsigned char eepromRead (const unsigned int Address, unsigned char* Data) {
bit oldEA;
	if (Address <= 0x07ff) {
	    oldEA = EA;
	    EA = 0;
		while(EECON & b00000001); //EEbusy
	    EECON |= 0x02; // Enable EEPROM memory space
	    *Data = XBYTE[Address];
	    EECON &= 0xFD; // Disable EEPROM memory space
	    EA = oldEA;
		return (0);
	}
    else
    {
        *Data = 0;
        return(1);
    }
}

Then I write the following code for write/read multiple byte (or structure)

void write_to_EEPROM (unsigned int EEPROM_address, void *buffer, unsigned char buffer_len) {
unsigned char *bp;
for (bp = buffer; buffer_len; buffer_len--, bp++, EEPROM_address++)
  {
	eepromWrite(EEPROM_address, *bp);
  }
  Wait20ms();
}

void read_from_EEPROM (unsigned int EEPROM_address, void *buffer, unsigned char buffer_len) {
unsigned char *bp;

for (bp=buffer; buffer_len; buffer_len--, bp++, EEPROM_address++)
  {
	eepromRead(EEPROM_address, bp);
  }
}

Most of the time it works. For example I could write/read a struct:
typedef struct    {
                     uchar       MotorMin;
		     uchar       MotorKnee;
                     uchar       MotorMax;
                  }  Blower_TYP;

//////////

write_to_EEPROM (FAN_ADDR, &BlowerNew, sizeof (Blower_TYP));

void readMotorConf (void) {
 Blower_TYP temp;
 read_from_EEPROM(FAN_ADDR, &temp, sizeof(Blower_TYP));
 Blower = temp;
}
}


Only sometime the read function give wrong reading of the blower. What could be the problem on above code? Thanks for advise!

Parents Reply Children
No data
More questions in this forum