Storing and accessing phrases in EEPROM

jodicalhon

New Member
I am storing some phrases in EEPROM on an 18A and, for the moment at least, sending them one after another to an LCD. (Part of a K9 model I'm building for my son.)

My coding is pretty clunky I think. Am I going about this in the right way?

EEPROM 6, ("Greetings, Master.") '6 to 23
EEPROM 24, ("Scanning for lifeforms...") '24 to 49
....more phrases...

SendPhrase:
b1 = 6
b2 = 23
gosub SendData
b1 = 24
b2 = 49
gosub SendData

SendData:
FOR get = b1 TO b2
READ get,byte
GOSUB SendDataByte
NEXT

This is all very straightforward, and is working which is always a good start, but I'm sure there's a better way.

Any ideas?



Edited by - Jo C on 04/05/2007 14:42:12
 

Jeremy Leach

Senior Member
I'd have a 'PhraseNumber' passed in a variable to your SendPhrase routine. Have another table in EEPROM that stores the start address and length of each phrase. The PhraseNumber is used to index this table and retrieve the StartAddress and Length of the phrase you want.

Could also do this Lookup with a couple of lookup commands. Could even dispense with the length information altogether because it's simply the start address of the next phrase minus the start address of the current phrase (but you would have to have a dummy start address after your last phrase).
 

hippy

Technical Support
Staff member
The only other alternative I can think of is to use a 'null-terminated string', where a null ( usually zero, or any other value you will never send to the LCD ) indicates the end of the message -<code><pre><font size=2 face='Courier'>EEPROM 6, (&quot;Greetings, Master.&quot;,0) ' 6 to 24
EEPROM 25, (&quot;Scanning for lifeforms...&quot;,0) ' 25 to 51

SendPhrase:
get = 6 : gosub SendData
get = 25 : gosub SendData

SendData:
Read get,byte
If byte = 0 Then SendDataDone
Gosub SendDataByte
get = get + 1
GoTo SendData
SendDataDone:
Reurn </font></pre></code> That does increase the amount of Eeprom used but reduces the amount of Code. It's swings and roundabouts. The 'Senddata:&quot; routine could be frther optimised -<code><pre><font size=2 face='Courier'>SendDataNow:
Gosub SendDataByte
SendData:
Read get,byte
get = get + 1
If byte &lt;&gt; 0 Then SendDataNow
Reurn </font></pre></code> The 'get' variable will also point to the start of the next message so you might not have to set it every time before calling 'SendData&quot;.
 
Top