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 }

 

转载于:https://www.cnblogs.com/zlbg/p/4558372.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值