External interrupts are the lowest priority interrupts of MSP430 and are maskable interrupts. They are relatively simple to use. 1.2.7 Simple port interrupts (external interrupts) All ports of P1 and P2 have interrupt capabilities and can be configured through registers PxIFG, PxIE and PxIES. For other ports, please refer to the specific pin manual. All P1 interrupt flags are the highest priority (compared to external interrupts of other pins), among which P1IFG.0 is the best. PXIV interrupt vector register: only P1IV and P2IV. The highest priority enabled interrupt generates a sequence number in the P1IV register, which will be recognized or added to the program counter, and then automatically execute the appropriate interrupt service routine. Disabling the P1 port interrupt will not affect the value in the P1IV register. P2 port has the same function. The PxIV register can only be accessed by word. PxIFGx interrupt flag register: This register is valid only when the corresponding interrupt enable PXIE is turned on and the general interrupt GIE is turned on. A low level indicates that there is no interrupt request waiting for response; A high level indicates that there is an interrupt request waiting for response; Note: During the use of the port's interrupt function, if operations such as PXIN and PXOUT are performed, the interrupt may change. Note: The interrupt flag needs to be cleared by software. There is one exception: if two interrupts occur at the same time, respond to the interrupt with a higher priority first. When the interrupt service program ends, the interrupt flag of this bit will be automatically cleared, and then respond to another interrupt. A low level in the PxIE interrupt enable register indicates that the interrupt is turned off; A high level indicates that the interrupt is allowed; A low level in the PXIES interrupt trigger mode selection register indicates a rising edge trigger; A high level indicates a falling edge trigger; External interrupt application example: /*Using the interrupt mode, switch S2 (connected to P2.2) controls the LEDs (connected to P1) to light up one by one (see the PCB diagram for wiring)*/ #include
int s=0; //s is used to indicate the number of key presses int num =0; //num indicates the LED value void main(void){ WDTCTL=WDTPW+WDTHOLD; //Turn off the watchdog P1DIR=0xff; //P1 is all connected to output P1OUT=0x00; //Connect the LEDs for initialization, so pull them all low, so the lights are off at the beginning P2DIR=0x00; //P2 is all set to input Because it needs to accept external interrupts P2IFG=0x00; //Clear the interrupt flag of port P2 P2IE=BIT2; //P2.2 turns on interrupt P2IES=0xff; //P2 is triggered by a falling edge P2IN=BIT2; //P2.2 input is pulled high, so when the switch is closed it will be pulled low to generate a falling edge (i.e. an interrupt) P2OUT=0xff; P2REN=0xff; //When used as an input, be sure to configure a pull-up resistor (very important, easy to forget, I made a mistake here -_') __enable_interrupt(); //Turn on the total interrupt while(1) { num=s%5; switch(num){ case 0:P1OUT=BIT1;break; case 1:P1OUT=BIT2;break; case 2:P1OUT=BIT3;break; case 3:P1OUT=BIT4;break; case 4:P1OUT=BIT5;break;}}} #pragma vector=PORT2_VECTOR //Fixed format, declare interrupt vector address__interrupt void Port2_ISR(void) {//interrupt subroutineunsigned int temp; //local variable int i; for(i=0;i<12000;i++); //delay debounceif((P2IN&0xff)!=0xff){ //If a key is pressedtemp=P2IFG; //Read interrupt flagP2IFG=0x00; //Clear flagif(temp==0x04) //If P2.2 generates interrupts++;}} //There are actually a few redundant sentences in this part, mainly to reflect the knowledge of each port interruptRemarksInterrupt subroutine calling format: #pragma vector=interrupt vector address__interrupt void interrupt service program name(void) { //interrupt handler} 1.2.8 Unused I/O Unused I/O pins are best set to normal I/O function, output direction and do not connect these pins on the PCB board to prevent floating input and reduce power consumption. Since these pins are not connected, their output values do not need to be considered. Alternatively, you can enable the high/low registers by setting the PxREN register of the unused pins to avoid interference from floating inputs.