PDA

View Full Version : Storing Decimal Value in Eeprom?

Carl Lawhon
01-07-2009, 05:08 AM
Is there any way I can store a decimal value in the eeprom? For example, how could I store something like 123.456?

Erik Friesen
01-07-2009, 05:53 AM
Using a modified version of James burrows/Javalins i2c code it would be something like this

var
long test
i2c:"i2c"
pub

The above examples require some modification when used with James code. In my code the 8 stands for the number of bytes to send.

I probably haven't answered your question directly but this is a way to store two longs in memory.

mcstar
01-07-2009, 05:56 AM
You need to convert your number to a floating point number first, then save it using the code that Erik posted or something similar. Floating point numbers are represented as a long in memory with the first bit set.

John Abshier
01-07-2009, 06:01 AM
You need to know what 123.456 actually is. If it is a floating point number, it occupies 4 bytes. You have 2 options to writing it to eeprom. First is to write 4 bytes, that is the floating point number. The other is to convert it to a string representation and then write the string, 8 bytes with a \$00 byte added to terminate the string. If this is data you pulled off the GPS, it may already be a string.

John Abshier

Paul Baker
01-07-2009, 08:24 AM
Another way to do it is to use BCD encoding where each digit is 4 bits, or 2 numbers per byte. Valid digits are 0-9, you can encode the decimal point as one of the unused values (say \$A), then to indicate the end of the number you can use another unused value (like \$F). This scheme maintains the simplicity of strings but encodes it at double the density. The number 123.456 would be stored as \$12, \$3A, \$45, \$6F.

You can go even fancier by assigning other unused values, like - to \$B and E to \$E. So a scientific number like -1.74E-6 would be \$B1, \$A7, \$4E, \$B6, \$F0. The last digit is a "don't care", it's just padding to make the number take 8 bits.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)

Post Edited (Paul Baker) : 1/7/2009 1:39:58 AM GMT

grasshopper
01-07-2009, 10:31 AM
I just store it like any other variable using the BasicObject. using this

var
long FloatingData 'floating point number

BasicObj.Write_HighMem(1200,FloatingData,4)

Carl Lawhon
01-08-2009, 02:03 AM
I hate to be noobish, but I'm still a little confused. Anyone want to hold my hand through it using basic_i2c_driver and the example value 123.456? (Thanks to everyone who has posted above, I certainly don't mean to say that you haven't been helpful).

Carl Lawhon

Mike Green
01-08-2009, 02:08 AM
You haven't said yet what form the number is in. Is it a string of characters? If so, what marks the end of the string. Is there a zero byte or carriage return (13) or do you have a known length for the string? Is it a floating point number (stored in a long)? First you have to specify what you have, then we can suggest a way to get it copied to an EEPROM.

Carl Lawhon
01-08-2009, 02:54 AM
Ah. It is a floating point value converted from the gps's deg, mn, and minfrac strings.

Mike Green
01-08-2009, 03:26 AM
In that case, the value fits in a long variable and you can use the read/write long methods in the Basic_I2C_Driver. I don't have a copy of the driver handy, but you'll see simplified read/write routines for words and longs. Important things to remember:

1) The 4 bytes of the long have to fit in an EEPROM "page". The best way to do this is simply to require the EEPROM address to be a multiple of 4.

2) A write operation takes roughly 5ms. You can put in a 5ms wait like WAITCNT(CLKFREQ/200+CNT) or you can use the test for completion of the write that's shown in the comments at the beginning of the driver.

3) The pin # of the SCL signal can be specified separately or combined with the EEPROM address as described in the comments. The pin # of the SDA signal is always the next pin.

grasshopper
01-08-2009, 03:41 AM
You could do this
obex.parallax.com/objects/30/ (http://obex.parallax.com/objects/30/)

Var
LONG GPS_DATA

Obj
EEPROM : "BS2_Function"

Pub Main
EEprom.Start(31,30)
GPS_DATA := 123.325
StoreEEProm

Pub StoreEEProm

'Write_HighMem(address, value, size) This will write to the Variable called GPS_DAta to an address
EEprom.Write_HighMem(10,GPS_DATA , 4)

Post Edited (grasshopper) : 1/7/2009 8:47:20 PM GMT