我有一个存储在EEPROM中的数组
从{0,0,0,0,1,1,1 ...}开始,最多54个元素来自地址'0'-address'53',我已经交叉检查了值,一切都很好。< / p>
但是当我使用'搜索功能'并且我从第0个地址搜索时已经传递'0'作为参数。
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);
}
但它必须返回'4'作为值,因为'4'元素不为零。
但它总是会返回53.
为什么会这样?
我正在使用c18编译器。如果逻辑上有任何错误,请纠正我。
此致
答案 0 :(得分:1)
在上面的代码中,break只会中断while循环,所以while循环会在x非零时中断,但是包含它的for循环无论如何都会递增并继续,只有当loopcnt时才会中断是54(大于53),此时addr将始终为53。