使用GPIO口的中断功能,识别到LaunchPad中的短按键和长按键,短按键点亮红色led,长按键10ms左右就点亮另一只LED
#include <msp430g2553.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
P1DIR = BIT0+BIT6;
P1OUT = BIT3;
P1REN |= BIT3;
P1IE |= BIT3; //中断使能
P1IES |= BIT3; //下降沿触发
P1IFG &= ~BIT3; //清零标志位
_EINT(); //开总中断
while(1);
}
#pragma vector=PORT1_VECTOR //固定格式
__interrupt void Port_1(void)
{
_DINT();
volatile unsigned int i; //不加volatile时,有时候长按与短按分不清楚
unsigned int flag=1;
i = 30000; // 延时消抖
do
{
i--;
}while (i != 0); // 标准的do……while语句,注意别漏掉了括号后面的分号
if((P1IN&BIT3)==0)
{
flag++;
}
if(flag==2)
{
P1OUT^=BIT6;
}
else
{
P1OUT^=BIT0;
}
P1IFG &= ~BIT3; //清除中断标记
}