164
A: Before Execution of BSET Instruction
P5
7
Input
Low
0
1
On
P5
6
Input
High
0
0
Off
P5
5
Output
Low
1
0
Off
P5
4
Output
Low
1
0
Off
P5
3
Output
Low
1
0
Off
P5
2
Output
Low
1
0
Off
P5
1
Output
Low
1
0
Off
P5
0
Output
Low
1
0
Off
Input/output
Pin state
DDR
DR
Pull-up
B: Execution of BSET Instruction
BSET.B #0
@PORT5
;set bit 0 in data register
C: After Execution of BSET Instruction
P5
7
Input
Low
0
0
Off
P5
6
Input
High
0
1
On
P5
5
Output
Low
1
0
Off
P5
4
Output
Low
1
0
Off
P5
3
Output
Low
1
0
Off
P5
2
Output
Low
1
0
Off
P5
1
Output
Low
1
0
Off
P5
0
Output
High
1
1
Off
Input/output
Pin state
DDR
DR
Pull-up
Explanation:
To execute the BSET instruction, the CPU begins by reading port 5. Since P5
7
and
P5
6
are input pins, the CPU reads the level of these pins directly, not the value in the data register.
It reads P5
7
as low (0) and P5
6
as high (1).
Since P5
5
to P5
0
are output pins, for these pins the CPU reads the value in the data register (0).
The CPU therefore reads the value of port 5 as H'40, although the actual value in P5DR is H'80.
Next the CPU sets bit 0 of the read data to 1, changing the value to H'41.
Finally, the CPU writes this value (H'41) back to P5DR to complete the BSET instruction.
As a result, bit P5
0
is set to 1, switching pin P5
0
to high output. In addition, bits P5
7
and P5
6
are
both modified, changing the on/off settings of the MOS pull-up transistors of pins P5
7
and P5
6
.
Programming Solution:
The switching of the pull-ups for P5
7
and P5
6
in the preceding example
can be avoided by using a byte in RAM as a work area for P5DR, performing bit manipulations on
the work area, then writing the result to P5DR.