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 am wondering how to save a variable's value in case of power reset. I using AT89C51ED2 internal EEPROM.. what i done recently is i can write in data into EEPROM and read from it, but the problem is that once i reset the power, the data inside the EEPROM all gone too...
below is some part of my program..
unsigned char read_8bit_EEPROM (unsigned int adr) { unsigned char val; bit ea_save; while (EECON&1); //wait while busy ea_save=EA; EA=0; EECON |= 0x02;//Enable eeprom data; val=*(unsigned char xdata*)adr; EECON &= ~0x02;//Disable eeprom data; EA=ea_save; return val; } // Write EEPROM 8-bit 'val' at address 'adr' void write_8bit_EEPROM (unsigned int adr, unsigned char value) { bit ea_save; while(EECON & 0x01); ea_save=EA; EA=0; EECON |= 0x02;//Enable eeprom data *(unsigned char xdata*)adr=value; EECON &= ~0x02;//Disable eeprom data EA=ea_save; } unsigned char chk_btn(void) { unsigned char val; if(SAVE==0) { delay_5ms(); //debounce while(SAVE==0); delay(2000000); LCD_com(0x01); LCD_print(0x30); delay(20000000); delay(20000000); val=0x30; } return val; } void main() { unsigned char nv; //init_timer1(); while(1) { DX=0; //set the voltage to 5v LCD_init(); LCD_print(read_8bit_EEPROM(0x10)); delay(20000); nv=chk_btn(); write_8bit_EEPROM((0x10),nv); } }
An even more fail-safe way to do this is to use a device that's been designed for this type of situation. Something that has an auto-store feature that has a mirror of the EEPROM in RAM, that copies the RAM to EEPROM on power down, such as the X24C45 from Xicor. During normal use, when you write to the EEPROM, it really gets written to RAM. You can force it to copy RAM to EEPROM, or let it do the copy automatically as power is going away.
But, you usually still need a circuit that does something like Erik described to insure that valid power remains applied to the device long enough as power is going away. It does take time to actually complete the EEPROM write operation.
I think Xicor got bought out by some other company, Intersil, I think. This link may work for you:
www.intersil.com/.../0,1477,X24C45,0.html
Of course, the kind of scenario you're trying to handle can have all kinds of interesting "special cases" show up after you *think* you have everything working well. Be careful... that's an interesting problem to try to solve.