PDA

View Full Version : Searching and Matching Input String with data stored in external EEPROM.



sayzer
- 16th December 2009, 09:24
This code is to search a string inside a data stored in an external EEPROM.

For example, an external eeprom is full of IDs in mini packet sizes say 8-byte;
And we have an ID coming from somewhere, serial comm, button etc. and we put that ID in a mini array.
Then, we search the entire external eeprom and try to match the ID with the stored IDs in external EEPROM.

When ID is found, the search is terminated immediately.

Any comments, additions, modifications are welcome.



<font color="#000000">SDA <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">6
</font>SCL <font color="#000080"><b>VAR </b></font>PORTA.<font color="#FF0000">7
</font>Index1 <font color="#000080"><b>VAR BYTE </b></font>BANK0 SYSTEM
Index2 <font color="#000080"><b>VAR BYTE
</b></font>IDFound <font color="#000080"><b>VAR BYTE
</b></font>EEAdrs <font color="#000080"><b>VAR WORD

</b></font>IDSize <font color="#000080"><b>CON </b></font><font color="#FF0000">8 </font><font color="#000080"><i>' The number of the elements of which our StringID is made.
</i></font>EECap <font color="#000080"><b>CON </b></font><font color="#FF0000">8192 </font><font color="#000080"><i>' IF using 24LC64 for example, eeprom capacity is 8192 bytes.
</i></font>EEPage <font color="#000080"><b>CON </b></font><font color="#FF0000">32 </font><font color="#000080"><i>' EEprom Page size. For 24LC64, Page size is 32-byte.
</i></font>ArraySize <font color="#000080"><b>CON </b></font><font color="#FF0000">64 </font><font color="#000080"><i>' Array size.
</i></font>TempPage <font color="#000080"><b>VAR BYTE</b></font>[ArraySize] <font color="#000080"><i>' Create Array to store EEProm data.
</i></font>StringID <font color="#000080"><b>VAR BYTE</b></font>[IDSize] BANK0 SYSTEM <font color="#000080"><i>'Create Array to store ID string data.



</i><b>PAUSE </b></font><font color="#FF0000">100

</font>Begin:
ARRAYWRITE StringID,[<font color="#008000">&quot;ABCD1234&quot;</font>] <font color="#000080"><i>' This is the ID (String) that we will search in external EEPROM.

</i></font>Menu:

<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000">$fe</font>,<font color="#FF0000">1</font>, <font color="#008000">&quot;ID:&quot;</font>,<font color="#000080"><b>STR </b></font>StringID\IDSize <font color="#000080"><i>' Print ID to be searched.
</i><b>PAUSE </b></font><font color="#FF0000">500

</font><font color="#000080"><b>GOSUB </b></font>SearchID <font color="#000080"><i>' Search the input string on external EEPROM.


</i><b>GOTO </b></font>Begin


SearchID:
EEAdrs = <font color="#FF0000">0 </font><font color="#000080"><i>' Starting location address on external eeprom.

</i></font>Cont0:
<font color="#000080"><b>I2CREAD </b></font>SDA, SCL,<font color="#FF0000">$A0</font>,EEAdrs,[<font color="#000080"><b>STR </b></font>TempPage\ArraySize] <font color="#000080"><i>' Read 64 bytes (ArraySize) at once.


</i><b>FOR </b></font>Index2 = <font color="#FF0000">0 </font><font color="#000080"><b>TO </b></font>(ArraySize-<font color="#FF0000">1</font>) <font color="#000080"><b>STEP </b></font>IDSize <font color="#000080"><i>' Search through 64-byte array with IDSize-byte mini packs.

</i></font>IDFound = <font color="#FF0000">0 </font><font color="#000080"><i>' If ID is found, this variable will be 8.
</i><b>FOR </b></font>Index1 = <font color="#FF0000">0 </font><font color="#000080"><b>TO </b></font>(IDSize-<font color="#FF0000">1</font>) <font color="#000080"><i>' Compare all StringID elements to mini packets inside ArraySize-byte main array.
</i><b>IF </b></font>StringID[Index1] = TempPage[Index1+Index2] <font color="#000080"><b>THEN </b></font>IDFound = IDFound + <font color="#FF0000">1 </font><font color="#000080"><i>' IF the bytes are the same, increment IDFound.
</i><b>NEXT </b></font>Index1

<font color="#000080"><b>IF </b></font>IDFound = IDSize <font color="#000080"><b>THEN </b></font>Found <font color="#000080"><i>' All eight StringID array elements are found &amp; matched in the main array. Exit both &quot;for loops&quot;.

</i><b>NEXT </b></font>Index2

<font color="#000080"><b>IF </b></font>EEAdr &gt;= EECap <font color="#000080"><b>THEN </b></font>NotFound <font color="#000080"><i>' Searching external EEprom is done. We are now at the last location of external Eprom.
</i></font>EEAdrs = EEAdrs + ArraySize <font color="#000080"><i>' Increment EEprom location address by array size.

</i><b>GOTO </b></font>Cont0 <font color="#000080"><i>' Search is still in progress, ID not found yet. Keep searching.


</i></font>NotFound:
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000">$fe</font>,<font color="#FF0000">1</font>, <font color="#008000">&quot;ID Not Found&quot;
</font><font color="#000080"><b>PAUSE </b></font><font color="#FF0000">500
</font><font color="#000080"><i>' Take some action here.

</i><b>RETURN


</b></font>Found:
<font color="#000080"><b>LCDOUT </b></font><font color="#FF0000">$fe</font>,<font color="#FF0000">1</font>, <font color="#008000">&quot;ID Found&quot;
</font><font color="#000080"><b>PAUSE </b></font><font color="#FF0000">500
</font><font color="#000080"><i>' Take some action here.

</i><b>RETURN







END

</b></font>

nquere
- 28th January 2010, 22:15
Thanks !:)
It's that i want to do this night :D