Shop OBEX P1 Docs P2 Docs Learn Events
adding a values in eeprom — Parallax Forums

adding a values in eeprom

MichaelDeFalcoMichaelDeFalco Posts: 19
edited 2007-07-31 16:37 in BASIC Stamp
Hello All:

I am trying to figure out how to add a count value to an eeprom location in the BS2.
I am using the write command (and read command).
When my value is greater than 255 the memory location rolls over to zero.
What is the simplest way to utilize·many locations?
Or is the best way to write hex numbers?
I need to count to 1 million.
I understand that each location can only be written to 10 miilion times-but this will outlast my hardware by many times.

Thanks...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
M.D.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-31 03:55
    Have a look at this website www.emesystems.com. Particularly the BS2 notes on multiple precision arithmetic. You'll need 3 bytes to represent numbers up to about 16 million. When you increment from 255 to 0, carry a 1 to the next byte. When that one increments from 255 to 0, carry a 1 to the 3rd byte.
  • Terry GTerry G Posts: 2
    edited 2007-07-31 04:01
    You can store the Highbyte and Lowbyte of a word variable into EEPROM. If you change the variable that you store your count in from a byte (8bit) to a word (16 bit): "VAR Word" instead of "VAR Byte" , your variable for COUNT will be able to store a maximum count of 65535 or $FFFF in hex. Unfortunately, this is the largest single value that can be expressed using 16 bits and the BS2 is a 16 bit processor. You can, with some programming, create an index (word variable also) that will tell you how many times your counter has turned over. If you set your counter to turn over at say 1,000 and increment your index each time the counter turns over, the index will represent how many thousands to add to the value in your count variable to get the total count. Using this technique it would be possible to store a count of 65,535,999 using 4 bytes of EEPROM. As far as I know, with the BS2 there is no way to express a single value larger than 65535 without the use of indexing and the need for "external" math.
    The maximum count that can be expressed with 32 bits is $FFFFFFFF in hex or 4,294,836,225 in decimal. Sounds like the Propeller has what you need. The prop can express single values in 32 bits known as longs which consist of 2 words or 8 bytes. The propeller also has eight independent 32 bit processors, one of which could do the counting while the rest perform independent and even unrelated tasks.

    If the indexing scheme sound interesting to you, reply and I will send you some code that demonstrates and better explains this technique.

    I'm sure that some of the Parallax programming Gurus will slap me silly and have a better way to approach this problem. The last one to answer one of my posts referred to himself as a teacher, and to me as ignorant.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-31 05:25
    Here's a simple subroutine to increment the 3 byte number in EEPROM locations 0-2 with the least significant byte in location 0.
    addOne:
      for i = 0 to 2              ' This can be extended to more bytes if needed
        read i,temp
        temp = temp + 1     ' Increment the location, then check for a carry
        write i,temp
        if temp > 0 then return
      next                           ' Carry a 1 to the next byte unless this is the last byte
      return
    
    
  • MichaelDeFalcoMichaelDeFalco Posts: 19
    edited 2007-07-31 16:04
    Mr. Green:

    Thank you for your concise and practical wisdom.

    Looks like the simplest way for my app.

    Appreciate it-also great info. from eme...





    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    M.D.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-31 16:37
    I forget to mention that temp has to be defined as a byte for this to work.
Sign In or Register to comment.