我在EEPROM中存储了一个数组
从地址‘0’到地址‘53’的{0,0,0,1,1.}最多54个元素开始,我已经交叉检查了值,一切都很好。
但是,当我使用‘搜索函数’,并且我已经传递'0‘作为参数,因为它搜索从第0地址。
无符号字符搜索( 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 compiler..If,逻辑上有任何错误,请纠正我。
问候
发布于 2014-03-19 16:29:57
在上面的代码中,中断只会从while循环中中断,因此,while循环在x为非零时会中断,但是包围它的for-循环无论如何都会增加并继续,只有在循环为54 (高于53)时才会中断,此时addr始终是53。
https://stackoverflow.com/questions/22510235
复制相似问题