📄 mainokuse.c
字号:
#include "reg52.h"
#include "intrins.h"
#include "math.h"
#include <absacc.h>
#include "VIIC_C51.h"
#include "ZLG7290.h"
//端口定义
sbit KEY_INT=P3^2;
sbit PA1=P2^0;
sbit PB1=P2^1;
sbit PC1=P2^2;
sbit PA2=P2^4;
sbit PB2=P2^5;
sbit PC2=P2^6;
//For Tlc2543
sbit CLK = P0^0;
sbit DIN = P0^1;
sbit DOUT = P0^2;
sbit CS = P0^3;
#define Pi 3.141593
//#define step (330./1500) //单步距离,mm
//#define step (0.21375) //单步距离,mm
#define step (119/300.) //单步距离,mm
#define pull 0
#define push 1
#define c 1100. //两滑轮间距,mm
#define dx 158. //滑轮到计数点原点投影距离,mm
#define dy 158.
#define minline 2. //最短直线分段
#define motordelay 1 //电机步间延时
#define gate 0x3f //传感器门槛
//#define gate 0x0f //传感器门槛 //I add
#define searth 35 //寻迹步数,35=1cm
//#define searthstep 10 //寻迹最小步子,mm
//#define trace_ru 1 //向右上寻迹
//#define trace_rd 2 //向右下寻迹
//#define trace_lu 3 //向左上寻迹
//#define trace_ld 4 //向左下寻迹
extern unsigned char ZLG7290_GetKey();
void Display(unsigned char *sd); //显示程序
void DelayX10ms(int count); //Delay time=count*10ms
void Delay(unsigned char no);
//void RunA(long num,bit dir); //输入转动的步数、方向,完成电机1转动(相对3相电机)
//void RunB(long num,bit dir); //输入转动的步数、方向,完成电机1转动(相对3相电机)
//float Pointab_A(float x,float y); //坐标变换xy-a
//float Pointab_B(float x,float y); //坐标变换xy-b
//float Pointxy_X(float a,float b); //坐标变换xy-a
//float Pointxy_Y(float a,float b); //坐标变换xy-b
//void Circle(float x,float y,float r); //画圆程序
//void Stepto(float x, float y);
//void Lineto(float x,float y);
//unsigned int ad549(void);
//unsigned int TraceA(void);
//unsigned int TraceB(void);
//void Mypath (void);
//void Go_ru(void); //右上
//void Go_lu(void); //左上
//void Go_rd(void); //右下
//void Go_ld(void); //左下
//unsigned int ad1(void);
//int Disp();
//int flag_trace;//,Ticks; //0:连续; 1:断开
//全局变量
unsigned char key;
unsigned char send[8]={00,00,00,00,00,00,00,00};//定义发送缓冲区 0~3--y 4~6--x 7--sign
float alast,blast,xlast,ylast,xkey,ykey;
unsigned char disp_buf[8]={0,0,0,0,0,0,0,0};
float sita,ad,xo,yo,kk,searthstep,xolast,yolast,Ticks,Traceflag,xbegin,ybegin;
int ll; //for trace
void main()
{
sita=45;
searthstep=5;
Ticks=0;
//xo=xlast;
//yo=ylast;
//int test;
//电机a初始化
PA1=0;
PB1=1;
PC1=1;
//random();
//for(test=0;test<9;test++);
//电机b初始化
PA2=0;
PB2=1;
PC2=1;
Ticks=0;
while (1)
{
static int i;
while(KEY_INT);
key=ZLG7290_GetKey();
if(key==1)i++;
if(key==2)i--;
send[0]=i%10;
send[1]=i/10%10;
send[2]=i/100%10;
send[3]=i/1000%10;
send[7]=key;
Display(send);
}
// Circle(400,500,250);
/* for(;;)
{
P0=0;
P1=0;
P2=0;
P3=0;
_nop_();
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
_nop_();
}
*/
//RunA(3000,1);
//for(;;);
//RunB(2500,0);
//DelayX10ms(1000);
//RunA(1500,pull);
//DelayX10ms(1000);
//RunB(1500,pull);
//Delay(2);
//当前点,以la、lb步数为坐标记,拉减放加
/*
while (1)
{
while(KEY_INT);
key=ZLG7290_GetKey();
ykey=send[0]+send[1]*10+send[2]*100+send[3]*1000;
xkey=send[4]+send[5]*10+send[6]*100;
switch(key)
{
case 1 :send[0]++; //ymm位
if(send[0]==10)
{
send[0]=00;
}
break;
case 2 :send[1]++;//ycm位
if(send[1]==10)
{
send[1]=00;
}
break;
case 3 :send[2]++;//ydm位
if(send[2]==10)
{
send[2]=00;
}
break;
case 4 :send[3]++;//ym位
if(send[3]==2)
{
send[3]=00;
}break;
case 5 :send[4]++;//xmm位
if(send[4]==10)
{
send[4]=00;
}
break;
case 6 :send[5]++;//xcm位
if(send[5]==10)
{
send[5]=00;
}
break;
case 7 :send[6]++;//xdm位
if(send[6]==8)
{
send[6]=00;
}
break;
case 8 :send[7]=1;Lineto(xkey,ykey);send[7]=0; break;
case 9 : //输入当前坐标
xlast=xkey;
ylast=ykey;
alast=Pointab_A(xlast,ylast);
blast=Pointab_B(xlast,ylast);
break;
case 10 :RunB(10,pull); break;
case 11 :RunB(10,push); break;
case 12 :RunA(10,pull); break;
case 13 :RunA(10,push); break;
case 14 :send[7]=4;
Ticks=0;
xo=0;
yo=0;
searthstep=5;
Traceflag=0;
xolast=xkey;
yolast=ykey;
xbegin=xkey;
ybegin=ybegin;
if(send[0]%2==0) TraceB();
else TraceA();
send[7]=0;break;
case 15 :send[7]=3;Mypath();send[7]=0;break;
case 16 :send[7]=2;Circle(xkey,ykey,250);send[7]=0;break;
}
Delay(100);
Display(send);
//if (key!=9) continue;//等待按键,输入坐标
//else break;
}
ylast=send[0]+send[1]*10+send[2]*100+send[3]*1000;
xlast=send[4]+send[5]*10+send[6]*100;
//*/
//xlast=400;
//ylast=500;
//alast=Pointab_A(xlast,ylast);
//blast=Pointab_B(xlast,ylast);
//Lineto(0,0);
Display(send);
//DelayX10ms(1000);
//RunA(4000,1);
//RunB(2500,0);
DelayX10ms(1000);
//for(test=0;test<200;test++)
//{RunA(1,1);Delay(100);}
//RunA(200,0);
//Delay(5000);
//Circle(400,500,250);
//DelayX10ms(1000);
//Mypath();
//Trace();
while(1);
}
void Display(unsigned char *sd)//显示程序
{
disp_buf[0] = sd[0];
disp_buf[1] = sd[1];
disp_buf[2] = sd[2];
disp_buf[3] = sd[3];
disp_buf[4] = sd[4];
disp_buf[5] = sd[5];
disp_buf[6] = sd[6];
disp_buf[7] = sd[7];
ZLG7290_SendBuf(disp_buf,8);
}
void DelayX10ms(int count) //Delay time=count*10ms
{
unsigned int l,m,n;
for(l=0;l<count;l++)
for(m=0;m<12;m++)
for(n=0;n<120;n++);
}
void Delay(unsigned char no)
{
int i,j; //延时参数
for(; no>0; no--)
{
for(i=0; i<10; i++)
for(j=0; j<100; j++)
_nop_();
}
}
void DelayAD(unsigned char no)
{
int i,j; //搞整到轨道中心的每步所用延时参数,消除跳跃
for(; no>0; no--)
{ for(j=0; j<100; j++)
_nop_();
}
}
//*/
/*
unsigned int Trace (void) //将物体直线运动 5mm*200
{
int i;
unsigned ad;
ll++;
ll%=38;
if(ll==37)searthstep+=5;
if(searthstep>20||Traceflag>0)
{
Traceflag++;
return 0;
}
ad=0;
for(i=0;i<20;i++)ad+=(float)(ad549());
ad/=20;
send[0]=ad%10;
send[1]=ad/10%10;
send[2]=ad/100%10;
send[3]=0;
send[4]=13;
send[5]=10;
send[6]=0;
Display(send);
DelayX10ms(1);
Ticks++;
if(Ticks==10)Ticks=5;
if(ad<gate)
{
//*
if( (sqrt( (xlast-xolast)*(xlast-xolast)-((ylast-yolast)*(ylast-yolast)))<10)&&(Ticks>1))
{
sita+=35;
Lineto(xo+searthstep*sin(sita/360*2*Pi),yo+searthstep*cos(sita/360*2*Pi));
Trace();
if(Traceflag>0)return 0;
}
else
//
{
//测试
//sita=45;
Lineto(xo+searthstep*sin(45/360*2*Pi),yo+searthstep*cos(45/360*2*Pi));
xolast=xo;
yolast=yo;
xo=xlast;
yo=ylast;
searthstep=5;
Trace();
if(Traceflag>0)return 0;
}
}
else
{
sita+=35;
Lineto(xo+searthstep*sin(atan((yo-ybegin)/(xo-xbegin))/360*2*Pi),yo+searthstep*cos((yo-ybegin)/(xo-xbegin)/360*2*Pi));
Trace();
if(Traceflag>0)return 0;
}
return 0;
}
//*/
/*
unsigned int Trace (void) //将物体直线运动 5mm*200
{
int i;
//while(;;)
{
while(ad1()<gate) RunA(1,pull);
for(i=0;i<80;i++)
{
RunB(1,pull);
if(ad549()<gate)break;
}
if(i==80)for(i=0;i<160;i++)
{
RunB(1,push);
if(ad549()<gate)break;
}
if(i==160)
{
RunB(80,pull);
RunA(65,pull);
yo++;
}
for(i=0;i<80;i++)
{
RunA(1,pull);
if(ad549()<gate)break;
}
if(i==80)for(i=0;i<160;i++)
{
RunA(1,push);
if(ad549()<gate)break;
}
if(i==160)
{
RunA(80,pull);
//RunB(45,push);
//yo++;
}
Trace();
xo++;
if(xo>20||yo>=3)return ;
}
}
//*/
/*
void Go_ru(bit dir) //右上
{
/* int i,i1;
i=ad549();
send[0]=i%10;
send[1]=i/10%10;
send[2]=i/100%10;
send[3]=i/1000%10;
send[4]=13;
send[5]=10;
send[6]=0;
Display(send);
if(flag_trace<97)return ;
if(dir==0)
{
RunA(10,push);
if(ad549()<gate)//正常
{
Go_ru(1);
flag_trace=99;
}
else //没找到
{
RunB(20,push); //zhaohuilailiao
if(ad549()<gate)//正常
{
Go_rd(1);
flag_trace=99;
}
else //没找到
{
RunB(20,pull); //tuihuiqu
Go_rd(0);
flag_trace--;
}
}
}
else
{
RunB(10,pull);
if(ad549()<gate)//正常
{
Go_ru(0);
flag_trace=99;
}
else //没找到
{
RunB(20,pull); //zhaohuilailiao
if(ad549()<gate)//正常
{
Go_ru(0);
flag_trace=99;
}
else //没找到
{
RunB(20,pull); //tuihuiqu
Go_rd(0);
flag_trace--;
}
}
}
}
/*
int i,i1;
i=ad549();
send[0]=i%10;
send[1]=i/10%10;
send[2]=i/100%10;
send[3]=i/1000%10;
send[4]=13;
send[5]=10;
send[6]=0;
Display(send);
if(flag_trace<97)return ;
while(ad549()<gate)RunB(10,pull); //右
for(i=0;i<searth;i++)while(ad549()>gate)RunA(1,pull);//上
if(i>=searth) //没找到
{
for(i=0;i<searth*2;i++)while(ad549()>gate)RunA(1,push); //下
if(i>=searth*2) //没找到
{
i1=i;
for(i=0;i<searth*2;i++)RunA(1,pull); //退回去,上
flag_trace--;
Go_lu(); //走左上
}
else Go_rd(); //找到,走右下
}
else
{
Go_ru();//找到,走右上
flag_trace=99;
}
//*/
//}
/*
void Go_rd(bit dir) //右下
{
/* int i,i1;
i=ad549();
send[0]=i%10;
send[1]=i/10%10;
send[2]=i/100%10;
send[3]=i/1000%10;
send[4]=13;
send[5]=10;
send[6]=0;
Display(send);
if(flag_trace<97)return ;
if(dir==0)
{
RunB(10,pull);
if(ad549()<gate)//正常
{
Go_ru(1);
flag_trace=99;
}
else //没找到
{
RunB(20,pull); //zhaohuilailiao
if(ad549()<gate)//正常
{
Go_ru(0);
flag_trace=99;
}
else //没找到
{
RunB(20,pull); //tuihuiqu
Go_rd(0);
flag_trace--;
}
}
}
}
/*
int i,i1;
//flag_trace=trace_rd;
i=ad549();
send[0]=i%10;
send[1]=i/10%10;
send[2]=i/100%10;
send[3]=i/1000%10;
send[4]=13;
send[5]=10;
send[6]=0;
Display(send);
if(flag_trace<97)return ;
while(ad549()<gate)RunB(10,pull); //右
for(i=0;i<searth;i++)while(ad549()>gate)RunA(1,push);//下
if(i>=searth) //没找到
{
for(i=0;i<searth*2;i++)while(ad549()>gate)RunA(1,pull); //上
if(i>=searth*2) //没找到
{
i1=i;
for(i=0;i<searth*2;i++)RunA(1,push); //退回去,下
flag_trace--;
Go_ld(); //走左下
}
else Go_ru(); //找到,走右上
}
else
{
Go_rd();//找到,走继续右下
flag_trace=99;
}
//*/
//}
/*
void Go_ld(bit dir) //左下
{
int i,i1;
//flag_trace=trace_ld;
i=ad549();
send[0]=i%10;
send[1]=i/10%10;
send[2]=i/100%10;
send[3]=i/1000%10;
send[4]=13;
send[5]=10;
send[6]=0;
Display(send);
if(flag_trace<97)return ;
while(ad549()<gate)RunB(10,push); //右左
for(i=0;i<searth;i++)while(ad549()>gate)RunA(1,push);//下
if(i>=searth) //没找到
{
for(i=0;i<searth*2;i++)while(ad549()>gate)RunA(1,push); //上
if(i>=searth*2) //没找到
{
i1=i;
for(i=0;i<searth*2;i++)RunA(1,pull); //退回去,下
flag_trace--;
Go_rd(); //走右下
}
else Go_ru(); //找到,走右上
}
else
{
Go_ld();//找到,走左下
flag_trace=99;
}
}
void Go_lu(bit dir) //左上
{
int i,i1;
//flag_trace=trace_lu;
i=ad549();
send[0]=i%10;
send[1]=i/10%10;
send[2]=i/100%10;
send[3]=i/1000%10;
send[4]=13;
send[5]=10;
send[6]=0;
Display(send);
if(flag_trace<97)return ;
while(ad549()<gate)RunB(10,push); //左
for(i=0;i<searth;i++)while(ad549()>gate)RunA(1,pull);//上
if(i>=searth) //没找到
{
for(i=0;i<searth*2;i++)while(ad549()>gate)RunA(1,push); //下
if(i>=searth*2) //没找到
{
i1=i;
for(i=0;i<searth*2;i++)RunA(1,pull); //退回去,上
flag_trace--;
Go_ru(); //走右上
}
else Go_rd(); //找到,走
}
else
{
Go_lu();//找到,走左上
flag_trace=99;
}
}
//*/
unsigned int ad549(void)
{
unsigned char AD1=0;
unsigned char i;
unsigned char AD2=0x04;
CS = 1;
CLK = 0;
DOUT= 1; /* Initialize for Read */
CS = 0; /* Enable and Drive out Bit9 */
for(i=0;i<8;i++)
{
AD1 = AD1<<1;
DIN=(bit)(AD2&0x80);
if( DOUT ) AD1 = AD1|0x01;
AD2<<=1;
CLK=1;
_nop_();
_nop_();
CLK=0; /* failing edge drive out bit */
_nop_();
_nop_();
}
//AD1 = AD1>>1;
CS = 1;
for(i=0;i<20;i++); /* delay >21us */
return ((unsigned int)(AD1));
}
int Disp()
{
send[0]=(long)ylast%10;
send[1]=(long)ylast/10%10;
send[2]=(long)ylast/100%10;
send[3]=(long)ylast/1000%10;
send[4]=(long)xlast%10;
send[5]=(long)xlast/10%10;
send[6]=(long)xlast/100%10;
Display(send);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -