Quickest way to get highest set bit on port in to a variable?


Closed Thread
Results 1 to 29 of 29

Hybrid View

  1. #1
    Join Date
    Dec 2014
    Posts
    12


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by towlerg View Post
    What PIC device?
    It´s a PIC18F45K80

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    sorry can't write PIC assembler but something like

    WReg= 8
    Rotate Left f through Carry, 1
    jump on carry, done
    dec WReg
    Rotate Left f through Carry, 1
    jump on carry, done
    etc...

    or maybe BTFSC - BitTest, skip if clear

    George

  3. #3
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    To repeat, look up NCD in the manual. I think it does what you want.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Except he wants the fastest not the easiest.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    This is what the NCD routine looks like in the pbppic18.lib file when stripping out the non runtime stuff:
    Code:
    ;****************************************************************
    ;* NCD        : Priority encode                                 *
    ;*                                                              *
    ;* Input      : R0 = 16 bit value                               *
    ;* Output     : W = bit number                                  *
    ;*                                                              *
    ;* Notes      :                                                 *
    ;****************************************************************
    
    NCD     clrf    R0 + 1
    NCDL    movwf   R0
            movlw   17              ; Set result
    ncdloop addlw   -1              ; Count down result - sets C so loop will end
            rlcf    R0, F           ; Shift upper bit to carry
            rlcf    R0 + 1, F
            bnc     ncdloop         ; If carry set then done
            goto    DUNN
    I'll say what I usually say which is that I pretty much suck at assembly language stuff but that looks pretty tight to me. Obviously the execution time will vary depending on how many times it has to iterate thru the loop before it finds a bit that is set. Then there are helper macros which puts the byte/Word in question into the correct register (R0) and so on, if interested they are in the pbppic18.mac file.

    Is there a faster way? Perhaps but I'd give NCD a try and get some performance data on that first - otherwise you don't know what to beat (or if you even need to beet it) - IMHO of course.

    /Henrik.

  6. #6
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    704


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Like Archangel mentioned above, you should also try the LOOKUP command. This command takes only a few cycles to execute.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Henric, never the less, not as quick as 8 rotates and 8 jump on carry in-line. Worst case, with no bits set in the target thats only 16 cycles.

    rscor01, I didn't realize LOOKUP would do that. How would that go?

    George

Similar Threads

  1. Replies: 6
    Last Post: - 18th July 2012, 03:42
  2. Whats the quickest way to set bits?
    By bearpawz in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 26th October 2010, 05:38
  3. Bit set question
    By nverma in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd April 2007, 22:23
  4. set flags within a variable?
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 24th October 2006, 12:07
  5. How do I to set one bit in a register?
    By jessey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th February 2006, 09:43

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