Write to data EEPROM hangs without delay
Posted: Tue Sep 23, 2014 5:29 am
My PIC is the 16F1823, and I am trying to write to data EEPROM. But the wait for the WR bit hangs for the second of two bytes I write, but only if I try to write the second byte immediately after writing the first byte. Here is the code (using the CCS compiler, but no CCS peripheral libraries):
This code is called as follows:
Here are some other interesting facts: I detect the fact that it hangs in the while loop by writing to an unused output pin before and after the loop. Even when it hangs, post mortem reading of data EEPROM through MPLAB confirms that both writes to addresses 0x04 and 0x05 were effective. If I add a time-wasting for loop between the two calls to WriteEEdata, then there are no hang-ups. This WriteEEdata function I have used successfully on previous projects.
Any idea what I might be doing wrong here?
- Code: Select all
void WriteEEdata(unsigned int addr, unsigned int data)
{
EEADR = addr;
EEDATA = data;
EECON1.EEPGD = 0;
EECON1.CFGS = 0;
EECON1.WREN = 1;
INTCON.GIE = 0; //..*** Interrupts Disabled ***
EECON2 = 0x55;
EECON2 = 0xaa;
EECON1.WR = 1; //..begins the write..
INTCON.GIE = 1; //..*** Interrupts Enabled ***
while( EECON1.WR ) {;} //..wait for write to finish
EECON1.WREN = 0;
}
This code is called as follows:
- Code: Select all
WriteEEdata(0x04, MyAddress & 0xff); //..this write always returns
WriteEEdata(0x05, (MyAddress>>8) & 0xff); //..this write hangs waiting for WR bit.
Here are some other interesting facts: I detect the fact that it hangs in the while loop by writing to an unused output pin before and after the loop. Even when it hangs, post mortem reading of data EEPROM through MPLAB confirms that both writes to addresses 0x04 and 0x05 were effective. If I add a time-wasting for loop between the two calls to WriteEEdata, then there are no hang-ups. This WriteEEdata function I have used successfully on previous projects.
Any idea what I might be doing wrong here?