大致思路为捕获到一定数量的上升沿后步进电机开始转动,之前用按键代替可以使电机转动,但现在电机只会震动不知道问题出在哪里?
#include
#define uint unsigned int
volatile unsigned int i,j,k=0; int cycle[100];
/*****staticconst unsigned char fullstepA[]=
{0x02,0x0a,0x08,0x09,0x01,0x05,0x04,0x06,0x02};****/
static const unsigned char fullstepB[]=
{0x02,0x06,0x04,0x05,0x01,0x09,0x08,0x0a,0x02};
void IO()
{
P1DIR|=BIT0; //p1.0选择ACLK输出
//P1OUT&=~BIT0;
P1SEL|=BIT0;
P4DIR|=BIT7;
P4OUT&=~BIT0;
P2DIR&=~BIT0;
P2REN |= BIT0; //启用P2.0内部上下拉电阻
P2OUT |= BIT0; //将电阻设置为上拉
P2SEL|=BIT0;
P6DIR |=0x0f;
P6OUT |=0x0f;
}
void main()
{
WDTCTL = WDTPW | WDTHOLD;
IO();
TA1CCTL1=CM_1+SCS+CAP+CCIE;//CCR1工作于捕获模式,同步捕获,上升沿触发,中断使能;
TA1CTL|=TASSEL_2+MC_2+TACLR+TAIE;//TA1主时钟SMCLK,连续计数模式,清除TAR;
_EINT(); //全局中断
while(1)
{
/**** if(k==1) {
P6OUT=fullstepA[j];
}*****/
if(k==2)
{
P6OUT=fullstepB[j];} //转动步进电机
}
}
#pragmavector=TIMER1_A1_VECTOR
__interruptvoid TIMER_A1(void)
{
switch(TA1IV)
{ case 2:
cycle=TA1CCR1; //将计数器的数值存入数组中
i++;
if(i<=50)
{ P4OUT &=~ BIT7; //灯的输出
k=1;
}
if(i>50)
{ k=2;
P4OUT|=BIT7;
j++; //步进电机相序累加
if(j==7)
{j=0;}
// i=0;
}
}
}