#include <msp430f6638.h>
void step(int p)
{
switch(p)
{
case 0:
{
P7OUT &=~ BIT4;
P1OUT &=~ BIT2;
P2OUT &=~ BIT2;
P1OUT &=~ BIT4;
P2OUT &=~ BIT3;
break;
}
case 1:
{
P7OUT |= BIT4;
P1OUT |= BIT2;
P2OUT &=~ BIT2;
P1OUT &=~ BIT4;
P2OUT &=~ BIT3;
}
case 2:
{
P7OUT |= BIT4;
P1OUT &=~ BIT2;
P2OUT |= BIT2;
P1OUT &=~ BIT4;
P2OUT &=~ BIT3;
}
case 3:
{
P7OUT |= BIT4;
P1OUT &=~ BIT2;
P2OUT &=~ BIT2;
P1OUT |= BIT4;
P2OUT &=~ BIT3;
break;
}
case 4:
{
P7OUT |=BIT4;
P1OUT &=~ BIT2;
P2OUT &=~ BIT2;
P1OUT &=~ BIT4;
P2OUT |= BIT3;
break;
}
default:
break;
}
}
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int flag=0;
P4DIR |= BIT5; // 设置P4.5 口方向为输出
P4DIR &= ~BIT0; // P4.0 置为输入
P4REN |= BIT0; // 使能P4.0 上拉电阻
P4OUT |= BIT0; // P4.0 口置高电平
P4IES |= BIT0; // 中断沿设置(下降沿触发)
P4IFG &= ~BIT0; // 清P4.0 中断标志
P4IE |= BIT0; // 使能P4.0 口中断
__bis_SR_register(GIE);
P1DIR |= BIT2 + BIT4;
P2DIR |= BIT2 + BIT3;
P7DIR |= BIT4;
P7OUT |= BIT4;
int a=0;
int b1[4]={1,3,2,4};
int b2[4]={1,4,2,3};
while(1)
{
if((P4IN&BIT0)==0)
{ //按键S7
__delay_cycles(160000);
if((P4IN&BIT0)==0)
{
flag=1;
}
}
if((P4IN&BIT0)==1)
{ //按键S7
__delay_cycles(160000);
if((P4IN&BIT0)==1)
{
flag=0;
}
}
If(flag==1)
{
step(b1[a++]); //正转
if (a>3)
a=0;
P4DIR |= BIT5;
__delay_cycles(160000);
}
If(flag==0)
{
step(b2[a++]); //反转
if (a>3)
a=0;
__delay_cycles(160000);
}
// P4 中断函数
#pragma vector=PORT4_VECTOR
__interrupt void Port_4(void)
{
P4OUT ^= BIT5;
P4IFG &= ~BIT0;
} |