我是新人...~~请大神们帮我看看我的程序问题出在哪了....用按键控制步进电机正转反转停止的 正转时有些一卡一卡的 而且不能反转....
#include<reg51.h>
unsigned char code runz[]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //正转步进电机相序表
unsigned char code runf[]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //反转步进电机相序表
sbit K1=P0^0;sbit K2=P0^1;sbit K3=P0^2;
void delay(unsigned char i);//延时函数声明
void motor_init()//步进电机初始化
{
P1=0xff;
}
void keymotor() //按键驱动步进电机 K1反转,K2正转,K3停止
{
loop1:if(K1==0)
{
delay(10);
if(K1==0)
{
unsigned char i;
while(1)
for(i=0;i<8;i++)
{
P1=runz[i];
delay(10);
if(K2==0)
goto loop2;
else if(K3==0)
goto loop3;
}
}
}
loop2:if(K2==0)
{
delay(10);
if(K2==0)
{
unsigned char i;
while(1)
for(i=0;i<8;i++)
{
P1=runf[i];
delay(10);
if(K1==0)
goto loop1;
else if(K3==0)
goto loop3;
}
}
}
loop3:if(K3==0)
{
delay(10);
if(K3==0)
{
while(1)
{
P1=0xff;
if(K1==0)
goto loop1;
else if(K2==0)
goto loop2;
}
}
}
}
void main()
{
motor_init();
while(1)
keymotor();
}
void delay(unsigned char i)
{
unsigned char j;
while(i--)
for(j=0;j<255;j++);
}
|