Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I declared some variables and matrices globally in PIC16F887A code, but when I simulate that code on Proteus, one of the matrices doesn't exist in the "PIC CPU variables" window, while all the other variables and matrices exist. Why's that?

void burnLEd(unsigned char,unsigned char);
void display();
void find_Dir();
unsigned char matrix[8][8];
unsigned char len=4;
unsigned char max_len=32;
unsigned char btn=6;
unsigned char v_c=1;
unsigned char v_r=0;
unsigned char i=0;
unsigned char j=0;
char pos_c[10];
char pos_r[10];


void main()
{
    TRISB=0;                              //output for matrix rows
    TRISD=0;                              //output for matrix cols
    TRISA=0;                              //output for keypad rows
    TRISC=255;                            //input from kepad cols
    PORTA=255;
    PORTB=255;
    PORTD=0;
//Matrix initial values to zero == all LEDs off
    for(i=0; i<8; i++)
    {        for(j=0; j<8; j++)
             {        matrix[i][j]=0;
             }
    }
//Snake's initial configuration_________________________________________________
//setting its positions
    for(i=0; i<len; i++)
    {        pos_r[i]=7;
             pos_c[i]=3-i;
    }
//putting points onto Matrix
    for(i=0; i<len; i++)
    {        matrix[pos_r[i]][pos_c[i]]=1;
    }
//main LOOP_____________________________________________________________________
    while(1)
    {      find_Dir();
//updating position of the head
           pos_c[0]=pos_c[0]+v_c;
           pos_r[0]=pos_c[0]+v_r;
//checking if snake's beyond the Screen
           if(pos_c[0] > 7)
           {           pos_c[0]=0;
           }
           else if(pos_c[0] < 0)
           {    pos_c[0]=7;
           }
           if(pos_r[0] > 7)
           {           pos_r[0]=0;
           }
           else if(pos_r[0] < 0)
           {    pos_r[0]=7;
           }
//updating rest of the body of snake
           for(i=1; i<len; i++)
           {        pos_c[i]=pos_c[i-1];
                    pos_r[i]=pos_r[i-1];
           }
//putting positions on the Matrix
          matrix[pos_r[0]][pos_c[0]]=1;
          matrix[pos_r[len-1]][pos_c[len-1]]=0;
//Displaying on Matrix
           for(i=0; i<2; i++)
           {        for(j=0; j<2; j++)
                    {        display();
                    }
           }
    }
    while(1);
}

void find_Dir()
{    PORTA=1;
     if(PORTC == 2)
     {       btn=2;
              v_c=0;
              v_r=-1;
              return;
     }
     PORTA=2;
     if(PORTC == 1)
     {        btn=4;
              v_c=-1;
              v_r=0;
              return;
     }
     if(PORTC == 4)
     {        btn=6;
              v_c=1;
              v_r=0;
              return;
     }
     PORTA=4;
     if(PORTC == 2)
     {        btn=8;
              v_c=0;
              v_r=1;
              return;
     }
     PORTA=0;
}

void display()
{   for(i=0; i<8; i++)
     {       for(j=0; j<8; j++)
             {      if(matrix[i][j] == 1)
                    {               burnLED(i,j);
                    }
             }
     }
}

void burnLED(unsigned char row,unsigned char col)         //PORTB=rows,PORTD=cols
{    PORTB=255 ^ (1<<row);
     PORTD=0 | (1<<col);
     Delay_ms(1);
}
share|improve this question
2  
Because it is unused after definition and optimized out by the compiler? –  jippie Sep 7 '13 at 14:44
    
no,, its being used a lot in the code,, –  Salman Azmat Sep 7 '13 at 14:45
    
here's a screenshot,, i.imgur.com/PUw2tpa.png?1 –  Salman Azmat Sep 7 '13 at 14:56
    
Can you show us the source code? –  Dave Tweed Sep 7 '13 at 14:59
1  
Data not explicitly marked "volatile" or equivalent may be held in a register, or only calculated where consumed - in addition to the possibility of being optimized out entirely, if never consumed in a way that has side effects. Compiler authors take the "if a tree falls in a forest and no one is there to hear it..." idea to heart. –  Chris Stratton Sep 7 '13 at 16:48

2 Answers 2

up vote 3 down vote accepted

Although you have fixed the immediate issue by disabling optimization (which proves that it was being optimized out), consider what would happen down the road if you need to optimize the code. You will likely have forgotten about this, and change the settings. Or, someone else might come and change it, or maybe you create a project with new settings.

The best approach, as Chris said, is to declare that variable volatile. This tells the compiler that the variable may be modified in an ISR and prevents the compiler from removing it.

One little word can save you a lot of headaches down the road.

share|improve this answer
    
Gratuitous use of volatile isn't necessarily needed - if there actually is an ISR or distinct thread which can access the variable, then it makes sense. Otherwise it's kind of wasteful - the issue here was visibility to a debugger, not anything operational, so temporarily disabling optimizations in order to debug the algorithm is a valid approach as well. –  Chris Stratton Sep 13 '13 at 18:01

There's nothing wrong with disabling optimizations during development and turning them back on for release code.

share|improve this answer
1  
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  Stephen Collings Sep 14 '13 at 1:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.