ARM Technical Support Knowledge Articles

SBIT MODIFICATIONS NOT CORRECTLY WRITTEN TO MEMORY

Applies to: C51 C Compiler

Answer


Information in this article applies to:


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

Article last edited on: 2005-07-15 14:07:21

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential