53
Bit Manipulation in a Register Containing a Write-only Bit
Example 3: In this example, the port 6 control register PCR6 is accessed by a BCLR instruction.
As in the examples above, P6
7
and P6
6
are input pins, with a low-level signal input at P6
7
and a
high-level signal at P6
6
. The remaining pins, P6
5
to P6
0
, are output pins that output low-level
signals. In this example, the BCLR instruction is used to change pin P6
0
to an input port. It is
assumed that a high-level signal will be input to this input pin.
[A: Prior to executing BCLR]
P6
7
P6
6
P6
5
P6
4
P6
3
P6
2
P6
1
P6
0
Input/output
Input
Input
Output
Output
Output
Output
Output
Output
Pin state
Low
level
High
level
Low
level
Low
level
Low
level
Low
level
Low
level
Low
level
PCR6
0
0
1
1
1
1
1
1
PDR6
1
0
0
0
0
0
0
0
[B: BCLR instruction executed]
BCLR #0, @PCR6
The BCLR instruction is executed designating PCR6.
[C: After executing BCLR]
P6
7
P6
6
P6
5
P6
4
P6
3
P6
2
P6
1
P6
0
Input/output
Output
Output
Output
Output
Output
Output
Output
Input
Pin state
Low
level
High
level
Low
level
Low
level
Low
level
Low
level
Low
level
High
level
PCR6
1
1
1
1
1
1
1
0
PDR6
1
0
0
0
0
0
0
0
[D: Explanation of how BCLR operates]
When the BCLR instruction is executed, first the CPU reads PCR6. Since PCR6 is a write-only
register, the CPU reads a value of H'FF, even though the PCR6 value is actually H'3F.
Next, the CPU clears bit 0 in the read data to 0, changing the data to H'FE. Finally, this value
(H'FE) is written to PCR6 and BCLR instruction execution ends.
As a result of this operation, bit 0 in PCR6 becomes 0, making P6
0
an input port. However, bits 7
and 6 in PCR6 change to 1, so that P6
7
and P6
6
change from input pins to output pins.