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

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

    Hi.
    I have eight sensors with digital output connected to a port and now I want to in the quickest way get only the highest set bit in to a variable.

    if port = 110000
    I want var = 000000

    if port = 001111
    I want var = 001000

    So how can I do this in quickest(least amount of clock cycles) possible way?

    Regards
    /Matias

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

    look at NCD command to do what you asked, checkout AND && for your example, search for MASK: http://www.picbasic.co.uk/forum/show...highlight=mask
    or you could use a lookup table
    Last edited by Archangel; - 1st March 2015 at 09:59.
    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.

  3. #3


    Did you find this post helpful? Yes | No

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

    What PIC device?

  4. #4
    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

  5. #5


    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

  6. #6
    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.

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