程序要求是短按p1.3的话,出现p1.0取反,长按p1.3的话,p1.6取反,下面是程序:
#include "msp430g2553.h"
#define IDLE 0
#define SHORT 1
#define LONG 2
#define COUNTER_THRESHOLD 50
unsigned char key_dect;
unsigned char WDT_Counter=0;
void GPIO_init();
void Key_SM();
void WDT_init();
unsigned char LongClick_Dect();
void P13_OnShortRelease();
void P13_OnLongClick();
int main(void) {
WDTCTL = WDTPW | WDTHOLD;
GPIO_init();
WDT_init();
_enable_interrupts();
_bis_SR_register(LPM3_bits);
}
void GPIO_init()
{
P1DIR|=BIT0+BIT6;
P1OUT|=BIT0;
P1OUT&=~BIT6;
P1REN|=BIT3;
P1OUT|=BIT3;
//P1DIR&=~BIT3;
//P1IES|=BIT3;
//P1IE|=BIT3;
}
void WDT_init()//为了消除尖刺
{
WDTCTL=WDT_ADLY_16;
IE1|=WDTIE;
}
#pragma vector=WDT_VECTOR
interrupt void WDT_ISR(void)
{
Key_SM();
}
void Key_SM() //状态机建模。。
{
static unsigned char State=0;
static unsigned char key_now=0;
unsigned char key_past=0;
unsigned char key_dect=0;
key_past=key_now;
if(P1IN&BIT3) key_now=1;
else key_now=0;
if((key_now==0)&&(key_past==1)) key_dect=1;
if((key_now==1)&&(key_past==0)) key_dect=2;
switch(State)
{
case IDLE: if(key_dect==1)
{State=SHORT;P13_OnShortRelease();}break;
case SHORT: if(key_dect==2)
{
State=IDLE;
P13_OnShortRelease();
}
if(LongClick_Dect())
{
State=LONG;
P13_OnLongClick();
}
case LONG: WDT_Counter=0;
if(key_dect==2) State=IDLE;
default:State=IDLE;break;
}
}
unsigned char LongClick_Dect()
{
WDT_Counter++;
if(WDT_Counter==COUNTER_THRESHOLD)
{
WDT_Counter=0;
return 1;
}
else return(0);
}
void P13_OnShortRelease()
{
P1OUT^=BIT0;
}
void P13_OnLongClick(unsigned int i)
{
P1OUT^=BIT6;//p1.6取反
}
把程序烧进单片机中,发现只能识别短按,长按识别不出来。想了一下,不知道什么问题。。。纠结,最后试着把长按的程序处理程序换成
while(1)
{
P1OUT^=BIT0;
P1OUT^=BIT6;
i=50000;
while(i--);
}
|