Writing to all bits within port

When writing a value to a port, all 8 bits are modified at the same time. However only the bits configured as outputs make it to the outside world.

WARNING: If the intension is to turn a single pin on or off the user must be aware that writing a value to the port will cause all outputs to change, not just the one the user is interested in.

e.g.

writing the value 0x40 to PORTA

BEFORE

PORTA 1 0 0 0 1 0 1 0
TRISA 0 0 1 0 0 1 0 1
PINS 1 0 x 0 1 x 1 x

AFTER

PORTA 0 1 0 0 0 0 0 0
TRISA 0 0 1 0 0 1 0 1
PINS 0 1 x 0 0 x 0 x

Set specific bit within PORT

To set a specific bit (or patern of bits) within a port, the user should OR a 1 into that bit (or patern of bits)

e.g.

ORing the value 0x40 to PORTA

BEFORE

PORTA 1 0 0 0 1 0 1 0
TRISA 0 0 1 0 0 1 0 1
PINS 1 0 x 0 1 x 1 x

AFTER

PORTA 1 1 0 0 1 0 1 0
TRISA 0 0 1 0 0 1 0 1
PINS 1 1 x 0 1 x 1 x
a more detailed explanation and examples of setting specific bits

see also using bit number instead of masks

Clear specific bit within port

To clear a specific bit (or patern of bits) within a port, the user should AND a 0 into that bit (or patern of bits)

e.g.

ANDing the value 0xF7 to PORTA

BEFORE

PORTA 1 0 0 0 1 0 1 0
TRISA 0 0 1 0 0 1 0 1
PINS 1 0 x 0 1 x 1 x

AFTER

PORTA 1 0 0 0 0 0 1 0
TRISA 0 0 1 0 0 1 0 1
PINS 1 0 x 0 0 x 1 x
a more detailed explanation and examples of clearing specific bits

see also using bit number instead of masks