Shop OBEX P1 Docs P2 Docs Learn Events
Incrementing 32 bits — Parallax Forums

Incrementing 32 bits

Scott4Scott4 Posts: 45
edited 2012-10-10 09:59 in BASIC Stamp
I have a BS2px. I need to increment 4 bytes (in a binary fashion) in the Scratch Pad RAM to accumulate a 32 bit binary number that I will ultimately convert to decimal to display miles for an odometer application.

Given 4 sequential bytes in the Scratch Pad Ram
byte1
byte2
byte3
byte4

Pseudocode would look something like this:

Shiftin 8 bits
Add the 8 bits to byte4
If byte4=$FF then (byte4 has overflowed and the balance needs to be carried and added to byte3)
Go back and get the next 8 bits

Anyone have some sample code to do this?

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-10-08 11:48
    The correct algorithm is as follows (assuming the new addend is eight bits only):
    1. Read byte 4.
    2. Add the new 8-bit value to it.
    3. If the sum is less than the original value of byte 4, go to step 4; otherwise, quit.
    4. Add one to byte 3. If the sum is zero, go to step 5; otherwise, quit.
    5. Add one to byte 2. If the sum is zero, go to step 6; otherwise, quit.
    6. Add one to byte 1. If the sum is zero, sell the car. It has way too many miles on it. :)

    -Phil
  • Scott4Scott4 Posts: 45
    edited 2012-10-08 11:55
    Thanks Phil, love the humor too! -Scott
  • Scott4Scott4 Posts: 45
    edited 2012-10-09 13:15
    Phil, will you review the attached flow chart to see if I have captured your intent. Thanks -Scott
    552 x 730 - 150K
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-10-09 20:17
    'Looks okay. Now, have you figured out how you will display the resulting 32-bit value?

    -Phil
  • Scott4Scott4 Posts: 45
    edited 2012-10-10 08:01
    :-) You caught me! :-)

    I'm in the process of designing the program and have not gotten to that part yet.
    I have a handle on these first 3 items:

    1) Count the pulses from the 8 magnets attached to the wheel rotating in front of the hall effect device. (done in hardware with a binary counter so no pulses are missed while the BS2px is doing other things)
    2) Shift the count to the BS2px (using hardware shift register and SHIFTIN command)
    3) Add the count to the accumulated total pulses. (32 bit scratch pad register)
    next...
    4) Divide the 32 bit register by the predetermined number of pulses per mile to get total miles.
    5) Convert "total miles" binary to BCD to display total miles.

    If you have design ideas, could help or point me to help with 32 bit divide and 32 bit to BCD conversion I would appreciate it. -Scott
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-10-10 09:59
    For the 32-bit divide, Tracy Allen is your friend:

    As an alternative, you could do the accumulation differently by never letting the lower 16 bits exceed 9999. If, when you add to the lower 16 bits, the sum is 10000 or greater, subtract 10000 and add one to the upper 16 bits. That way, you can display both the upper and lower numbers using the DEC4 operator, and the result will be displayed correctly.

    -Phil
Sign In or Register to comment.