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


    Did you find this post helpful? Yes | No

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

    Not wishing to be rude, but how can 255 iterations be quicker than NCD or the in-line assembler equivalent?

    George

  2. #2
    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?

    Quote Originally Posted by towlerg View Post
    Not wishing to be rude, but how can 255 iterations be quicker than NCD or the in-line assembler equivalent?

    George
    The 255 iterations is just to test the LOOKDOWN2 command line.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

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

    As I see it, . . . . you only need 8 iterations with lookdown and using < or > as it jumps to the first "iteration" that tests as true.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

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

    For those who are curios as to the testing results of NCD vs. Lookdown2 in the context of this discussion.
    I ran the following code in the MPLAB Simulator.

    Code:
    NCDSUB:
        for i = 0 to 255
    	mystatus = i
    	''Start MPLAB SIM Stopwatch
        	mybyte = NCD mystatus
        	''Stop MPLAB SIM Stopwatch
    	next i
        lcdout $FE,1,"NCD: ", idec mybyte
        pause 500
        
        return
        
    LOOKSUB:
    
        for i = 0 to 255
    	mystatus = i
    	''Start MPLAB SIM Stopwatch
        	lookdown2 mystatus, <= [%00000000, %00000001, %00000011, %00000111, %00001111, %00011111, %00111111, %01111111, %11111111], mybyte
        	''Stop MPLAB SIM Stopwatch
        next i
            
        lcdout,1, "Lookdown: ", idec mybyte
        pause 500
    
        return
    and here are the results.

    Name:  NCDTest.png
Views: 1011
Size:  7.7 KB
    Regards,
    TABSoft

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

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

    Lookdown2 makes 2x code over lookup, have you tested to confirm? Thanks
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

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

    Best I can think of is 3 assembler instructions, but more time than 3 cycles of course.
    Copy the port value to a byte, count how many times you shift the byte right until the byte is zero.

    If those times are for a single value,
    I think it would be better than either example to check each bit in a verbose fashion still in the compiler.
    Code:
    bytevar var byte ‘ buffer byte for port
    bitnum var byte ‘ output bit number that was set
    
    bytevar = porta
    
    
    btfsc		_bytevar		,07
    goto labelone
    btfsc		_bytevar		,06
    goto labeltwo
    btfsc		_bytevar		,05
    
    ......

  7. #7
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

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

    Or rotate left until carry is set:
    Code:
    bitcount var byte
    bytebuff var byte
    
    NCD8:
    bytebuff = portX
    clrf	_bitcount
    bcf	status	,C
    findbit:
    incf	_bitcount
    rrf	_bytebuff
    btfss	status	,C
    goto findbit
    return
    It’s been a while for asm, and might not work straight up, but enough to get the idea.
    findbit: is currently an endless loop if the port is zero, so you’d need to check for zero at the beginning.

Similar Threads

  1. Replies: 6
    Last Post: - 18th July 2012, 02:42
  2. Whats the quickest way to set bits?
    By bearpawz in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 26th October 2010, 04:38
  3. Bit set question
    By nverma in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd April 2007, 21:23
  4. set flags within a variable?
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 24th October 2006, 11: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, 08: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