The program requires that if p1.3 is pressed short, p1.0 will be inverted, and if p1.3 is pressed long, p1.6 will be inverted. The following is the program:
#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()//To eliminate spikes
{
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 inverted
}
After burning the program into the MCU, I found that it could only recognize short presses, but not long presses. I thought about it and didn't know what the problem was. Finally, I tried to change the long press program handler to
while(1)
{
P1OUT^=BIT0;
P1OUT^=BIT6;
i=50000;
while(i--);
}
|