division problem 500000 / 133


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default division problem 500000 / 133

    Hello,

    I need to divide 500000 by my input
    so 500000 / input Ex: 500000/145= 3448
    Obviously this is a 20 bit number.
    tried reducing to 50000, but I get stuck with a decimal, used :
    input1 = 50000 / 145 '=344
    output1= input1 >> 2 '=86

    1) How do I combine these two numbers to create one?
    2) maybe there is another and better way to do it ?

    K

  2. #2
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: division problem 500000 / 133

    Thought I had it,
    but there is something fishy about the shifting i believe..

    input1 = 50000 / 142
    output1= input1 >> 2
    input1= input1 * 10
    output1 = output1 /10
    input1 = input1 +output1

    K

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: division problem 500000 / 133

    Hi,
    If you're running on a 18F series then you could enable support for LONGS in the compiler and simply be done with it. It will increase the code size quite a bit though so I understand if you're trying to avoid it.

    Have you looked at the DIV32 operator? I'm not 100% sure it'll work with a variable but I think it will, try it out:
    Code:
    Dummy  VAR WORD
    Result VAR WORD
    Divider VAR WORD
    
    ' Be aware that Divider must be at least 8 or Result will
    ' overflow and show the wrong result.
    Divider = 145
    
    Dummy = 500*1000            ' Produce intermediate result of 500000
    Result = DIV32 Divider      ' Divide the intermediate result
    /Henrik.

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: division problem 500000 / 133

    Hi,

    I am using the 16F88 chip..
    tried your code but shows 65535 on LCD


    K

  5. #5
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: division problem 500000 / 133

    Here is a snipit form a program I use and it works great:
    '************************************************* *******************
    CALCF_C: 'CALCULATE DEG.F and DEG.C for LM 95231 TEMPERATURE SENSOR READING
    '************************************************* *******************
    CALCF:
    SCRATCH = TEMPREAD * 1000
    TEMPF = DIV32 17777
    TEMPF = TEMPF * 10
    TEMPF = TEMPF + OFFSET
    CALCC:
    SCRATCH = TEMPREAD * 10
    TEMPC = DIV32 32
    RETURN

    Just make sure you don't have any interrupts going on when using DIV32.
    I also think your problem may be that you are using a variable after the DIV32 keyword. Try entering the constant value and see if that doesn't work.
    Last edited by Dave; - 24th May 2013 at 12:01.
    Dave Purola,
    N8NTA
    EN82fn

  6. #6
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: division problem 500000 / 133

    Dummy = 500*1000 ' Produce intermediate result of 500000
    Result = DIV32 133

    lcdout $FE,2, "Duty:", dec Result

    ... still getting 65535 as output

    Thats all I have in the program, no interrupt nothing else !

    then I initialized my parameters this way:
    a var word
    b var word
    a= 500
    b = 1000
    Dummy VAR word
    Result VAR WORD
    Divider con 145

    Dummy = a * b ' Produce intermediate result of 500000
    Result = DIV32 133

    and its now working.. Dummy doesnt like numbers
    Last edited by lerameur; - 25th May 2013 at 15:36.

Similar Threads

  1. // Division Operator
    By Dick Ivers in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 9th September 2011, 14:27
  2. What is wrong with this division?
    By tjkelly in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 5th March 2010, 17:34
  3. Division by 0
    By sirvo in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 4th August 2007, 01:11
  4. Division with some decimal
    By Darklakebridge7 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 10th May 2007, 06:41
  5. division
    By eoasap in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th March 2006, 16:26

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