Declaré algunas variables y matrices globalmente en el código PIC16F887A, pero cuando simulo ese código en Proteus, una de las matrices no existe en la ventana "PIC CPU variables", mientras que todas las demás variables y matrices existen. ¿Porque eso?
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);
}
Aunque solucionó el problema inmediato al deshabilitar la optimización (lo que prueba que se estaba optimizando), considere lo que sucedería en el futuro si necesita optimizar el código. Es probable que se haya olvidado de esto y cambie la configuración. O bien, alguien más podría venir y cambiarlo, o tal vez cree un proyecto con una nueva configuración.
El mejor enfoque, como dijo Chris, es declarar que la variable es volátil. Esto le dice al compilador que la variable puede modificarse en una ISR y evita que el compilador la elimine.
Una pequeña palabra puede ahorrarte muchos dolores de cabeza en el futuro.
No hay nada de malo en deshabilitar las optimizaciones durante el desarrollo y volver a activarlas para el código de lanzamiento.
yippie
salman azmat
salman azmat
david tweed
salman azmat
Kaz
chris stratton
salman azmat
adam lorenzo