Help with number conversion.
mynet43
Posts: 644
I have an application that requires me to keep a 64-bit positive number. It's the encoder count from an electric motor, at 5000 steps per rev.
I need to be able to do three things:
1. Increment the 64-bit number by some number, across the 32 bit boundary.
2. Convert a 20 character string of decimal numbers to a 64 bit number.
3. Convert the 64-bit number to a 20 byte character string of decimal numbers.
Most of this doesn't seem too difficult, but I'm not sure what to do when the high bit of the least significant 32 bit long is set. I want to treat it as only a positive number that flows into the higher long as one 64 bit number. When the 64 bit number fills up, it should start over at zero.
Can someone help me with the spin code needed to do these three things?
Thank you for your help and support.
Jim
I need to be able to do three things:
1. Increment the 64-bit number by some number, across the 32 bit boundary.
2. Convert a 20 character string of decimal numbers to a 64 bit number.
3. Convert the 64-bit number to a 20 byte character string of decimal numbers.
Most of this doesn't seem too difficult, but I'm not sure what to do when the high bit of the least significant 32 bit long is set. I want to treat it as only a positive number that flows into the higher long as one 64 bit number. When the 64 bit number fills up, it should start over at zero.
Can someone help me with the spin code needed to do these three things?
Thank you for your help and support.
Jim
Comments
John Abshier
I like your thinking It probably doesn't need to be that big because it's tracking a motor going less than 100 RPM. This would allow about 67 hours of continuous operation.
It's in the specification I'm working with, but I may try to contact them tomorrow to see if they really need that much capacity.
I would still like to know how to do the math. So if anyone knows how to work with 64 bit numbers, please let me know.
Thanks for your help.
Jim
Long1 would be the least significant bits of the 64 bit number.
Long2 would be the most significant bits of the 64 bit number.
Long3 would be the original value of the least significant bits of the 64 bit number.
Do the following:
Copy long1 to long3
Add the increment value to long1
If long1 is less than long3 add 1 to long2
Works in PASM or Spin
Sorry but I'm still confused. I was all ready to try kwinn's solution. Actually I do need it in PASM, as that's where I'm counting the encoder ticks.
This seems like a great solution, BUT. Kuroneko, are you saying it won't work because the high bit is set and it will get treated as a negative number? That's the whole point of what I'm trying to figure out.
I'm not sure what to do with a 32 bit long when the hi bit gets set. I'm sure this is not rocket science, but I'm missing the concept right now. If I add '1' to $7FFF_FFFF, do I get a positive or negative number. If I add '2' to it, do I get a carry set and a value of 1, etc. If I add 1 to it twice, do I get 1+(-1) = 0?
Would someone with a more organized brain lay this out so I can understand it?
Let's assume I'm doing it in the encoder counting routine, where the 64-bit number is always either incremented or decremented by 1. This is all I'm looking for.
Thank you all for your help and support.
Jim
In PASM, you would have 2 32 bit registers.
Increment:
add one to the lower 32 bit value. If the addition results in an over flow, increment the upper 32 bit register. The 64 bit values rolled over If the upper 32 bit number overflows.
Decrement:
Subtract one until the lower 32 bit value is less than zero (sighed compare). Then subtract one from the upper 32 bit number.
You can find objects in the OBEX for converting string to numbers and numbers to strings.
I think I get it. Just the wc in assembly will take care of the add and subtract. That's easy. Thanks.
Also, I know how to convert back and forth to characters with a 32 bit long in spin. It looks like this may be trickier with a 64 bit number. Any ideas here? I don't think this is covered in the OBEX.
Thanks again for your help!
Jim