这是一个单片机控制步进电机正反转的程序,但是只能转3圈,谁能改成一直转的程序?

#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code FFW[]=
{
0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09
};
uchar code REV[]=
{
0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01
};
sbit K1 = P3^0;
sbit K2 = P3^1;
sbit K3 = P3^2;
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void SETP_MOTOR_FFW(uchar n)
{
uchar i,j;
for(i=0;i<5*n;i++)
{
for(j=0;j<8;j++)
{
if(K3 == 0) break;
P1 = FFW[j];
DelayMS(25);
}
}
}
void SETP_MOTOR_REV(uchar n)
{
uchar i,j;
for(i=0;i<5*n;i++)
{
for(j=0;j<8;j++)
{
if(K3 == 0) break;
P1 = REV[j];
DelayMS(25);
}
}
}
void main()
{
uchar N = 3;
while(1)
{
if(K1 == 0)
{
P0 = 0xfe;
SETP_MOTOR_FFW(N);
if(K3 == 0) break;
}
else if(K2 == 0)
{
P0 = 0xfd;
SETP_MOTOR_REV(N);
if(K3 == 0) break;
}
else
{
P0 = 0xfb;
P1 = 0x03;
}
}
}

你这个程序是受按键控制的
按一个正转几圈,按另一个反转几圈。如果想改成按一个一直转下去,然后按其他键还有反应,需要修改程序架构。当然如果不需要按键反应,按照楼上所说在某处加一个while(1)就结束了追问

谢谢!我想改成按正转键会一直正转,按反转键会一直反转,按停止键停止的程序。

追答

这样的话需要硬件来调试的。没有硬件也得有原理图来搭建仿真环境。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-29
加个while(1)追问

谢谢!我想改成按正转键会一直正转,按反转键会一直反转,按停止键停止的程序。

相似回答