步进电机的控制
/************************************** 步 进电 机 *
* *
* s1s2(正、反转) s3(正反转) s4(暂停) *
* *
*************************************/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define motor P1
#define n 500
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^3;
int time;
uchar code table0[]={0x20,0x30,0x10,0x18,0x8,0xC,0x4,0x24};//正转
uchar code table1[]={0x24,0x4,0xC,0x8,0x18,0x10,0x30,0x20};//反转
void delay(uint z)
{
while(--z);
}
void motor_z()
{
uint i;
for(i=0;i<8;i++)
{
motor=table0;
delay(130);
}
}
void motor_f()
{
uint j;
for(j=0;j<8;j++)
{
motor=table1;
delay(130);
}
}
void motor_stop()
{
if(s4==0)
{
delay(5);
if(s4==0)
{
while(!s4);
time=0;
P1=0xff;
}
}
}
void main()
{
P3=0xff;
while(1)
{
if(s1==0)
{
delay(5);
if(s1==0)
{
while(!s1);
time=n;
while(time--)
{
motor_z();
motor_stop();
}
}
}
else if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
time=n;
while(time--)
{
motor_f();
motor_stop();
}
}
}
else if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
time=n;
while(time--)
{
motor_z();
motor_stop();
}
time=n;
while(time--)
{
motor_f();
motor_stop();
}
}
}
else if(s4==0)
{
delay(5);
if(s4==0)
{
while(!s4);
{
motor_stop();
}
}
}
}
}
页:
[1]