Incrementing 32 bits
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?
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
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
-Phil
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
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