converting long integer


Closed Thread
Results 1 to 6 of 6
  1. #1
    jra's Avatar
    jra Guest

    Default converting long integer

    Hello,

    Wondering if anyone can provide an algorithm for converting
    a long integer ( up to 32 bit ) into two 16 bit words. The integer
    value will be sent serially to the pic. I then need to convert that value into two word variables... a high word and a low word.

    This is easy enough to do in Visual basic on a pc but I need to do it on the pic.

    Is there some binary arithmetic trick or hex conversion i can use ?

    Thanks

    Jay

  2. #2
    carl_schell's Avatar
    carl_schell Guest


    Did you find this post helpful? Yes | No

    Default 32-Bit Math

    Hi Jay -

    It is hard for me to see exactly what you are looking for, but one thought that crosses my mind is this...but maybe this will give you an idea to solve your particular problem...I used a similar routine to solve a problem where I needed to divide using 1 million...so you multiply two larger numbers to give you this, which you cannot store, but you can opperate on...let me demonstrate briefly

    You have the ability to multiply two 16-bit variables...this then places the full 32-bit result internally...you can the use the DIV32 command (you must ensure than no other operations occur before using the DIV32 command and should disable any interrupts you may be using)

    ie:
    '2^16 => 65535 so this is the largest number availible you can store in a variable
    'However!...you can deal with larger numbers easily

    j var word
    k var word
    dummy var word


    j = 1000

    Disable

    dummy = j * j ' 1,000 * 1,000 = 1,000,000
    k = DIV32 50 ' 1,000,000 / 50 = 20,000

    Enable

    Sorry, I am out of time for now (late for my daughter's swimming lesson), but start with this and look at the manual under the DIV32 command for more info...and write back as to any further ideas...i will adress them when i get more time...

    Carl

  3. #3
    jra's Avatar
    jra Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks for the reply Carl

    I am aware of the DIV32 command . Actually what i am trying to do is use the pic as a pulse generator. I want to serially input a decimal value between 0 and say 100,000,000 and have the pic generate square wave output pulses equal to the number that was serially input. So, If the pc sends 789267 to the pic it spits out that many pulses.

    This is simple if i had 32 bit variables.

    I think I have figured out a simple solution. I'll post the code
    if it works.

    However, If anyone has a solution it would be appreciated

    Thanks

    Jay

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Why not just use one word value as the multiplier for the second in a FOR NEXT loop, and not even mess with large numbers?

    Receive two words from your PC, then send (A*B) number of pulses.
    Code:
    A VAR WORD
    B VAR WORD
    X VAR WORD ' Outter loop
    Y VAR WORD ' Inner loop
    
    Out_Pulse:
    FOR X = 0 TO A ' Send A*B pulses
      FOR Y = 0 TO B
        SEND PULSE HERE
      NEXT Y
    NEXT X
    GOTO ?
    You have up to 65,536 * 65,536 pulses without a drop of math.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    jra's Avatar
    jra Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce,

    I have already considered this solution but it's a pain in the neck for the end user. I don't want the user to have to convert the number of pulses into 2 16 bit words. They may be sending pulse info via hyperterminal. Just want to send a decimal number of pulses for ease of use.

    I have come up with a simple solution.

    Thanks for your help

    Jay

  6. #6
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    Perhaps this will be to your liking.

    /Ingvar


    @ device pic16F876, hs_osc

    DEFINE OSC 20

    TxPin VAR PortA.0
    RxPin VAR PortA.1
    PulsPin VAR PortA.2

    HiWord VAR WORD
    LoWord VAR WORD

    Dummy1 VAR WORD
    Dummy2 VAR WORD

    Dummy VAR BYTE

    ADCON1 = 7
    TrisA = %11111010
    WHILE 1
    SEROUT2 TxPin,84,["Enter number of pulses(9 digits)!",13,10]
    'Recieve Hi and Low part of pulsnumber
    'Number must always be transmitted as 9 digits(leading zeroes) and
    'not greater than 655359999.
    SERIN2 RxPin,84,[DEC5 HiWord]
    SERIN2 RxPin,84,[DEC4 LoWord]
    'Debuginfo
    SEROUT2 TxPin,84,["Sending ",DEC5 HiWord, DEC4 LoWord, " pulses.",13,10]

    'Send the pulses
    For Dummy1 = 1 to HiWord
    For Dummy2 = 0 to 9999
    PulsPin = 1
    PulsPin = 0
    NEXT
    NEXT
    For Dummy1 = 1 to LoWord
    PulsPin = 1
    PulsPin = 0
    NEXT
    WEND


    END

Similar Threads

  1. EasyHID and USB for Newbies??
    By Squibcakes in forum USB
    Replies: 68
    Last Post: - 26th November 2015, 22:41
  2. High Resolution Timer & Speed Calculator
    By WOZZY-2010 in forum Code Examples
    Replies: 4
    Last Post: - 7th February 2010, 16:45
  3. communication on long distances
    By iugmoh in forum General
    Replies: 3
    Last Post: - 8th May 2009, 22:27
  4. Serial communication fails after long pause
    By brid0030 in forum General
    Replies: 4
    Last Post: - 13th February 2008, 18:56
  5. Long varible strings
    By flashg in forum Serial
    Replies: 0
    Last Post: - 14th October 2006, 04:40

Members who have read this thread : 1

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