I have an array stored in EEPROM
Starting with {0,0,0,0,1,1,1...} up to 54 elements from address '0'-address'53' and i have cross checked the value and everything is fine.
but when i employ a 'search function' and i have passed '0' as the argument as it searches from 0th address.
unsigned char search(char current_time)
{
unsigned int loopcnt = 0;
unsigned int add ;
unsigned char addr = 0; //We will store start address of 1's here
unsigned char lastAddr =current_time;
unsigned int x;
add = 0;
//If lastAddr is already overflowing, reset it
if(lastAddr >= 53)
{
lastAddr = 0;
addr=53;
return(addr);
}
for(loopcnt = lastAddr; loopcnt < 54; loopcnt++)
{
addr = loopcnt;
x=eeread(add);
//This is start location of our scanning
while(x!= 0)
{
x=eeread(add);
loopcnt++;
add++;
//Count the 1's we got!
if(loopcnt==53)
{
addr=53;
break;
}
}
}
return (addr);
}
But it has to return '4' as value since after the '4'th element is non zero.
But it returns 53 always.
Why is it like that?
Im using c18 compiler..If there is any mistake in logic do correct me.
Regards
In the above code it the break only breaks out of the while loop, so the while loop will break when x is non-zero, however the for-loop that is encasing it will increment and continue anyway, breaking only when loopcnt is 54 (above 53) at which point addr will always be 53.