gpt4 book ai didi

c - eeprom搜索功能

转载 作者:行者123 更新时间:2023-11-30 15:36:36 25 4
gpt4 key购买 nike

我有一个数组存储在 EEPROM 中

从 {0,0,0,0,1,1,1...} 开始,地址“0”-地址“53”最多 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”个元素之后非零。

But it returns 53 always.

为什么会这样?

我使用的是c18编译器。如果逻辑上有任何错误,请纠正我。

问候

最佳答案

在上面的代码中,break仅从while循环中中断,因此当x非零时,while循环将中断,但是封装它的for循环无论如何都会递增并继续,仅在loopcnt时中断是 54(高于 53),此时 addr 将始终为 53。

关于c - eeprom搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22510235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com