Troubles with I2C_Write


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    While I have your attention, I'm puzzling over how to efficiently do a 10-bit by 10-bit division (which will yield a value 0<x<1) then multiply it again by 1023 to get a number (0<=x<=1023). I'd prefer not to use LONG values as they eat up precious RAM, but I also want to keep it reasonably quick. I have plenty of words set up, but no LONGs.
    Here's the math:
    a, b, and x are WORDs
    x=1023*(a-b)/(a+b)
    Any thoughts?
    EDIT: Oh. Ha ha. I don't even have LONGs to work with. Yeah... that makes a difference now, doesn't it?
    The only thing I can conceive of is using a two-fold mechanism that combines this:
    Code:
    'Shift RESULT left 10 times
    FOR Iter=1 to 10
    	RESULT=RESULT<<1
    NEXT
    with this:
    Code:
    'RESULT= B / A
    RESULT=0
    WHILE B>A
    	B=B-A
    	RESULT=RESULT+1
    WEND
    The trick is to keep the working value inside 16-bits, but inside 10 bits at the very end.
    Last edited by The Altruist; - 23rd March 2010 at 02:23.

  2. #2
    Join Date
    Aug 2009
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    For those of you wondering, this algorithm gives decently accurate 10-bit ratios with a Big O of (n^2). I hope some one finds it useful.
    Bear in mind that the Numerator MUST be smaller than the Denominator, and that the denominator bit-shifted left to be bigger than the Power of 2 you're trying to fit it in. Therefore, don't change the 1024 to 16384 and expect it to work (while still using 16-bit WORDs).

    Code:
    TenBitRatio:
    		Result=0
    		while Denominator<1024 
    			Numerator=Numerator<<1
    			Denominator=Denominator<<1
    		WEND
    		while	Denominator>0
    			Digit=0
    			while	Numerator>Denominator
    				Numerator	=	Numerator	-	Denominator
    				Digit=Digit+1
    			WEND
    			Denominator	=	Denominator>>1
    			Result	=	Result	<<	1
    			Result	=	Result	+	Digit
    		WEND
    Last edited by The Altruist; - 23rd March 2010 at 23:59.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts