Volatile Variable being optimized away?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have some code that is not working and it looks like the problem is that the compiler is optimizing away my variable. I added the volatile declaration and it is still being optimized away. From what I have read, this might be due to the variable not being used, but it is. And from what I have read, it is not wise to turn off the optimization? How do I get this to work?

The error is in the if (justpressed[I]) test

What am I missing?

int pressed[NUMBUTTONS]={}, justpressed[NUMBUTTONS]={}, justreleased[NUMBUTTONS]={};

int main(void)
{
    volatile int i;
 int j;
 // Run timer2 interrupt every 16.32 ms
 TCCR2A = 0;
 TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //1024 prescaler

 //Timer2 Overflow Interrupt Enable
 TIMSK2 |= 1<<TOIE2;
 sei(); //  Enable global interrupts
 while(1)
    {
  volatile int numOn=0;
  volatile int comboCheck=0;
  for (i = 0; i < 12; i++) //only check first 12 buttons
  { 
  
   if (justpressed[i]) //***@@@ At this point i is optimized away.. test malfunctions
   {
    justpressed[i] = 0;
   LEDState[i]=!LEDState[i]; //toggle LED Status
    
    for ( j =0; j < 12; j++) //count the number of LEDs lit
    { 
     if (LEDState[j]) 
     {
      numOn++;  
     }
    }
    if (numOn==5)
    {
     for ( i=0; i<5; i++)
     {
      if (LEDState[combo1[i]])
      comboCheck++;
     }
     if (comboCheck==5) // Correct combo reached
     {
      // to do---Go to second part
     }
     else // wrong combo
     {
      for ( i = 0; i< 12; i++)
      
      LEDState[i]=false;         
     }
    //Call second routine  
    }
 
   }
  }

 }
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't you need justpressed[] and etc to be volatile?  Those are the values that are changed "invisibly" to the compiler...

(in code you didn't include, so I'm just guessing...)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

justpressed[] doesn't get optimized away, just i so when I reference justpressed[i] it fails, but if I check justpressed[0] it works.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 
int pressed[NUMBUTTONS]={}, justpressed[NUMBUTTONS]={}, justreleased[NUMBUTTONS]={};

you have not initializes anything  in justpressed[NUMBUTTONS]={}. then how it will check

if (justpressed[i]) //***@@@ At this point i is optimized away.. test malfunctions
   {
    justpressed[i] = 0;
   LEDState[i]=!LEDState[i]; //toggle LED Status

and why you need volatile here?

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

Thanks & Regards,

Sasi

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

GO GREEN

Last Edited: Sat. Nov 29, 2014 - 07:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

int justpressed[NUMBUTTONS]={} will initialize with all of the elements set to 0. This is reinforced by when I print justpressed[0] ( or any other index of the array) it will return 0. If I print justpressed[i], I get a message that it was optimized away. Also when I print i, I get the same optimized away message. If I turn off optimization, it runs correctly.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes. okay but in 

  

if(justpressed[i])   // if it is a true value then only it comes inside the loop

{

..

}

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

Thanks & Regards,

Sasi

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

GO GREEN

Last Edited: Sat. Nov 29, 2014 - 07:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Making i volatile does nothing, as far as I can see, since you never even take its address. You haven't shown all your code. Assuming your ISR modifies justpressed[], then it must be volatile or the compiler can legally just assume every element is *always* zero within main.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if (justpressed[i]) //***@@@ At this point i is optimized away.. test malfunctions

How are you determining that 'i' is optimized away here? I simply don't believe you. Show the .lss file. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can't believe no one has spotted that he's modifying his outermost loop variable (i) within the loop

( A cardinal sin if found at code review time).