“Not enough RAM ” error in PIC16F877a

I have been trying to run the following code:

void reset();

int board[8][8];
int i,switcher,j,k;

void main() {
TRISB = 0X00;
TRISD = 0X00;
TRISC = 0X00;
reset();
TRISA =0X11;
while(1)
{
for( i=0;i<16;i++)
{
switcher = i;
PORTB.F0  = switcher%2;
switcher /= 2;
PORTB.F1 = switcher%2;
switcher /= 2;
PORTB.F2 = switcher%2;
switcher /= 2;
PORTB.F3 = switcher%2 ;
if (!PORTA.F0 == board[i/8][i%8])
break;
delay_ms(1000);
}
switcher = i;
PORTC.F0   =  switcher%2;
switcher /= 2;
PORTC.F1 = switcher%2;
switcher /= 2;
PORTC.F2 = switcher%2;
switcher /= 2;
PORTC.F3 = switcher%2 ;
switcher /= 2;
PORTC.F4 = switcher%2;
switcher /= 2;
PORTC.F5 = switcher%2 ;
break;
}
}

void reset()
{
PORTB = 0;
PORTD = 0;
PORTC= 0;
}


On running this code, it's showing

"Not enough RAM" error.

However on running this code :

void reset();

int board[80][80];

void main() {
TRISB = 0X00;
TRISD = 0X00;
TRISC = 0X00;
reset();
TRISA =0X11;
}

void reset()
{
PORTB = 0;
PORTD = 0;
PORTC= 0;
}


It runs successfully with no errors (Note that I am creating a multidimensional array of 80x80 whereas for the previous one it's only 8x8). Can someone help me in identifying the cause for the problem? And if possible then also the proper solution for the problem ?

• Your array presumably uses more memory than the chip has. In the second case, you never actually use it, so it is dropped by the compiler and there is no issue. – Chris Stratton Sep 9 at 15:01
• You have probably chosen the wrong part for a task approached in this way, but you should also be using a bitmap not a full int (which is likely 2 or even 4 bytes) to store what appears to be a binary value. – Chris Stratton Sep 9 at 15:03
• I have tried using uint8_t for declaring variables instead of int , which i assume should only take around 100 bytes for the program . But still it is showing the same error. – ferty567 Sep 9 at 15:23
• That's still using 8 bits to store only one. And there may be other reservations taking memory beyond just your array. – Chris Stratton Sep 9 at 15:33
• Ok Chris i will try. Thank you for your reply. – ferty567 Sep 9 at 15:35

Presumably, you are only using each element of the array for a single boolean value--in that case you can reduce your space requirements to 64 bytes of space by packing the data more efficiently. This answer describes one way to do this, where bitwise operations are used to index into an array of uint8_t values. Alternatively, you could try using your existing technique (2D integer array) with a uint8_t[8][8] to avoid making larger code changes, but this remains wasteful of memory (8x waste).