We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I need to write to specific memory location, but when the program calls the function to do it, it does not. This is the function void EEPROMwrite(unsigned int adr, unsigned int dat) { EA=0; // disable Interrupts during write DEECON=(unsigned int)((adr>>8)&0x01); // mode: write byte, set address DEEDAT=dat; // set write data DEEADR=(unsigned char) adr; // start write EA=1; while((DEECON&0x80)==0); // wait until write is complete } I have noticed that when this function is called, DEECON = 0x0E, which is wrong and skips the instruction where DEECON=(unsigned int)((adr>>8)&0x01); Then it gets the data to be written, the address, sets EA and gets out of the function. it does not wait until written is complete why? Thanks
Sounds like you are using a Philips LPC9xx. The following is a simplified form that works:
sfr DEECON = 0xF1; sfr DEEDAT = 0xF2; sfr DEEADR = 0xF3; void WriteE2Byte (word E2Addr, byte Value) { DEECON = High(E2Addr); // Set byte R/W mode and address MSB DEEDAT = Value; DEEADR = Low(E2Addr); while (!(DEECON & 0x80)) PatDog(); // Wait for write }
Bob Horeck, thank you for your help, but what version of LPC9XX had its SFR wrong. I am using the LPC932. Also, in the function you posted what is PatDog();...How come when I test the EEPROM sample program it works fine, but in my program it does not. I have set up the exact same function, but with two different addresses in memory and of course different data. Thanks
Bob Horeck, I checked the Reg932.h file which a recent one (1998-2003) and the DEECON register is define as you showed in the function you posted. I found that when I try the EEPROM sample function with the simulator it works fine, but when I try it with the emulator it gives me the symptoms I described before.
"How come when I test the EEPROM sample program it works fine, but in my program it does not." Must be some mistake or misunderstanding in your program, surely? Look carefully at what you changed in going from the sample to your own program. Go back to the sample, then make just one change at a time to make it like your program. The point that it starts going wrong is, obviously, the bit to look at! Have you tried comparing the generated assembler?
Andy Neil, I did just what you said. changed one thing at the time. In fact I made the sample program just like the writing section of my program...(mostly changing characters to interger variables). It still works fine in the modified sample, but not in my program. I will keep checking...otherwise I'll post it for you to see. Thank you
"mostly changing characters to interger variables" Aha! Is that wise? Is it valid? Are you sure that is safe? Remember: the 8051 is an 8-bit processor, and ints are 16 bits in C51. Therefore, changing anything from char to int makes a BIG difference to the code generated by the compiler! In particular, because the code now has to deal with all these 16-bit values in two 8-bit halves, you are much more susceptible to interrupts happening in the middle of the operation and messing things up! Are you sure you have specified everything as volatile that needs it?