📄 free.c
字号:
#include<reg52.h>
#include<stdio.h>
#include<absacc.h>
#include<math.h>
#pragma REGPARMS
//定义为变量
sbit cpl=P1^4;
sbit dir_left=P1^5;
sbit cpr=P1^6;
sbit dir_righ=P1^7;
/*unsigned char data ttm;
unsigned int data ttmh;*/
unsigned char firstnode[2];
unsigned char secondnode[2];
unsigned char finalnode[2];//给定坐标
unsigned char xdata DGS _at_ 0xe200;
unsigned char xdata DGD _at_ 0xe400;
unsigned char xdata PKEY _at_ 0xe000;
void led(void);
unsigned char key_value(void);
int axis_value(void);
void jetfree(void);
float line_k(void);
int cacul_pull(void);
int cacul_pulr(void);//计算脉冲数
void motor(int pul_ln,int pul_rn);//电机转动程序
extern unsigned char delay2(unsigned char);
extern unsigned char ldseg2(unsigned char);
//主程序
void main(void)
{ firstnode[0]=0;
firstnode[1]=0;
while(1)
{led();
jetfree();
}
}
unsigned char key_value(void)//0~F
{
unsigned char data x1,x2,x3,x4;
x1=4;
x3 = 0xFF;
for(x2=0x08;x2>=1;x2=x2/2)//0x80 0x40 0x20 0x10 0x08 0x04 0x02 0x01
{
DGD=x2; //选择行
DGS = 0x00;
x4=15-(PKEY&15);//
if(x4!=0)
{
x3=(7-x1)*4;
while(x4!=1){x3++;x4/=2;}
}
x1++;
}
return(x3);
}
int axis_value(void)
{unsigned char m,n,o,p=16,i,b;int c;
while(p>=10)
{p=key_value();}m=p;
while(p<=9)
{p=key_value();}
while(p>=10)
{for(i=0;i<6;i++){DGD=0x01;DGS=ldseg2(m);delay2(50);}p=key_value();}
n=p;
while(p<=9)
{p=key_value();}
while(p>=10)
{for(i=0;i<6;i++){DGD=0x01;DGS=ldseg2(m);delay2(50);DGD=0x02;DGS=ldseg2(n);delay2(50);}p=key_value();}
o=p;
while(p<=9)
{p=key_value();}
while(p>=10)
{for(i=0;i<20;i++){DGD=0x01;DGS=ldseg2(m);DGD=0x02;DGS=ldseg2(n);
DGD=0x10;DGS=ldseg2(o);delay2(50);} p=key_value();}b=p;
c=1000*m+100*n+10*o+b;
return c;
}
void led(void)
{int a,b,c,d,e;char p;
e=axis_value();
a=e/1000;
b=e/100-10*a;
d=e%10;
c=(e%100-d)/10;
while(p!=10)
{DGD=0x01;DGS=ldseg2(a);delay2(20);
DGD=0x02;DGS=ldseg2(b);delay2(20);
DGD=0x10;DGS=ldseg2(c);delay2(20);
DGD=0x20;DGS=ldseg2(d);delay2(20);p=key_value();
}finalnode[0]=a*10+b;finalnode[1]=c*10+d;
}
//自由定坐标运动函数
void jetfree(void)
{ float lk;
char cirst;
int i,x,pull,pulr;
while( cirst!=12)//定义12键为yetfree的送数键
{
cirst=key_value();
}
x=finalnode[0]-firstnode[0];
lk=line_k();
for(i=0;i<abs(x/0.2);i++)
{
secondnode[0]=firstnode[0]+0.2;
secondnode[1]=firstnode[1]+0.2*lk;
pull=cacul_pull();
pulr=cacul_pulr();
motor(pull,pulr);//电机转动程序
firstnode[0]=secondnode[0];
firstnode[1]=secondnode[1];
}
}
float line_k(void)//计算斜率
{
float k;
k=(finalnode[1]-firstnode[1])/(finalnode[0]-firstnode[0]);
return(k);
}
int cacul_pull(void)//计算脉冲数
{
int pulnum;
float len1,len2;
len1=sqrt((115-firstnode[1])*(115-firstnode[1])+(firstnode[0]+15)*(firstnode[0]+15));
len2=sqrt((115-secondnode[1])*(115-secondnode[1])+(secondnode[0]+15)*(secondnode[0]+15));
pulnum=(len2-len1)/0.009;
return(pulnum);
}
int cacul_pulr(void)//计算脉冲数
{
int pulnum;
float len1,len2;
len1=sqrt((115-firstnode[1])*(115-firstnode[1])+(firstnode[0]-95)*(firstnode[0]-95));
len2=sqrt((115-secondnode[1])*(115-secondnode[1])+(secondnode[0]-95)*(secondnode[0]-95));
pulnum=(len2-len1)/0.009;
return(pulnum);
}
void motor(int pul_ln,int pul_rn)//电机转动程序
{
int a,i,j;
if(pul_ln>0)
dir_left=1;//设定高电平为正传
else
{pul_ln=-pul_ln;
dir_left=0;}
if(pul_rn>0)
dir_righ=1;//设定高电平为正传
else
dir_righ=0;
a=(pul_ln>pul_rn?pul_rn:pul_ln);
for(i=a;i>0;i--)
{ for(j=500;j>0;j--);
cpl=!cpl;
cpr=!cpr;}
if(pul_ln>pul_rn)
{ for(i=pul_ln-a;i>0;i--)
{ for(j=500;j>0;j--);
cpl=!cpl;
}
}
else
{ for(i=pul_ln-a;i>0;i--)
{ for(j=500;j>0;j--);
cpr=!cpr;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -