MSP430 G2553 LaunchPad GPIO中断
P1、P2端口上的每个管脚都支持外部中断。P1端口的所有管脚都对应同一个中断向量(Interrupt Vector),类似的,P2端口的所有管脚都对应另一个中断向量;通过PxIFG寄存器来判断中断来源于具体哪个管脚。
相关的寄存器如下表所示。
Register | Short Form | Register Type | Initial State |
Interrupt Flag | PxIFG | Read/write | Reset with PUC |
Interrupt Edge Select | PxIES | Read/write | Unchanged |
Interrupt Enable | PxIE | Read/write | Reset with PUC |
PxIFG:中断标志,1表示有中断事件待处理
PxIES:中断边沿选择,0表示上升沿触发,1表示下降沿触发
PxIE:GPIO中断使能,0表示禁用,1表示使能
程序示例
利用板上S2按键控制LED1灯闪烁,每按下一次,LED1灯闪烁一次。该程序还缺少按键消抖的功能。(G2 LaunchPad Rev1.5上P1.3没有连接电容及上拉电阻)
1 #include "io430.h" 2 3 #define LED1 BIT0 4 #define PUSH2 BIT3 5 6 //function declarations 7 void delay(void); 8 9 void main(void) 10 { 11 // Stop watchdog timer to prevent time out reset 12 WDTCTL = WDTPW + WDTHOLD; 13 14 //set P1.3 to input with pullup 15 P1OUT = 0; 16 P1OUT |= PUSH2; //initialize the pullup state 17 P1REN |= PUSH2; //enable internal pullup 18 19 //set P1.0 to output 20 P1DIR |= LED1; //P1.0 out to LED1, P1.3 remains input for PUSH2 button 21 22 //set the interrupt registers 23 P1IES |= PUSH2; //select high -> low transition 24 P1IFG &= ~PUSH2; //clear the flag for P1.3 before enabling the interrupt, 25 // to prevent an immediate interrupt 26 P1IE |= PUSH2; //enable interrupt for P1.3 27 28 __enable_interrupt(); //turn on the interrupts 29 30 while(1) 31 { 32 } 33 34 } 35 36 void delay(void) 37 { 38 volatile unsigned int i; 39 for(i = 0; i < 50000; i++); 40 } 41 42 //interrupt service routines 43 #pragma vector = PORT1_VECTOR 44 __interrupt void P1_ISR(void) 45 { 46 if((P1IFG & PUSH2) == PUSH2) 47 { 48 P1IFG &= ~PUSH2; //clear the interrupt flag 49 50 P1OUT |= LED1; //turn on LED1 51 delay(); 52 P1OUT &= ~LED1; //turn off LED1 53 } 54 else 55 { 56 P1IFG = 0; 57 } 58 }