Read state of specific bit (i.ex. of a PORT)


+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french part)
    Posts
    758

    Default Read state of specific bit (i.ex. of a PORT)

    Hi There,

    I'm "reading" a 3x4 keypad matrix and want to show the user he is currently pressing a button.

    So I light up a LED while any key is pressed and shut if OFF when the key is released and it works fine.

    I'm using this piece of code as a debouncer (that keypad must have yo-yo contacts! ):
    Code:
    WHILE PORTB.4 = 0 : WEND
    WHILE PORTB.5 = 0 : WEND
    WHILE PORTB.6 = 0 : WEND
    WHILE PORTB.7 = 0 : WEND
    Is there a way to make this a little bit better?

    I mean, can I read the four ports B and evaluate them at once instead of four lines of code?

    I tried several things like
    Code:
    WHILE PORTB & %11110000 = 0 : WEND
    but I can't get it working

    Any idea how to do this please?
    Roger

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,748

    Default Re: Read state of specific bit (i.ex. of a PORT)

    if key active high

    WHILE PORTB >15 : WEND

    or

    WHILE (PORTB&$f0) : WEND


    or if active low

    WHILE (PORTB < 240) : WEND
    Last edited by richard; - 31st October 2020 at 13:45.
    This is more entertaining than Free to Air TV

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french part)
    Posts
    758

    Default Re: Read state of specific bit (i.ex. of a PORT)

    Thanks Richard

    Nevertheless, I still don't get it.

    Is this statment (version 2013-03-06) really correct?
    Attached Images Attached Images  
    Roger

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    1,748

    Default Re: Read state of specific bit (i.ex. of a PORT)

    yes absolutely
    result equals the low nibble of portb as read, the high nibble is masked off
    This is more entertaining than Free to Air TV

Similar Threads

  1. Replies: 11
    Last Post: - 13th January 2013, 11:25
  2. How can I read a port's status
    By lecoind in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd October 2009, 10:54
  3. PIN state VS Variable read
    By cburtis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th December 2007, 12:54
  4. Replies: 2
    Last Post: - 27th March 2007, 12:48
  5. How to read data 4 bit
    By chai98a in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 11th December 2006, 06:07

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts