|
//按键控制步进电机自动正转反转程序://已实测验证OK
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit k1=P3^4;
sbit k2=P3^5;
sbit A=P3^0;
sbit B1=P3^1;
sbit C=P3^2;
sbit D=P3^3;
void DelayMS(uint aa)
{
uchar bb;
while(aa--)
{
for(bb=0;bb<115;bb++) //1ms基准延时程序
{
;
}
}
}
void cw()//正转
{
uint i,j;
i=30;
j=200;
while(j--)
{
A=1;
DelayMS(i);
A=0;
DelayMS(i);
B1=1;
DelayMS(i);
B1=0;
DelayMS(i);
C=1;
DelayMS(i);
C=0;
DelayMS(i);
D=1;
DelayMS(i);
D=0;
DelayMS(i);
}
}
void ccw()//反转
{
uint i,j;
i=30;
j=200;
while(j--)
{
D=1;
DelayMS(i);
D=0;
DelayMS(i);
C=1;
DelayMS(i);
C=0;
DelayMS(i);
B1=1;
DelayMS(i);
B1=0;
DelayMS(i);
A=1;
DelayMS(i);
A=0;
DelayMS(i);
}
}
void main(void)
{
A=1;
B=1;
C=1;
D=1;
while(1)
{
if(k1==0)
{
while(k1==0);
DelayMS(200);
cw();
DelayMS(50000);
ccw();
}
if(k2==0)
{
while(k2==0);
DelayMS(200);
ccw();
DelayMS(50000);
cw();
}
}
}
|
|