Need help isolating 1/2 portb


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    74

    Default Need help isolating 1/2 portb

    I am using a pic 16f628 Portb.0 - portb3 as outputs and portb4 - portb7 as inputs. I have set the trisb correctly but cant figure out how to read the inputs only. Can someone give me some ideas
    Thanks

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


    Did you find this post helpful? Yes | No

    Default

    You have several options depending on what you application requires.

    X VAR BYTE
    OPTION_REG.7 = 0 ' Pull-ups on for inputs
    TRISB = %11110000

    ' Shift upper 4-bits into lower 4-bit positions.
    X = PORTB >> 4 ' X returns 0 to 15

    ' Mask lower nibble from result.
    X = PORTB & %11110000 ' X returns upper 4-bits, 0's for lower.

    ' Test individual bits one by one.
    IF PORTB.7 = ? THEN
    IF PORTB.6 = ? THEN, etc,,,
    Regards,

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

  3. #3
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce

    I worked out another way to do it also, but no sure if it is correct but seems to work ok for me
    b0=portb
    b1=b0 & %11110000
    b2=b1/16

    also can you please explain
    OPTION_REG.7 = 0 ' Pull-ups on for inputs

    I am currently using external resistors for pull-ups and I would like to get rid of them if I can

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


    Did you find this post helpful? Yes | No

    Default


    I worked out another way to do it also, but no sure if it is correct but seems to work ok for me
    b0=portb
    b1=b0 & %11110000
    b2=b1/16
    That will definitely work but, b0 = PORTB >> 4 gives you the same result, uses less program code space, and saves you 2 RAM variables.

    also can you please explain
    OPTION_REG.7 = 0 ' Pull-ups on for inputs
    I am currently using external resistors for pull-ups and I would like to get rid of them if I can
    On most PIC's (check your datasheet) bit #7 of the OPTION_REG register turns ON or OFF PORTB internal pull-ups. OPTION_REG.7 = 0 turns them ON. OPTION_REG.7 = 1 turns them OFF.

    PORTB pins set to inputs will have the weak internal pull-ups. PORTB pins configured as outputs are not affected.
    Regards,

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

  5. #5
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    Many thanks Bruce

Similar Threads

  1. PICs can do more if use others than delays instructions
    By hardcore in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 24th February 2010, 19:52
  2. MY FIRST scrolling 8X8 LED
    By earltyso in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 15th August 2008, 16:23
  3. shifting problem
    By helmut in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 31st August 2007, 06:11
  4. Output PIC module
    By freelancebee in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th September 2005, 20:10
  5. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 5th May 2005, 23:29

Members who have read this thread : 0

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