📄 main.txt
字号:
#include <reg51.h>
#include <lcd.h>
#include "stdio.h"
#include "string.h"
#include "absacc.h"
#include "intrins.h"
#define SMOTOR 40 //步进电机走步脉冲周期 = 50*10
#define XIUZHENG 290//位置传感器的修正值
//define stepmotor 0 IO
sbit motor0sp = P1^1;//0号电机走步脉冲
sbit motor0dir = P1^0;//0号电机转向
//define stepmotor 1 IO
sbit motor1sp = P1^3; //1号电机走步脉冲
sbit motor1dir = P1^2;//1号电机转向
sbit weizhi = P3^2;//位置
sbit jixieshou = P2^6;//机械手
sbit beep = P3^5;//蜂鸣器
/////////////////////////////
sbit KEY1 = P1^4;//键//
sbit KEY2 = P1^5;// //
sbit KEY3 = P1^6;// //
sbit KEY4 = P1^7;//盘//
/////////////////////////////
void Delay(int time);
void stepmotor(unsigned char motorNo,unsigned char motordir,int bushu);
void stepmotortongbu(unsigned char motrodir,int bushu);
int stepmotor0(void);
void functionone(void);
int jianceyellow(void);
unsigned char yanseshibie(void);
void Interface0(void);
void Interface1(void);
void Interface2(void);
void Interface3(void);
void disp_zuobiao(void);
unsigned int pyellow = 0;//三种颜色球的坐标,横坐标以步数表示
unsigned int pred = 0;
unsigned int pgreen = 0;
////////////////////////////////////////////////
unsigned char buffer; //程序中用到的数据缓冲区//
int buffer0; //
////////////////////////////////////////////////
unsigned char InterfaceNo = 1;//菜单编号
unsigned char disp[3],dispbuf[5][5];//显示缓冲区
///////////////主程序开始/////////////////
void main(void)
{
jixieshou = 0;
disp_int(); //初始化函数
disp_clear(); //清屏函数
beep = 0;
Interface0();
stepmotor(1,0,3900);//////从规定位置上升到顶部
while(1)
{
if(~KEY1)///////C键
{
Delay(1000);
if(~KEY1)
{
switch(InterfaceNo)
{
case 10:
stepmotortongbu(1,10);
break;
case 11:
InterfaceNo = 0;
break;
case 12:
InterfaceNo = 1;
break;
case 13:
InterfaceNo = 1;
break;
default : break;
}
}
}
////////////////////////
if(~KEY2)////////按键1
{
Delay(1000);
if(~KEY2)
{
switch(InterfaceNo)
{
case 10:
InterfaceNo = 1;
break;
case 11:
functionone();
break;
case 12:
disp_string(13,3,"Ing");
buffer0 = 0;
stepmotortongbu(1,pyellow);//吊yellow球
Delay(20000);
stepmotor(1,1,6100);
Delay(10000);
jixieshou = 1;
Delay(10000);
stepmotor(1,0,2500);
Delay(20000);
motor0dir = 0;//步进电机同步向右运行
motor1dir = 1;
while(weizhi)
{
motor0sp = 1;
motor1sp = 1;
Delay(SMOTOR);
motor0sp = 0;
motor1sp = 0;
Delay(SMOTOR);
buffer0++;
//if(bushu > bushu+1900) return 1;//出错,再检测一遍球筒的位置
}
//stepmotortongbu(0,XIUZHENG);//无需修正
Delay(20000);
//stepmotor(1,1,2500);
//Delay(10000);
jixieshou = 0;
Delay(10000);
stepmotor(1,0,3600);
Delay(10000);
stepmotortongbu(1,(buffer0 - pyellow));
disp_string(13,3," OK");
beep = 1;
Delay(5000);
beep = 0;
break;
case 13: //////////////////修改
stepmotor(1,0,10);
//stepmotortongbu(1,50);
break;
default : break;
}
}
}
/////////////////////////
if(~KEY3)///////按键2
{
Delay(1000);
if(~KEY3)
{
switch(InterfaceNo)
{
case 10:
InterfaceNo = 1;
break;
case 11:
InterfaceNo = 2;
break;
case 12:
disp_string(13,5,"Ing");
buffer0 = 0;
stepmotortongbu(1,pred);//吊red球
Delay(20000);
stepmotor(1,1,6100);
Delay(10000);
jixieshou = 1;
Delay(10000);
stepmotor(1,0,2500);
Delay(20000);
motor0dir = 0;//步进电机同步向右运行
motor1dir = 1;
while(weizhi)
{
motor0sp = 1;
motor1sp = 1;
Delay(SMOTOR);
motor0sp = 0;
motor1sp = 0;
Delay(SMOTOR);
buffer0++;
//if(bushu > bushu+1900) return 1;//出错,再检测一遍球筒的位置
}
//stepmotortongbu(0,XIUZHENG);//无需修正
Delay(20000);
//stepmotor(1,1,2500);
// Delay(10000);
jixieshou = 0;
Delay(10000);
stepmotor(1,0,3600);
Delay(10000);
stepmotortongbu(1,(buffer0 - pred));
disp_string(13,5," OK");
beep = 1;
Delay(5000);
beep = 0;
Delay(5000);
beep = 1;
Delay(5000);
beep = 0;
break;
case 13:///////////////////
stepmotor(1,1,10);
//stepmotortongbu(0,50);
break;
default : break;
}
}
}
///////////////////////////
if(~KEY4)////按键3
{
Delay(1000);
if(~KEY4)
{
switch(InterfaceNo)
{
case 10:
InterfaceNo = 1;
break;
case 11:
InterfaceNo = 3;
break;
case 12:
disp_string(13,7,"Ing");
buffer0 = 0;
stepmotortongbu(1,pgreen);//吊绿球
Delay(20000);
stepmotor(1,1,6100);
Delay(10000);
jixieshou = 1;
Delay(10000);
stepmotor(1,0,2500);
Delay(20000);
motor0dir = 0;//步进电机同步向右运行
motor1dir = 1;
while(weizhi)
{
motor0sp = 1;
motor1sp = 1;
Delay(SMOTOR);
motor0sp = 0;
motor1sp = 0;
Delay(SMOTOR);
buffer0++;
//if(bushu > bushu+1900) return 1;//出错,再检测一遍球筒的位置
}
//stepmotortongbu(0,XIUZHENG);//无需修正
Delay(20000);
//stepmotor(1,1,2500);
//Delay(10000);
jixieshou = 0;
Delay(10000);
stepmotor(1,0,3600);
Delay(10000);
stepmotortongbu(1,(buffer0-pgreen));
disp_string(13,7," OK");
beep = 1;
Delay(5000);
beep = 0;
Delay(5000);
beep = 1;
Delay(5000);
beep = 0;
Delay(5000);
beep = 1;
Delay(5000);
beep = 0;
break;
case 13:
stepmotortongbu(0,10);
break;
default : break;
}
}
}
/////////////////////////////////
switch(InterfaceNo)
{
case 0:
InterfaceNo = 10;
Interface0();
break;
case 1:
InterfaceNo = 11;
Interface1();
break;
case 2:
InterfaceNo = 12;
Interface2();
buffer = yanseshibie();
disp_zuobiao();
InterfaceNo = 12;
break;
case 3:
InterfaceNo = 13;
Interface3();
default :
break;
}
}//end while
}
/******************************************************************************/
//函数功能:识别三种颜色球的位置,机械手位于60cm处顶端
//入口参数:无
//出口参数:全局变量
/******************************************************************************/
unsigned char yanseshibie(void)
{
unsigned int temp1,temp2,temp3;
/////////////////////////////////////////////////////////
/////////////////识别黄色////////////////////////////////
/////////////////////////////////////////////////////////
pyellow = 0;//位置清零
stepmotor(1,1,3850); //从初始位置下降3850移动到检测黄色位置的高度
Delay(20000);
Delay(10000);
motor0dir = 1;//步进电机同步向左运行
motor1dir = 0;
while(weizhi)
{
motor0sp = 1;
motor1sp = 1;
Delay(SMOTOR);
motor0sp = 0;
motor1sp = 0;
Delay(SMOTOR);
if(pyellow >= 3400)
{
Delay(20000);
stepmotortongbu(0,3400);
stepmotor(1,0,3800);
return 0;
}
pyellow++;
}
//stepmotortongbu(1,XIUZHENG);
Delay(20000);
Delay(20000);
stepmotortongbu(0,pyellow);//回到初始位置
pyellow += XIUZHENG;//修正
Delay(20000);
Delay(20000);
stepmotor(1,1,150);//下降
//识别红色
Delay(20000);
Delay(20000);
pred = 0;
//stepmotor(1,1,4100);//从初始位置上升移动到检测红球的高度
Delay(20000);
Delay(10000);
motor0dir = 1;//步进电机同步向左运行
motor1dir = 0;
while(weizhi)
{
motor0sp = 1;
motor1sp = 1;
Delay(SMOTOR);
motor0sp = 0;
motor1sp = 0;
Delay(SMOTOR);
if(pred >= 3400)
{
Delay(20000);
stepmotortongbu(0,3400);
stepmotor(1,0,3950);
return 0;
}
pred++;
}
if(pred < (pyellow-500))
{
Delay(20000);
Delay(20000);
stepmotortongbu(0,pred);
pred += XIUZHENG;
Delay(20000);
Delay(20000);
stepmotor(1,1,250);
}
else
{
stepmotortongbu(1,700);//跳过黄球
pred += 700;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -