Creating and using EEPROM Strings with Edata
Some 14-bit core and most 18F microcontrollers have on-board EEPROM (Electrically Erasable Programmable Read-Only Memory), which is a bit of a misnomer because it is not read only, and although writing to this memory too many times is unhealthy for the device, reading this memory is both fast and harmless to it. Which offers a great place for text and data storage and retrieval.
Combining the EEPROM of a device with a string format, the compiler is capable of reducing the overhead of printing, or transmitting large amounts of text data using a memory resource that is very often left unused and ignored. The Estr modifier may be used in commands that deal with text processing i.e. Print, Serout, HRsout, and Rsout and String handling etc.
The Estr modifier is used in conjunction with the Edata directive, which is used to initially create the string of characters: -
EE_String1 Edata "Hello World", 0
The above line of code will create, in the EEPROM area (named Data Memory in the datasheets), the values that make up the ASCII text "Hello World", at address EE_String1 in Data (EEPROM) Memory. Note the null terminator after the ASCII text.
To display, or transmit this string of characters, the following command structure could be used:
HRsoutLn Estr EE_String1
The identifier that declared the address where the list of Edata values resided, now becomes the string's name. In a large program with lots of text formatting, this type of structure can save many bytes of valuable code space.
Try both these small programs, and you'll see that using Estr saves code space, but it does fill up the, relatively, small EEPROM space: -
First the standard way of displaying text: -
Device = 18F26K40
' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
' Set the Baud Rate for HRsoutLn
HRsoutLn "HELLO WORLD"
HRsoutLn "HOW ARE YOU?"
HRsoutLn "I AM FINE!"
Now using the Estr modifier: -
Device = 18F26K40
' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600
' Set the Baud Rate for HRsoutLn
Text1 Edata "HELLO WORLD", 0
Text2 Edata "HOW ARE YOU?", 0
Text3 Edata "I AM FINE!", 0
HRsoutLn Estr Text1
HRsoutLn Estr Text2
HRsoutLn Estr Text3
Again, note the null terminators after the ASCII text in the Edata directives. Without these, the microcontroller will continue to transmit data in an endless loop.
The term 'virtual string' relates to the fact that a string formed from the Edata command cannot (rather should not) be written to often, but can be read as many times as wished without causing harm to the device.
Not only identifiers can be used with the Estr modifier, constants, variables and expressions can also be used that will hold the address of the Edata's identifier (a pointer). For example, the program below uses a Byte size variable to hold 2 pointers (address of a variable or array) to 2 individual null terminated text strings formed by Edata .
Device = 18F26K40
' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud Rate for HRsoutLn
Dim wAddress as Word
' Pointer variable
'
' Create the text to display in EEPROM
'
EE_String1 Edata "HELLO ", 0
EE_String2 Edata "WORLD", 0
wAddress = EE_String1
' Point address to EE_String1
HRsoutLn Estr wAddress
' Display EE_String1
wAddress = EE_String2
' Point Address to EE_String2
HRsoutLn Estr wAddress
' Display EE_String2
Notes
Note that the identifying text must be located on the same line as the Edata directive or a syntax error will be produced. It must also not contain a postfix colon as does a line label or it will be treat as a line label. Think of it as an alias name to a constant, where the constant is the address within EEPROM where the values reside.
Any Edata directives must be placed at the head of the BASIC program as is done with Symbols, so that the name is recognised by the rest of the program as it is parsed. There is no need to jump over Edata directives as you have to with Cdata, because they do not occupy flash memory, but reside in high data memory.