Port 2

Port 2 is a general I/O port also functioning as the pins for the SCI3 serial interface. There are only 3 pins available on port 2 in the H8/3664. None of them have switchable pullups.

PIN Alternate Function
P20 SCK3
P21 RXD
P22 TXD

Three registers control its functions:

• Port mode register 1 (PMR1)
• Port control register 2 (PCR2)
• Port data register 2 (PDR2)

PMR1 has a single bit (IO.PMR1.BIT.B1) which enables the TXD function of P22. This is an imprtant register bit if only because it appears to be in the wrong register. However, there is no PMR2. The function of P11 is controlled by the serial control register SCR3. Pin P20 needs settings in both SCR3 and the Serial Mode Register SMR to enable it to function as the SCK3 bit.

PCR2 determines the direction of the pin. If a pin is designated as an IO pin, setting a bit in PCR2 to 1 makes the corresponding pin an output, setting it to zero makes the corresponding pin an input.

PDR2 simply holds the output data at the port 2 pins if the pin is configured as an output. Reading a pin while it is configured as an input reads the value at the pin regardless of the value in the PDR2 register.

After a reset, this port is configured as all general purpose input pins.

Using the header files provided with the GNUH8 toolchain, port 2 and its registers are found in the IO address space and can be accessed using statements like these:

int data;
IO.PCR2 = 0x03;        // set P20,P21 and P22 as outputs
IO.PDR2.BIT.B1 = 1;    // set P21 to 1
data = IO.PDR2.BIT.B2; // read a single bit into an integer variable

You should note that the final statement may not really read the individual bit - it simply reads the whole of PDR2 into data if the optimisation level is anything other than none. Thus you should treat the answer with a little care. The compiler can be trusted to a degree since the optimiser will mask subsequent access to the variable to ensure that the required bit is used.

Comments are closed.