>> I seem to be having problems understanding the interactions >> of OPTION_REG.7 (RBPU bit) and the reading of SW1.
OPTION_REG.7 turns ON or OFF the PortB internal pull-ups.
On the Lab-X3 board, when SW1 is pressed, it will ground RB7. If you don't have the internal pull-ups turned ON, then you have what's called a floating input on RB7 (or any other portb pin set to an input).
With the internal pull-ups ON (OPTION_REG.7=0), you have internal pull-up resistors from Vcc to all portb pins (that are set to inputs).
RB7 sees Vcc through the internal pull-up resistor when SW1 is not being pressed, and ground when SW1 is pressed. This is why you want OPTION_REG.7=0 if you're using RB7 on the LAB-X3 as an input. So RB7 does not "float" when SW1 is not being pressed.
If you had an external pull-up from Vcc to RB7, then of course you wouldn't need to turn on internal pull-ups.
Portb pins that are set to outputs are not affected by OPTION_REG.7=0. Only portb pins that are set as inputs will see the internal pull-ups.
Example;
TRISB = %00110011
OPTION_REG.7=0
Only RB0, RB1, RB4 and RB5 will see Vcc through the internal pull-ups since these are the only pins on portb set to inputs by the TRISB direction control register.
Bookmarks