Save data in EEPROM power failed???

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);
          }
}

Parents
  • best guess:
    your routine is too slow or your power drops too fast.

    a "failsafe' way of doing this is to up the power to, say 5.5V and feed the uC through a diode. Then put a large cap across the uC. Then make a circuit that detect the power dropping before the diode which generate an interrupt and save the values while the cap across the uC keep it going.

    Erik

Reply
  • best guess:
    your routine is too slow or your power drops too fast.

    a "failsafe' way of doing this is to up the power to, say 5.5V and feed the uC through a diode. Then put a large cap across the uC. Then make a circuit that detect the power dropping before the diode which generate an interrupt and save the values while the cap across the uC keep it going.

    Erik

Children
More questions in this forum