📄 che.c
字号:
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define sudu1 350
#define long 200
#define short 30
//定义端口位
sbit P1_0=P1^0;//电源指示灯
sbit A3=P1^1;//第三个步进电机
sbit B3=P1^2;
sbit C3=P1^3;
sbit D3=P1^4;
sbit A1=P2^0;//第一二个步进电机
sbit B1=P2^1;
sbit C1=P2^2;
sbit D1=P2^3;
sbit A2=P2^4;
sbit B2=P2^5;
sbit C2=P2^6;
sbit D2=P2^7;
void delay(int);
void qidong();
void motor_move1(uint);
void motor_move2(uint);//正转
void motor_move3(uint);//左转
void motor_move4(uint);//右转
void motor_ahead(uint,uint);
void motor_right(uint,uint);
void motor_left(uint,uint);
void motor_li(uint,uint);
//bit status=0;
void main()
{
P1=0xff;
P2=0xff;
P1_0=0; //开机
EA=0; //总中断使能
EX0=0; //使能外部中断0
delay(20000);
delay(20000);
while(1)
{
qidong();//循环体
}
}
void qidong()
{
motor_li(6,sudu1);//犁放
P1=0xfe;
P2=0xff;
delay(20000);
motor_ahead(long,sudu1);//长
P1=0xfe;
P2=0xff;
delay(20000);
motor_li(6,sudu1);//犁起
P1=0xfe;
P2=0xff;
delay(20000);
motor_right(65,sudu1);
P1=0xfe;
P2=0xff;
delay(20000);
motor_ahead(short,sudu1);//短
P1=0xfe;
P2=0xff;
delay(20000);
motor_right(65,sudu1);
P1=0xfe;
P2=0xff;
delay(20000);
motor_li(6,sudu1);//犁放
P1=0xfe;
P2=0xff;
delay(20000);
motor_ahead(long,sudu1);//长
P1=0xfe; //端口恢复
P2=0xff;
delay(20000);
motor_li(6,sudu1);//犁起
P1=0xfe;
P2=0xff;
delay(20000);
motor_left(65,sudu1);
P1=0xfe;
P2=0xff;
delay(20000);
motor_ahead(short,sudu1);//短
P1=0xfe;
P2=0xff;
delay(20000);
motor_left(65,sudu1);
P1=0xfe;
P2=0xff;
delay(20000);
}
void motor_move1(uint speed) //
{
A3=0;
B3=1;
C3=1;
D3=1;delay(speed);
A3=0;
B3=0;
C3=1;
D3=1;delay(speed);
A3=1;
B3=0;
C3=1;
D3=1;delay(speed);
A3=1;
B3=0;
C3=0;
D3=1;delay(speed);
A3=1;
B3=1;
C3=0;
D3=1;delay(speed);
A3=1;
B3=1;
C3=0;
D3=0;delay(speed);
A3=1;
B3=1;
C3=1;
D3=0;delay(speed);
A3=0;
B3=1;
C3=1;
D3=0;delay(speed);
}
void motor_move2(uint speed) //步进电机正转
{
P2=0xe6;delay(speed);
P2=0xc7;delay(speed);
P2=0xd3;delay(speed);
P2=0x9b;delay(speed);
P2=0xb9;delay(speed);
P2=0x3d;delay(speed);
P2=0x7c;delay(speed);
P2=0x6e;delay(speed);
}
void motor_move3(uint speed) //左转
{
P2=0xef;delay(speed);
P2=0xcf;delay(speed);
P2=0xdf;delay(speed);
P2=0x9f;delay(speed);
P2=0xbf;delay(speed);
P2=0x3f;delay(speed);
P2=0x7f;delay(speed);
P2=0x6f;delay(speed);
}
void motor_move4(uint speed) //右转
{
P2=0xf6;delay(speed);
P2=0xf7;delay(speed);
P2=0xf3;delay(speed);
P2=0xfb;delay(speed);
P2=0xf9;delay(speed);
P2=0xfd;delay(speed);
P2=0xfc;delay(speed);
P2=0xfe;delay(speed);
}
void motor_li(uint distance,uint speed)
{
uint temp=distance;
while(temp--)motor_move1(speed);
}
void motor_ahead(uint distance,uint speed)
{
uint temp=distance;
while(temp--)motor_move2(speed);
}
void motor_right(uint distance,uint speed)
{
uint temp=distance;
while(temp--) motor_move4(speed);
}
void motor_left(uint distance,uint speed)
{
uint temp=distance;
while(temp--)motor_move3(speed);
}
void delay( int x1)
{
unsigned int y1;
for (y1=1;y1<x1;y1++);
}
/*在每个电机工作完以后(应该对每个电机进行复位加低电平)
然后 在 进行下一步操作 否则电机会工作不正常 第一二个电机的时间
的延迟还要改 延迟时间应该在1ms左右 第三个工作正常 第一二个还要改进
电机的工作顺序还不对
电机转速太慢
*/
/*
还需要改进的地方:第三个步进电机每次运行只能转半圈,
第一二个步进电机在转弯时每次要正好转90度
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -