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
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
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,,,
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
That will definitely work but, b0 = PORTB >> 4 gives you the same result, uses less program code space, and saves you 2 RAM variables.
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
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.
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
PORTB pins set to inputs will have the weak internal pull-ups. PORTB pins configured as outputs are not affected.
Bookmarks