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
  • I tried a simplified example that would compile and got the following:

    line level    source
    
       1          bit my_bit;
       2
       3
       4          void main (void)
       5          {
       6   1      my_bit = 0;
       7   1
       8   1      while(1)
       9   1        {
      10   2        if (my_bit == 1)
      11   2          {
      12   3          my_bit = 0;
      13   3          }
      14   2        }
      15   1      }
      16
    
    ASSEMBLY LISTING OF GENERATED OBJECT CODE
    
    
                 ; FUNCTION main (BEGIN)
                                               ; SOURCE LINE # 4
                                               ; SOURCE LINE # 5
                                               ; SOURCE LINE # 6
    0000 C200        R     CLR     my_bit
    0002         ?C0001:
                                               ; SOURCE LINE # 8
                                               ; SOURCE LINE # 9
                                               ; SOURCE LINE # 10
    0002 3000FD      R     JNB     my_bit,?C0001
                                               ; SOURCE LINE # 11
                                               ; SOURCE LINE # 12
    0005 C200        R     CLR     my_bit
                                               ; SOURCE LINE # 13
    0007         ?C0003:
                                               ; SOURCE LINE # 14
    0007 80F9              SJMP    ?C0001
    0009         ?C0002:
                                               ; SOURCE LINE # 15
    0009         ?C0004:
    0009 22                RET
                 ; FUNCTION main (END)
    

    This look AOK to me. Can you perhaps show us the exact C source code and the generated assembly code so we can see what you mean?

    Jon

Reply
  • I tried a simplified example that would compile and got the following:

    line level    source
    
       1          bit my_bit;
       2
       3
       4          void main (void)
       5          {
       6   1      my_bit = 0;
       7   1
       8   1      while(1)
       9   1        {
      10   2        if (my_bit == 1)
      11   2          {
      12   3          my_bit = 0;
      13   3          }
      14   2        }
      15   1      }
      16
    
    ASSEMBLY LISTING OF GENERATED OBJECT CODE
    
    
                 ; FUNCTION main (BEGIN)
                                               ; SOURCE LINE # 4
                                               ; SOURCE LINE # 5
                                               ; SOURCE LINE # 6
    0000 C200        R     CLR     my_bit
    0002         ?C0001:
                                               ; SOURCE LINE # 8
                                               ; SOURCE LINE # 9
                                               ; SOURCE LINE # 10
    0002 3000FD      R     JNB     my_bit,?C0001
                                               ; SOURCE LINE # 11
                                               ; SOURCE LINE # 12
    0005 C200        R     CLR     my_bit
                                               ; SOURCE LINE # 13
    0007         ?C0003:
                                               ; SOURCE LINE # 14
    0007 80F9              SJMP    ?C0001
    0009         ?C0002:
                                               ; SOURCE LINE # 15
    0009         ?C0004:
    0009 22                RET
                 ; FUNCTION main (END)
    

    This look AOK to me. Can you perhaps show us the exact C source code and the generated assembly code so we can see what you mean?

    Jon

Children
No data