piclist 2001\04\06\105826a >
Thread: Rounding to closest 1's multiple, code enclosed :)
www.piclist.com/techref/microchip/devices.htm?key=pic
picon face BY : Bob Ammerman email (remove spam text)



Tony,

I hate to do this to you, but here is simpler(?) way to round to nearest 5:

Given 25 bit number HI:MID:LO

Its value is

LO + 256*MID + 65536*HI

Which is equal to

LO+255*MID+MID+65535*HI+HI

But 255*MID and 65535*HI are both multiples of 5, so:

LO+MID+HI == HI:MID:LO (mod 5)

This can be at most a 10 bit number, let us call it H2:L2

now we can:

   movf    L2,W,A
   andlw  0x0F
   movwf ONES,A

   swapf L2,W,A
   andlw 0x0F
   btfsc   L2,4
   addlw 5        ; worth 6, not 1

   btfsc    H2,0
   addlw    6
   btfsc    H2,1
   addlw    2

and Bob's your uncle!

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

--
http://www.piclist.com hint: To leave the PICList
spam_OUTpiclist-unsubscribe-requestspam_OUTspamspamBeGonemitvma.mit.edu


<001301c0bea9$992eb760$e000000a@CDS> 7bit

See also: www.piclist.com/techref/microchip/devices.htm?key=pic
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Rounding to closest 1's multiple, code enclosed :)

month overview.

new search...