Shop OBEX P1 Docs P2 Docs Learn Events
Storing Decimal Value in Eeprom? — Parallax Forums

Storing Decimal Value in Eeprom?

Carl LawhonCarl Lawhon Posts: 36
edited 2009-01-07 20:41 in Propeller 1
Is there any way I can store a decimal value in the eeprom? For example, how could I store something like 123.456?

Comments

  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-01-06 22:53
    Using a modified version of James burrows/Javalins i2c code it would be something like this

    var
    long  test
    i2c:"i2c"
    pub
        i2c.writepage(eepromaddress,0,8,@test,8,1)'write data to eeprom
    
    'to read
       i2c.readpage(eepromaddress,0,8,@test,8,1)
    
    


    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.
  • mcstarmcstar Posts: 144
    edited 2009-01-06 22:56
    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 AbshierJohn Abshier Posts: 1,116
    edited 2009-01-06 23:01
    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 BakerPaul Baker Posts: 6,351
    edited 2009-01-07 01:24
    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


    Post Edited (Paul Baker) : 1/7/2009 1:39:58 AM GMT
  • grasshoppergrasshopper Posts: 438
    edited 2009-01-07 03:31
    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 LawhonCarl Lawhon Posts: 36
    edited 2009-01-07 19:03
    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 GreenMike Green Posts: 23,101
    edited 2009-01-07 19:08
    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 LawhonCarl Lawhon Posts: 36
    edited 2009-01-07 19:54
    Ah. It is a floating point value converted from the gps's deg, mn, and minfrac strings.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-01-07 20:26
    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.
  • grasshoppergrasshopper Posts: 438
    edited 2009-01-07 20:41
    You could do this
    object is at link
    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
Sign In or Register to comment.