仿真电路如上图,代码如下:
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
char x = 0;
uchar code XZF[4]= //正转
{
0x0e,0x0d,0x0b,0x07,
};
uchar code XZR[4]= //反转
{
0x0b,0x07,0x0e,0x0d,
};
sbit K1 = P0^0;//按键
sbit K2 = P0^1;
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void SETP_MOTOR_XF()
{
if(x > 3) x = 0;
P1 = XZF[x];
x++;
}
void SETP_MOTOR_XR()
{
if(x < 0) x = 3;
P1 = XZR[x];
x--;
}
void main()
{
while(1)
{
if(K1 == 0)
{
DelayMS(150);
if(K1 == 0) SETP_MOTOR_XF();
}
else if(K2 == 0)
{
DelayMS(150);
if(K2 == 0) SETP_MOTOR_XR();
}
}
}
我按下按键,要不步进电机不转,要不然就是转得很“奇怪”,总之我是想让它按下K1就一直正转,K2一直反转,但是不成功。问题在哪里?求指导指导啊!