STOR-ing word values in EEPROM
Hi,
I am using the stor object on the EM1202 to store equipment data (model number, serial number, IP, port, firmware revision etc) in EEPROM. This is fine as I can store and recall all the data as strings.
However, I now want to store a whole set of values from a 10-bit A-to-D converter into EEPROM. I have the values in 16-bit word variables and want to store them to EEPROM as 2-bytes each, but stor.setdata and stor.getdata accept strings only. If I convert to strings using the hex function, I end up using minimum 4-bytes, or double the space!
Can anyone suggest how I can easily write/read these values in only 2-bytes each?
Thanks.
I am using the stor object on the EM1202 to store equipment data (model number, serial number, IP, port, firmware revision etc) in EEPROM. This is fine as I can store and recall all the data as strings.
However, I now want to store a whole set of values from a 10-bit A-to-D converter into EEPROM. I have the values in 16-bit word variables and want to store them to EEPROM as 2-bytes each, but stor.setdata and stor.getdata accept strings only. If I convert to strings using the hex function, I end up using minimum 4-bytes, or double the space!
Can anyone suggest how I can easily write/read these values in only 2-bytes each?
Thanks.
1 person has this question
I have this question, too!
Tell me when someone answers.
The more people who ask this question, the more it gets noticed.
The more people who ask this question, the more it gets noticed.
-
Inappropriate?Hi,
Use the chr() function to encode a byte value to a char.
Then store it on the eeprom
Afterwards use the asc() function to restore the value back from a char.
If you are able to use long variables, ex. 2x 16 bytes data you can also use ddval and ddstr to convert 4 bytes to 4 chars at the same time.
Usefull to convert and store ip adresses.
1 person says
this answers the question
-
Inappropriate?Hello again Wolftec,
I have used ddstr and ddval to store the IP adress in EEPROM, also firmware revision (in format #.#.#), but I don't understand if this can be used here? I have word values of 0-1023 but I thought ddstr/ddval would only work with byte variables?
As you suggest, I have managed to store word_variable in two bytes:
low_byte=word_variable AND &b11111111
high_byte=word_variable/256
string=chr(low_byte)+chr(high_byte)
stor.setdata(string, address)
This is the shortest way I can see to do it. There really should be a method to write mutli-byte data to EEPROM without converting to strings!
To read back from EEPROM:
low_byte = asc(stor.getdata(address,1))
high_byte= asc(stor.getdata(address+1,1))
word_variable=(256*high_byte)+low_byte
Thanks for your help. I'd be interested in any more information on the use of ddstr and ddval, as these don't appear to be well documented?
Hugh
I’m thankful
-
Inappropriate?Thinking of it,
If your going to work with byte or long variables, asc() and chr() are better for this kind of work
ddval and ddstr are better with values that are in strings. such as tcp/ip adresses.
Hope this has helped you.
1 person says
this answers the question
Loading Profile...




