This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430求助。。。我是新手。。用的是g2553

我的目的是初始状态LED1,LED2 均熄灭,第一次按下按键,LED1 点亮,第二
次按下按键,LED1 熄灭,LED2 点亮,第三次按下按键,LED1,LED2 均熄灭,
第四次按下按键,LED1,LED2 持续亮灭闪烁。
程序如下:
#include"msp430.h" //包含头文件,系统自动匹配对应芯片的头文件
void delay(unsigned int i) //延时子函数
{
unsigned int j;
for(j=0;j<i;j++);
}
void main(void)
{
int n=0;
WDTCTL=WDTPW+WDTHOLD; //关看门狗
P1DIR|=BIT0; //P1.0 作输出口
P1DIR|=BIT6; //p1.6作输出口
P1DIR&=~BIT3; //P1.3 作输入口
P1REN|=BIT3; //P1.3 上/下拉电阻使能
P1OUT&=~BIT0;
P1OUT&=~BIT6;
while(1)
{
  if((P1IN&0x08)==0)
  {
   delay(500);
   if((P1IN&0x08)==0)
   {
    while((P1IN&0x08)==0);
    n++;
    switch (n)
    {
    case 1:
    {
     P1OUT|=0x01;
        break;
    }
    case 2:
    {
     P1OUT^=BIT0+BIT6;
     break;
    }
    case 3:
    {
     P1OUT&=0xbe;
     break;
    }
    case 4:
    {
     while(1)
     {
     P1OUT^=BIT0|BIT6;
     __delay_cycles(1000000);
     n=0;
     break;
     }
    }
    }
   }
  }
}
}
请大神帮忙看看错在哪里了,本人新手。。。

  • 现在有什么问题?觉得你的消抖时间有点短

  • 现在按键按下去没反应,但按键是没问题的用其他程序测试过的。。。

  • #include"msp430.h" //包含头文件,系统自动匹配对应芯片的头文件
    
    int n = 3;
    
    void main(void)
    {
    	WDTCTL = WDTPW+WDTHOLD; //关看门狗
    
    	P1DIR |= BIT0; //P1.0 作输出口
    	P1DIR |= BIT6; //p1.6作输出口
    	P1OUT &= ~BIT0;
    	P1OUT &= ~BIT6;
    
    	P1IE |=  BIT3;                            // P1.3 interrupt enabled
    	P1IES |= BIT3;                            // P1.3 Hi/lo edge
    	P1REN |= BIT3;							// Enable Pull Up on SW2 (P1.3)
    	P1IFG &= ~BIT3;                           // P1.3 IFG cleared
    
    	__bis_SR_register(LPM3_bits + GIE);
    
    	while(1)
    	{
    		switch (n)
    		{
    		case 0:
    		{
    			P1OUT |= BIT0;
    			P1OUT &= ~BIT6;
    			break;
    		}
    		case 1:
    		{
    			P1OUT |= BIT6;
    			P1OUT &= ~BIT0;
    			break;
    		}
    		case 2:
    		{
    			P1OUT &= ~(BIT0 + BIT6);
    			break;
    		}
    		case 3:
    		{
    			P1OUT ^= BIT0|BIT6;
    			__delay_cycles(1000000);
    			break;
    		}
    		}
    	}
    
    }
    
    // Port 1 interrupt service routine
    #pragma vector=PORT1_VECTOR
    __interrupt void Port_1(void)
    {
    	unsigned int i;
    	unsigned char PushKey;
    
    	PushKey = P1IFG & BIT3;
    	for(i=3000;i>0;i--)
    	{
    		__no_operation();
    	}
    	if((P1IN & PushKey)==PushKey)
    	{
    		P1IFG = 0;
    		return;
    	}
    
    	if(PushKey & BIT3)
    	{
    
    		n++;
    		if(n == 4)
    			n = 0;
    		__bic_SR_register_on_exit(LPM3_bits);
    	}
    	P1IFG &= ~BIT3;                           // P1.3 IFG cleared
    }
    
  • 不客气。

    新手建议从TI 官方的范例程序开始~ 一般自己想要实现的功能都可以从范例代码中拼凑而成。

x 出现错误。请重试或与管理员联系。