The IO port is the most b
asic component for the processor system to communicate with the outside world. From basic keyboards and LEDs to complex peripheral chips, they are all read or controlled through the input and output operations of the IO port.
In the MSP430 series, different microcontrollers have different numbers of IO ports. The smallest MSP430F20xx series has only 10 IO ports, which is suitable for applications in ultra-small devices; the most feature-rich MSP430FG46xx series has up to 80 IO ports, which is enough to handle complex applications with many external devices. In the MSP430FE425 microcontroller, there are 14 IO ports, which belongs to the series with fewer IO ports. However, since devices that require a large number of pins, such as LCDs and multi-channel analog inputs, have dedicated pins and do not occupy IO ports, the number of IO ports is still sufficient in most designs.
l IO port register
Similar to most MCUs, MSP430 MCU also groups 8 IO ports into one group. For example, P1.0~P1.7 belong to P1 port. Each group of IO ports has 4 control registers, and P1 and P2 ports also have 3 additional interrupt registers.
Table 2.1.1 IO port register list.
Register Name Register Function Read/Write Type Reset Initial Value
PxIN Px port input register read only
PxOUT Px port output register is readable and writable and remains unchanged
PxDIR Px port direction register readable and writable 0 (all input)
PxSEL Px port second function selection readable and writable 0 (all IO ports)
PxIE Px port interrupt allows readable and writable 0 (all interrupts are not allowed)
PxIES Px port interrupt edge selection readable and writable remains unchanged
PxIFG Px port interrupt flag is readable and writable 0 (no interrupt occurs)
This is the first time that a register list appears in this book. It is necessary to explain that the registers and flags of the MSP430 microcontroller are all in uppercase. If a lowercase "x" appears, it means that there is more than one device, so there is more than one register. In order to shorten the length of the list, it is not necessary to list all of them. The letter x is used to represent the serial number. For example, for PxOUT in the table, when x is 1, 2, 3, it becomes P1OUT, P2OUT, P3OUT.
n PxDIR register is used to set the direction of each IO port: 0 = input 1 = output The IO port of MSP430 microcontroller is a bidirectional IO port, so when using the IO port, you must first use the direction selection register to set the direction of each IO port. For example, P1.5, P1.6, and P1.7 are connected to buttons, and P1.1, P1.3, and P1.4 are connected to LEDs, then P1.5, P1.6, and P1.7 should be set as input, and P1.1, P1.3, and P1.4 should be set as output:
P1DIR|=BIT1+BIT3+BIT4; // Set P1.1, P1.3, P1.4 as output
P1DIR &=~ (BIT5+BIT6+BIT7); // P1.5, P1.6, P1.7 are set as input (can be omitted)
Since the PxDIR register will be cleared to 0 during the reset process, the IO port directions that are not set are all in input state, so the second sentence can be omitted.
For all IO ports that have been set to output state, their output level can be set through the PxOUT register; for all IO ports that have been set to input state, their input level can be read back through the PxIN register. For example, read back the switch state on the P1.5 port, and determine if it is in the pressed state (low level), then output a high level from the P1.1 port to light up the LED:
if((P1IN & BIT5)==0) P1OUT|=BIT1; //If P1.5 is low level, P1.1 outputs high level
The PxSEL register is used to set the function of each IO bit: 0 = normal IO port 1 = second function
In the MSP430 microcontroller, many internal functional modules also need to communicate data with the outside world. In order not to increase the number of chip pins, most of them are multiplexed with IO ports. Therefore, most IO pins have a second function. Through the register PxSEL, some IO pins can be specified as the second function. For example, from the pin layout diagram in the appendix, we can find that the P2.4 and P2.5 ports of the MSP430x42x series microcontrollers and the TXD and RXD common pins of the serial port. If you need to configure these two pins as serial port transceiver pins, you must set the 4 and 5 positions of P2SEL high:
P2SEL |= BIT4 + BIT5; // P2.4,5 are set as serial port transmit and receive pins
l IO port interrupt
In all MSP430 microcontrollers, the total of 16 IO ports, including P1 and P2, can trigger interrupts. In the MSP430x42x series, 14 IO ports belong to P1 or P2, so each IO can be used as an interrupt source. Use the following two registers to configure the IO port for interrupt use:
The PxIE register is used to set the interrupt enable for each IO bit: 0 = not allowed 1 = allowed
The PxIES register is used to select the interrupt trigger edge for each IO bit: 0 = rising edge 1 = falling edge
Before using the IO port interrupt, you need to set the IO port to input state and allow the interrupt of the IO bit, and then select the trigger mode as rising edge trigger or falling edge trigger through the PxIES register. For example, set P1.5, P1.6, and P1.7 ports as interrupt sources, falling edge trigger:
P1DIR &=~(BIT5 + BIT6 + BIT7); // P1.5, P1.6, P1.7 are set as input (optional)
P1IES |= BIT5 + BIT6 + BIT7; // Set P1.5, P1.6, and P1.7 as falling edge interrupts
P1IE |= BIT5 + BIT6 + BIT7; // Enable P1.5, P1.6, P1.7 interrupts
EINT(); // General interrupt enabled
n PxIFG register is the IO interrupt flag register: 0 = interrupt condition is not met 1 = interrupt condition has been met Regardless of whether the interrupt is allowed or not, and regardless of whether the interrupt service program is being executed, as long as the corresponding IO meets the interrupt condition (such as the arrival of a falling edge), the corresponding bit in PxIFG will be immediately set to 1 and maintained, and can only be cleared manually by software. The purpose of this mechanism is to ensure that every interrupt will not be missed as much as possible. In the MSP430 series of microcontrollers, the 8 interrupts of port P1 and the 8 interrupts of port P2 each share an interrupt entry, so another important role of this register is to determine which IO bit generated the interrupt in the interrupt service program. The following interrupt service program demonstrates that different codes are executed after an interrupt occurs on P1.5, P1.6, and P1.7:
#pragma vector = PORT1_VECTOR //P1 port interrupt source
__interrupt void P1_ISR(void) //Declare an interrupt service routine named P1_ISR()
{
if(P1IFG & BIT5) //Judge the 5th bit of P1 interrupt flag (P1.5)
{
... ... //Write P1.5 interrupt handler here
}
if(P1IFG & BIT6) //Judge the 6th bit of P1 interrupt flag (P1.6)
{
... ... //Write P1.6 interrupt handler here
}
if(P1IFG & BIT7) //Judge the 7th bit of P1 interrupt flag (P1.7)
{
... ... //Write P1.7 interrupt handler here
}
P1IFG=0; // Clear all interrupt flags of P1
}
Note that the interrupt flag must be manually cleared before exiting the interrupt, otherwise the interrupt will continue to occur. Similarly, even if there is no interrupt condition at the IO port, manually writing "1" to the corresponding bit of the PxIFG register will also trigger an interrupt. Changing the interrupt edge selection register is equivalent to a jump, which will also trigger an interrupt. Therefore, changing the PxIES register should be done after turning off the interrupt, and the interrupt flag should be cleared in time before turning on the interrupt. The large number of IO interrupts of the MSP430 microcontroller is very suitable for keyboard input, but it should be noted that the keyboard has a mechanical structure. During the closing or releasing process, the collision and rebound of the mechanical structure will cause a few milliseconds of "burrs" on the signal.