Reading Input on Port B


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2011
    Posts
    26

    Default Reading Input on Port B

    Hi guys,

    I can read the input on any individual pin on portb like this:

    if (portb.2 = 0) Then do something

    However, if I have successive reads such as this it doesn't work properly:

    if (portb.2 = 0) Then do something1
    if (portb.3 = 0) Then do something2
    if (portb.4 = 0) Then do something3

    Do I need to do something with the hardware in-between reading the pins? I am using a 16f887 40-pin PIC.

    Thanks

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Reading Input on Port B


    The pinout usually give some great hints... AN like ANALOG.. so you want to disable the analog features of the offending pins...and have a look at this thread
    http://www.picbasic.co.uk/forum/showthread.php?t=561
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jun 2011
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Reading Input on Port B

    I am able to read each pin separately without any problems.

    This works:
    if (portb.2 = 0) Then do something

    So does this:
    if (portb.3 = 0) Then do something

    So does this:
    if (portb.4 = 0) Then do something



    However, if I check all three immediately after one another, I am getting funny results. Do I need to let portb 'rest' in-between reads, etc? Is there something I am missing?

    I have set the registers as per the datasheet for the 16f887:

    CM1CON0=%00000000 'disable COMPARATOR 1
    CM2CON0=%00000000 'disable COMPARATOR 2
    trisb = %11111111 'set all pins to input

    Any ideas?

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Reading Input on Port B

    Maybe the victim of R-M-W? I am guessing the do something part may be setting output pins high? If they are on the same port, maybe this is the problem. I like to grab the input port and put it in a variable. Then I can check the bits in the variable and do something -or not.

    Just suggestions, I am not sure of the problem.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

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


    Did you find this post helpful? Yes | No

    Default Re: Reading Input on Port B

    Maybe the victim of R-M-W?
    Nope.

    Pins that power-up as analog inputs, when read (assuming it's a digital input), will read ZERO unless you have disabled the analog peripheral on the pin you're reading.

    TIP: Look at ANSELH.

    I'll bet, if you set all of these pins to logic 1 (externaly), they still all read zero until you disable the analog periperal!
    Last edited by Bruce; - 30th July 2011 at 23:21.
    Regards,

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

  6. #6
    Join Date
    Jun 2011
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Re: Reading Input on Port B

    I have set:

    ANSELH=%00000000

    If I check for say a portb.3 button press, it works fine. If I check for a portb.5 button press it works fine.

    However, if I check for say portb.3 and then portb.5 straight away, sometimes portb.5 comes back as 1 instead of 0, etc.

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


    Did you find this post helpful? Yes | No

    Default Re: Reading Input on Port B

    Post your code. It's tough to help troubleshoot what we can't see..
    Regards,

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

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Reading Input on Port B

    But, in 2011, everything's possible
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

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