Newbie question on reading port staus


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2009
    Posts
    50

    Default Newbie question on reading port staus

    I am sure this is real simple:

    OK, so I have inputs on PortB.0 through PortB.7.

    How can I read the status of this as a whole?

    For example if PortB.0, PortB.1 and PortB.3 are all HIGH then I want to retun the digit "11"....and so forth from 0-127.

    Thanks!

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: Newbie question on reading port staus

    Hi,
    Just read the Port register:
    Code:
    myValue = PortB
    That's it.

    /Henrik.

    EDIT: Make sure to check and setup stuff like ADCON, CMCON for any pins you're going to use.

  3. #3
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: Newbie question on reading port staus

    Ah, if only all things in life were this easy!
    Thanks!!!

  4. #4
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: Newbie question on reading port staus

    Now, how do I get the value of the Port if the port is less than 8 bits?

    Thanks!

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


    Did you find this post helpful? Yes | No

    Default Re: Newbie question on reading port staus

    Exactly the same way as before.
    On a 16F877, for example, PortA is 6bits. Reading it will return a byte with the state of the port pins in the lower 6 bits, the two high bits will always read zero.
    PortE on the 16F877 is only 3bits. Reading it will return a byte with the state of the port pins in the lower 3 bits, the upper 5 bits will always read zero.

    If you look at the register summary in the datasheet for the 16F877 (or whatever device you're actually using) it's all pretty clear.

    /Henrik.

  6. #6
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: Newbie question on reading port staus

    Yes, that makes good sense. But what if I only use say 4 bits of the PortA port in my code and make the other PortA bits as outputs? Would reading the port give me the correct result?
    :-)

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


    Did you find this post helpful? Yes | No

    Default Re: Newbie question on reading port staus

    it will also return the state of the output. Nothing hard to solve through a bitwise operation or simply by breading the bits you need.
    Steve

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

  8. #8
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: Newbie question on reading port staus

    Awesome!! THX!!

Similar Threads

  1. Newbie with an I2C question
    By andywpg in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd February 2012, 21:30
  2. Reading Input on Port B
    By bodgetts in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 31st July 2011, 02:01
  3. Reading the port - Little help needed
    By Megahertz in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th June 2011, 20:15
  4. Analog port newbie questions
    By rngd in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 10th March 2008, 12:38
  5. Is this possible....Newbie question...
    By CosRush in forum mel PIC BASIC
    Replies: 5
    Last Post: - 15th November 2005, 20:46

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