This section talks about external interrupts.
Read the introduction and understand the program is the best way
External interrupt is the lowest priority interrupt of MSP430 and it is a maskable interrupt. It is relatively simple to use.
1.2.7 Simple port interrupt (external interrupt)
All ports of P1 and P2 have interrupt capability, which can be configured through registers PxIFG, PxIE and PxIES. For other ports, please refer to the specific pin manual. All P1 interrupt flags have the highest priority (compared to the external interrupts of other pins), and P1IFG.0 is the best.
PXIV interrupt vector registers: only P1IV and P2IV. The highest priority enabled interrupt generates a sequence number in the P1IV register, which is recognized or added to the program counter, and then automatically executes the appropriate interrupt service routine. Disabling the P1 port interrupt does not affect the value in the P1IV register. The 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: When using the port interrupt function, performing operations such as PXIN and PXOUT may cause the interrupt to 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 higher priority first. When the interrupt service routine ends, the interrupt flag of this bit will be automatically cleared, and then respond to the other interrupt.
PxIE Interrupt Enable Register
A low level indicates that the interrupt is disabled;
A high level indicates that interruption is enabled;
PXIES interrupt trigger mode selection register
A low level indicates a rising edge trigger;
A high level indicates a falling edge trigger;
External interrupt application example: /*Using the interrupt method, 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 <msp430.h>
int s=0; //s is used to indicate the number of key presses
int num =0; //num represents the LED value
void main(void){
WDTCTL=WDTPW+WDTHOLD; //Turn off the watchdog
P1DIR=0xff; //P1 is all connected to output
P1OUT=0x00; //Connect to LED initialization, so pull all low, so the light is off at the beginning
P2DIR=0x00; //P2 is set to input. Because it needs to accept external interrupts
P2IFG=0x00; //Clear the interrupt flag of port P2
P2IE=BIT2; //P2.2 enables interrupt
P2IES=0xff; //P2 is falling edge triggered
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. interrupt)
P2OUT=0xff;
P2REN=0xff; //When used as input, you must configure the pull-up resistor (very important, easy to forget, I made a mistake here-_')
__enable_interrupt(); //Open the general 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 subroutine
unsigned int temp; //local variable
int i;
for(i=0;i<12000;i++); // Delay debounce
if((P2IN&0xff)!=0xff){ //If a key is pressed
temp=P2IFG; //Read interrupt flag
P2IFG=0x00; // Flag clear
if(temp==0x04) //If P2.2 generates an interrupt
s++;}} //This part actually has a few redundant sentences, mainly to reflect the knowledge of each port interrupt
Note: Interrupt 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 not connected on the PCB board to prevent floating input and reduce power consumption. Because these pins are not connected, their output values do not need to be paid attention to. Or you can enable the high/low register by setting the PxREN register of the unused pin to avoid the interference of floating input.
|