#include
#define uchar unsigned char
#define uint unsigned int
uchar code zheng[4]={0xf1,0xf2,0xf4,0xf8,};
uchar code fan[4]={0xf9,0xfc,0xf6,0xf3,};
sbit I1=P0^0;
sbit I2=P0^1;
sbit I3=P0^2;
sbit I4=P0^3;
sbit ENA=P0^4;
sbit ENB=P0^5;
sbit KEY=P0^6;
void delay(uint t)
{
uint k;
while(t--)
{
for(k=0; k<100; k++)
{ }
}
}
void motor1(char A[4])
{
uchar i;
for (i=0; i<4; i++) //一个周期转30度
{
P1 =((P1|0X0F)&0XFF)&A[i]; //取数据
delay(15); //调节转速
}
}
void motor2(char B[4])
{
uchar i;
for (i=0; i<4; i++) //一个周期转30度
{
P1=(P1|0XF0)&(0X0F|(B[i]<<4)); //取数据
delay(15); //调节转速
}
}
void motor3(char C[4])
{
uchar i;
for (i=0; i<4; i++) //一个周期转30度
{
P2 =((P2|0X0F)&0XFF)&C[i]; //取数据
delay(15); //调节转速
}
}
void motor4(char D[4])
{
uchar i;
for (i=0; i<4; i++) //一个周期转30度
{
P2=(P2|0XF0)&(0X0F|(D[i]<<4)); //取数据
delay(15); //调节转速
}
}
void motor5(char C[4])
{
uchar i;
for (i=0; i<4; i++) //一个周期转30度
{
P3 =((P3|0X0F)&0XFF)&C[i]; //取数据
delay(15); //调节转速
}
}
void motor_M1()
{
I1=1;
I2=0;
I3=1;
I4=0;
if(KEY==0)
{
delay(20);
if(KEY==0)
ENA=1;
else
ENA=0;
}
ENB=1;
}
main() //主程序
{
uint ii;
KEY=1;
while(1)
{
I1=1;
I2=0;
I3=1;
I4=0;
ENA=1;
ENB=1;
while(1) //等待按键按下
{
if(KEY==0)
{
delay(20);
if(KEY==0)
{
ENA=0;
break;
}
}
}
motor1(zheng); motor1(zheng);motor1(zheng);motor1(zheng);motor1(zheng);motor1(zheng);//+180
motor1(fan);motor1(fan);motor1(fan);motor1(fan);motor1(fan);motor1(fan);//-180
motor2(zheng);motor2(zheng);motor2(zheng); //+90
motor2(fan);motor2(fan);motor2(fan); //-90
for(ii=0;ii<120;ii++)// 步进电机3正转10圈
{
motor3(zheng);
}
for(ii=0;ii<492;ii++) //步进电机4正转41圈
{
motor4(zheng);
}
ENB=0; //电机2停转
for(ii=0;ii<396;ii++) //步进电机5正转33圈
{
motor5(zheng);
}
for(ii=0;ii<492;ii++) //步进电机4翻转41圈
{
motor4(fan);
}
ENB=1; //电机2正转
for(ii=0;ii<396;ii++) //步进电机5翻转33圈
{
motor5(fan);
}
}