This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

volertale variebles or eeprom mem ory

I got the question about volatile variebles and eeprom memory. (I use C and IDE)

First of all, I have a program that has to remember a variable's value even if I reset the thing (this value changes between 0 - 9 so it is an unsigned char). The details and the program is below ;

*counter is my variable that I change value via interrupt and push button
*counter is my variable that microcontroller has to remember after reset
*counter is my variable that can be changed by both main and interrupt


----------------------------------------------------------------

volatile unsigned char counter;
.....
.....


void interrupt()
{
  counter++;
  Delay_ms(150);
  if(counter == 10)
  counter=0;
  while(bit is zero);
  Delay_ms(20);
  .....
  .....
  SFRA=0xD0;
}

void main()
{

  for(;
  {



    if(counter == 0)
     {
       .....program writes 7 seg the value of counter
     }

     if(counter == 1)
     {
        .....program writes 7 seg the value of counter

     }

     if(counter == 2)
     {
      .....program writes 7 seg the value of counter

     }

     if(counter == 3)
     {
        .....program writes 7 seg the value of counter
     }

     if(counter == 4)
     {
        .....program writes 7 seg the value of counter

     }

     if(counter == 5)
     {

     .....program writes 7 seg the value of counter
     }

     if(counter == 6)
     {
       .....program writes 7 seg the value of counter
     }

     if(counter == 7)
     {
      ......program writes 7 seg the value of counter

      }

      if(counter ==
      {
       ........program writes 7 seg the value of counter
      }

      if(counter == 9)
      {
      ......program writes 7 seg the value of counter
      }


  }



}

Now, this program does not work properly it loses the value of counter. I got two choices;

1. declaring counter variable as a volatile  (but I could not succeed)
2. using eeprom memory but this will increase my loop time.

Please help me   I am confused.

Parents
  • The "volatile" keyword has nothing to do with a variable surviving a reset. It just informs the compiler that some unknown external force may modify the variable at any time, so the compiler may not assume that it can keep a copy of the variable in a register and reuse multiple times.

    For nonvolatile storage, i.e. data that should be kept even if the processor is reset or after a power loss, you must use EEPROM, flash, battery-backed RAM, FRAM or similar, i.e. you must use a memory type that is capable of retaining data when your equipment is not powered.

Reply
  • The "volatile" keyword has nothing to do with a variable surviving a reset. It just informs the compiler that some unknown external force may modify the variable at any time, so the compiler may not assume that it can keep a copy of the variable in a register and reuse multiple times.

    For nonvolatile storage, i.e. data that should be kept even if the processor is reset or after a power loss, you must use EEPROM, flash, battery-backed RAM, FRAM or similar, i.e. you must use a memory type that is capable of retaining data when your equipment is not powered.

Children
No data