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

volatile modifier for bit?

I have a flag bit variable that is set by an interrupt routine. Then in my main loop the flag is checked like:

 adc0_flag = 0;
 while(1){
    if(adc0_flag == 1){
       adc0_flag = 0;
       process_data()
    }
 ...

 }
I tried declaring adc0_flag as:

volatile bit adc0_flag;

But this did not fix the problem that call to process_data() was optimized out [using no optimization--ie opt(0)]

Changing it to:
volatile unsigned char adc0_flag;

fixes the problem--but wastes memory. I have several such bit flags being modified by interrupts. Is there an alternative?

Is it true that volatile does nothing for bit variables or am I doing something wrong?

Parents
  • "call to process_data() was optimized out "

    Are you sure it's optimisation?
    Are you sure there isn't some other problem?
    Have you checked the generated assembler?

    The code you posted has at least one typo - please cut-and-paste the exact code you are compiling!


    In the expression

    (adc0_flag == 1)
    you are comparing a bit to a literal integer constant.
    Does it help if you just write
    if( adc0_flag ){
    (choosing a better name for the flag would help here)

Reply
  • "call to process_data() was optimized out "

    Are you sure it's optimisation?
    Are you sure there isn't some other problem?
    Have you checked the generated assembler?

    The code you posted has at least one typo - please cut-and-paste the exact code you are compiling!


    In the expression

    (adc0_flag == 1)
    you are comparing a bit to a literal integer constant.
    Does it help if you just write
    if( adc0_flag ){
    (choosing a better name for the flag would help here)

Children
No data