Keil Logo

C51: SBIT MODIFICATIONS NOT CORRECTLY WRITTEN TO MEMORY


Information in this article applies to:

  • C51 All Versions

QUESTION

I have declared a bdata variable and created bits (using sbit). When I change the bits in my program, the changes are not written back into memory. What's going on?

ANSWER

You should use the volatile keyword to define bdata variables that are modified with sbit's. volatile is an ANSI standard keyword that disables access optimization and is required to prevent the compiler from re-using the unmodified variable content. For example:

volatile unsigned char bdata var;  // use volatile keyword here
sbit var_0 = var^0;
sbit var_1 = var^1;
unsigned char xdata values[10];

void main (void)  {
  unsigned char i;

  for (i = 0; i < sizeof (values); i++)  {
    var = values[i];
    if (var_0)  {
      var_1 = 1;
      values[i] = var;  // without the volatile keyword, the compiler
                        // assumes that 'var' is unmodified and does not
                        // reload the variable content.
    }
  }
}

MORE INFORMATION

SEE ALSO

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Friday, July 15, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.