|
/*目的是按下按键则蜂鸣器就发出声音,电路板是msp430F169的学习板*/
#include <msp430x16x.h>
#define NO_KEY 0 //没有案按键
#define KEY_K1 1 //按键1
#define KEY_K2 2 //按键2
#define KEY_K3 3 //按键3
#define STEP_IDEL 0 //按键延时
#define STEP_DELAY 1 //等待按键
#define STEP_FINISH 2 //按键过程结束
#define TRUE 1
unsigned int wDelay = 0;
unsigned char nKey = NO_KEY;
/*void init_BT(void)
{
BTCTL = 0x06;
IE2 |= 0x80;
}*/
unsigned char GetKey(void)
{
static unsigned char nStep = STEP_IDEL;
static unsigned char nBufferedKey;
switch(nStep)
{
case STEP_DELAY: //按键消抖
if(wDelay > 4)
{ wDelay = 0;
nStep = STEP_FINISH;
}
else
nBufferedKey = NO_KEY;
break;
case STEP_FINISH: //按键完成
if(((P1IN & 0x04) == 0 )&& (nBufferedKey == KEY_K1))
nKey = KEY_K1;
else if(((P1IN & 0x08) == 0)&& (nBufferedKey == KEY_K2))
nKey = KEY_K2;
else if(((P1IN &0x10) == 0) &&(nBufferedKey ==KEY_K3))
{ nKey = KEY_K3;
nStep = STEP_IDEL;
}
break;
case STEP_IDEL: //按键检测
if ((P1IN & 0x04) == 0)
{
nBufferedKey = KEY_K1;//当按P1.2时为低电平
wDelay = 0;
nStep = STEP_DELAY;
}
else if((P1IN &0x08) == 0)//当按P1.3时为低电平
{
nBufferedKey = KEY_K2;
wDelay = 0;
nStep = STEP_DELAY;
}
else if((P1IN & 0x10) == 0)//当按P1.4时为低电平
{
nBufferedKey = KEY_K3;
wDelay = 0;
nStep = STEP_DELAY;
}
break;
default:
break;
}
return nBufferedKey;
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;//关闭看门狗
TACTL = TASSEL1 + TACLR ;//时钟源SMCLK,清除TAR
// init_BT();
CCTL0 = CCIE;//CCR0中断使能
CCR0 = 50000;//产生3.2ms的定时
TACTL |= MC1;//Timer_a增计数模式
wDelay = 0;
_EINT();//使能系统中断
while (TRUE)
{
nKey = GetKey();
switch (nKey)
{
case NO_KEY:
break;
case KEY_K1://按P1.2时
P4DIR |= 0x0c;
P4OUT |= 0x04;
nKey= NO_KEY;
break;
case KEY_K2://按P1.3时
P4DIR |= 0x0c;
P4OUT |= 0x08;
nKey= NO_KEY;
break;
case KEY_K3://按P1.4时
nKey = NO_KEY;
break;
}
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A( void)
{
wDelay++;
CCR0 += 50000;
}
/*#pragma vector = BASICTIMER_VECTOR
__interrupt void BT_Interrrupt(void)
{
wDelay++;
CCR0 += 50000;
}*/
/*interrupt [TIMERA0_VECTOR]void Timer_a_interrupt(void)//Timer_a_0中断
{
wDelay++;
CCR0 += 50000;
}*/
这是自己弄一个小程序,怎么好像进不了中断啊,按键也没有什么响应,劳烦大虾们给看看啊~~~~
|
|