Edata
Syntax
Edata Constant1 { ,...Constantn etc }
Overview
Places constants or strings directly into the on-board EEPROM of compatible microcontroller's
Parameters
Constant1, Constantn are values that will be stored in the on-board EEPROM. When using an Edata statement, all the values specified will be placed in the EEPROM starting at location 0. The Edata statement does not allow you to specify an EEPROM address other than the beginning location at 0. To specify a location to write or read data from the EEPROM other than 0 refer to the Eread, Ewrite commands.
Example
' Stores the values 1000,20,255,15, and the ASCII values for
' H','e','l','l','o' in the EEPROM starting at memory position 0.
Edata 1000, 20, $FF, 0b00001111, "Hello"
Notes
16-bit, 24-bit, 32-bit and floating point values may also be placed into EEPROM. These are placed LSB first (Lowest Significant Byte). For example, if 1000 is placed into an Edata statement, then the order is: -
Edata 1000
In EEPROM it looks like 232, 03
Alias's to constants may also be used in an Edata statement: -
Symbol cAlias = 200
Edata cAlias, 120, 254, "Hello World"
Addressing an Edata table.
EEPROM data starts at address 0 and works up towards the maximum amount that the PICmicroâ„¢ will allow. However, it is rarely the case that the information stored in EEPROM is one continuous piece of data. EEPROM is normally used for storage of several values or strings of text, so a method of accessing each piece of data is essential. Consider the following piece of code: -
Edata "Hello"
Edata "World"
Now we know that EEPROM memory starts at 0, so the text "Hello" must be located at address 0, and we also know that the text "Hello" is built from 5 characters with each character occupying a byte of EEPROM memory, so the text "World" must start at address 5 and also contains 5 characters, so the next available piece of EEPROM memory is located at address 10. To access the two separate text strings we would need to keep a record of the start and end address's of each character placed in the tables. Counting the amount of EEPROM used by each piece of data is acceptable if only a few Edata tables are used in the program, but it can become tedious if multiple values and strings are needing to be stored, and can lead to program glitches if the count is wrong.
Placing an identifying name before the Edata table will allow the compiler to do the byte counting for you. The compiler will store the EEPROM address associated with the table in the identifying name as a constant value. For example: -
Hello_Text Edata "Hello"
World_Text Edata "World"
The name Hello_Text is now recognised as a constant with the value of 0, referring to address 0 that the text string "Hello" starts at. The World_Text is a constant holding the value 5, which refers to the address that the text string "World" starts at.
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.
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 Ldata or Cdata, because they do not occupy flash memory, but reside in high data memory.
The example program below illustrates the use of EEPROM addressing.
' Display two text strings held in EEPROM
Device = 18F26K40
' Tell the compiler what device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Dim MyChar as Byte
' Holds the character read from EEPROM
Dim Charpos as Byte
' Holds the address within EEPROM
'
' Create a string of text in EEPROM. null terminated
Hello Edata "HELLO ",0
'
' Create another string of text in EEPROM. null terminated
World Edata "WORLD",0
Charpos = Hello
' Point Charpos to the start of text "Hello"
DisplayText()
' Display the text "Hello"
Charpos = World
' Point Charpos to the start of text "World"
DisplayText()
' Display the text "World"
Stop
' We're all done
'
' Procedure to read and display the text held at the address in Charpos
'
Proc DisplayText()
Do
' Create an infinite loop
MyChar = Eread Charpos
' Read the EEPROM data
If MyChar = 0 Then Break ' Exit when null found
HRsout MyChar
' Display the character
Inc Charpos
' Move up to the next address
Loop
' Close the loop
EndProc
Formatting an Edata table.
Sometimes it is necessary to create a data table with a known format for its values. For example all values will occupy 4 bytes of data space even though the value itself would only occupy 1 or 2 bytes.
Edata 100000, 10000, 1000, 100, 10, 1
The above line of code would produce an uneven data space usage, as each value requires a different amount of data space to hold the values. 100000 would require 4 bytes of EEPROM space, 10000 and 1000 would require 2 bytes, but 100, 10, and 1 would only require 1 byte.
Reading these values using Eread would cause problems because there is no way of knowing the amount of bytes to read in order to increment to the next valid value.
The answer is to use formatters to ensure that a value occupies a predetermined amount of bytes.
These are: -
Byte
Word
Long
Dword
Float
Placing one of these formatters before the value in question will force a given length.
Edata Dword 100000, Dword 10000 ,_
Dword 1000, Dword 100, Dword 10, Dword 1
Byte will force the value to occupy one byte of EEPROM space, regardless of its value. Any values above 255 will be truncated to the least significant byte.
Word will force the value to occupy 2 bytes of EEPROM space, regardless of its value. Any values above 65535 will be truncated to the two least significant bytes. Any value below 255 will be padded to bring the memory count to 2 bytes.
Long will force the value to occupy 3 bytes of EEPROM space, regardless of its value. Any values above 16777215 will be truncated to the three least significant bytes. Any value below 255 will be padded to bring the memory count to 3 bytes.
Dword will force the value to occupy 4 bytes of EEPROM space, regardless of its value. Any value below 65535 will be padded to bring the memory count to 4 bytes. The line of code shown above uses the Dword formatter to ensure all the values in the Edata table occupy 4 bytes of EEPROM space.
Float will force a value to its floating point equivalent, which always takes up 4 bytes of EEPROM space. If all the values in an Edata table are required to occupy the same amount of bytes, then a single formatter will ensure that this happens.
Edata as Dword 100000, 10000, 1000, 100, 10, 1
The above line has the same effect as the formatter previous example using separate Dword formatters, in that all values will occupy 4 bytes, regardless of their value. All four formatters can be used with the as keyword. The example below illustrates the formatters in use.
' Convert a Dword value into a string array
' Using only BASIC commands
' Similar principle to the Str$ command
Device = 18F26K40
' Tell the compiler what device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Dim P10 as Dword
' Power of 10 variable
Dim Cnt as Byte
Dim J as Byte
Dim Value as Dword
' Value to convert
Dim MyString[11] as Byte ' Holds the converted value
Dim bElement as Byte
' Index within the Byte array
Clear
' Clear all RAM before we start
Value = 1234576
' Value to convert
DwordToStr()
' Convert Value to string
HRsoutLn Str MyString
' Display the result
Stop
'-------------------------
' Convert a Dword value into a string array
' Value to convert is placed in 'Value'
' Byte array 'MyString' is built up with the ASCII equivalent
MyWord ()
bElement = 0
J = 0
Repeat
P10 = Eread J * 4
Cnt = 0
While Value >= P10
Value = Value - P10
Inc Cnt
Wend
If Cnt <> 0 Then
MyString[bElement] = Cnt + "0"
Inc bElement
EndIf
Inc J
Until J > 8
MyString[bElement] = Value + "0"
Inc bElement
MyString[bElement] = 0
' Add the null to terminate the string
EndProc
' Edata table is formatted for all 32 bit values.
' Which means each value will require 4 bytes of EEPROM space
Edata as Dword 1000000000, 100000000, 10000000, 1000000,100000,_
10000, 1000, 100, 10
Label names as an address in an Edata table.
If a label's name is used in the list of values in an Edata table, the labels address will be used. This is useful for accessing other tables of data using their address from a lookup table. See example below.
' Display text from two Cdata tables
' Based on their address located in a separate table
Device = 16F877
' Tell the compiler what device to compile for
Declare Xtal = 16
' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Dim wAddress as Word
Dim DataByte as Byte
wAddress = Eread 0
' Locate the address of the first string
Do
' Create an infinite loop
DataByte = Cread wAddress ' Read each character from the Cdata string
If DataByte = 0 Then Break ' Exit if null found
HRsout DataByte
' Display the character
Inc wAddress
' Next character
Loop
' Close the loop
wAddress = Eread 2
' Locate the address of the second string
Do
' Create an infinite loop
DataByte = Cread wAddress ' Read each character from the Cdata string
If DataByte = 0 Then Break' Exit if null found
HRsout DataByte
' Display the character
Inc wAddress
' Next character
Loop
' Close the loop
Stop
'
' Table of address's located in EEPROM
'
Edata as Word String1, String2
String1:
Cdata "Hello",0
String2:
Cdata "World",0