# Algorithm for finding next data in eeprom [closed]

I have an EEPROM and I'm storing data in it, from address 0x00 to 0xff. I have to scan the EEPROM regularly for non zero values.

eg:

address  0x00 0x01 0x02 0x03 0x04......0x0A 0x0B 0x0C.........0xFF
values    0    0    1    1    0          0    1   0             0


If I do a scanning function to find first address of value I'm able to do it. But, how will I bypass the first detected address (0x02), and find the next address in the second call of function?

Note: If we encountered more than one consecutive 1's, then the first address is taken (0x02).

I will be extremely happy if you provide some technique to tackle this.

int scaneeprom(void)
{ static unsigned int x,
static unsigned int counter;

for(i=0+counter; i<255;i++)
{
{
x=i;
counter++;
}

return(i);

}


This is suppose to scan from 0x04-0xff on the second call.

I would appreciate your input that will rectify my mistake.

• This question appears to be off-topic because it is about programming – m.Alin Feb 6 '14 at 11:45
• @Lundin The fact that the OP mentioned EEPROM is irrelevant. The question is actually about a programming algorithm. This question is closed for a good reason. – m.Alin Feb 26 '14 at 12:00
• @m.Alin First of all, embedded systems microcontroller programming should always be on-topic on this site. There is no site on the SE network which is more suitable for such. So by closing such questions you basically tell people "get lost, we don't want firmware programmers in our community". – Lundin Feb 26 '14 at 12:21
• @Lundin embedded programming is indeed on-topic here, but besides reading from an EEPROM (which is not the object of the question) it's a purely coding problem. As such it's a better fit for Stack Overflow. – clabacchio Feb 26 '14 at 13:59
• @Lundin both statements are right but this falls in a grey area, being about an application that runs on an embedded system. It just doesn't take specifical EE skills to answer this question, and by closing it we try to set an example of what is on-topic and what's not. – clabacchio Feb 26 '14 at 14:51

You need to find consecutive 1s and stop once you find it. In your current code, You are reading entire EEPROM even if you find required condition.

Basic Algorithm would be:

2.Read EEPROM. Increment a counter if you find 1 and reset that if you find 0.

3.During this read, if your counter is ever goes more than 2, store that location as "last address" and stop search.

Unoptimized code would be:

unsigned char Eepromscan(void)
{
unsigned char loopcnt = 0;
unsigned char onecount = 0;
unsigned char addr = 0; //We will store start address of 1's here
static unsigned char lastAddr = 0;

{
}
for(loopcnt = lastAddr; loopcnt < 0xFF; loopcnt++)
{
addr = loopcnt; //This is start location of our scanning
{
onecount++;  //Count the 1's we got!
}

if(onecount > 1)
{
//There are multiple 1s... WooHoo!
// And These 1s start at "addr" and end at current location "loopcnt"

lastAddr = loopcnt; //Next time,We will start scanning from here.
break;
}
}

}


This is rough code. There are lot of places to improvement, plus you should handle conditions like what if there wasn't any consecutive 1s in entire EEPROM. or What if you want to start scanning from start when you reach 0xFF without finding any consecutive 1s.

P.S. This code has bugs and we expect you to find those conditions and handle.

In C you could use a variable declared as "static", which will be initialized to '0' by the startup code. Below quotes are from the ISO/IEC 9899:1999 (E) Standard.

All objects with static storage duration shall be initialized (set to their initial values) before program startup.

It is also guaranteed to retain its value between calls.

3 An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

• Make sure that you declare this variable as "Unsigned Char" so that it will not increment more than 0xFF and roll over. – Swanand Feb 6 '14 at 4:10
• @Swanand That will usually work, but it's not guaranteed- only guarantee is that it's at least 8 bits. Sometimes unsigned char is > 8 bits (eg. AD's SHARC). analog.com/static/imported-files/software_manuals/… – Spehro Pefhany Feb 6 '14 at 4:22