PDA

View Full Version : Need help isolating 1/2 portb



Darrenmac
- 8th November 2004, 22:03
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

Bruce
- 9th November 2004, 00:37
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,,,

Darrenmac
- 9th November 2004, 01:22
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

Bruce
- 9th November 2004, 01:51
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.

Darrenmac
- 9th November 2004, 01:55
Many thanks Bruce