📄 利用c8051f单片机控制的铣床模型(直线圆弧插补算法).txt
字号:
利用C8051F单片机控制的铣床模型(直线圆弧插补算法)
利用C8051F单片机控制的铣床模型(直线圆弧插补算法)
收集:杨本荣 来源: 网络 发表于 2005-9-3
#include <c8051f000.h>
# include <math.h>
/*******画圆弧延时70个单位,画正交直线延时7个单位,画斜线延时70个单位 */
unsigned char DELTIM;
sbit p12=P1^2;
sbit p13=P1^3;
bdata unsigned char dddd;
void delay(unsigned long tim );
void stepx (unsigned long sx,int szf);
void stepy (unsigned long sx,int szf);
void stepxy(long xe,ye);//任意直线插补,设当前为坐标原点。
void stepz(int sz);
void arcn13s24 (int x0,int y0,int xe,int ye,int fx,int
fy)//第一象限逆圆弧
{ int f=0,n,nmax,x,y; x=x0;y=y0;
nmax=labs(xe-x0)+labs(ye-y0);
for (n=0;n<nmax;n++)
{if (f>=0){stepx(1,fx);f=f-2*x+1;x-=1;}
else {stepy(1,fy);f+=2*y+1; y+=1;}}
}
void arcs13n24 (int x0,int y0,int xe,int ye,int fx,int
fy)//第一象限逆圆弧
{ int f=0,n,nmax,x,y; x=x0;y=y0;
nmax=labs(xe-x0)+labs(ye-y0);
for (n=0;n<nmax;n++)
{if (f>=0){stepy(1,fx);f=f-2*y+1;y-=1;}
else {stepx(1,fy);f+=2*x+1; x+=1;}}
}
void linxyz(int xe,ye,ze)
{ int n,x=0,y=0,z=0,fx,fy,fz,ne;
if (xe<0){ fx=-1;} else{ fx=1;}
if (ye<0){ fy=-1;} else{ fy=1;}
if (ze<0){ fz=-1;} else{fz=1;}
xe=abs(xe);ye=abs(ye);ze=abs(ze);
ne=xe>ye ? xe : ye; ne=ne>ze ? ne : ze;
for (n=0;n<ne;n++)
{ if ((x*ne)<=(n*xe)){stepx(1,fx); x+=1;}
if ((y*ne)<=(n*ye)){stepy(1,fy); y+=1;}
if ((z*ne)<=(n*ze)){stepz(fz); z+=1;}
}}
void linxy(int xe,int ye)
{int n,x=0,y=0,ne,fx,fy;
if (xe<0){ fx=-1;} else{ fx=1;}
if (ye<0){ fy=-1;} else{ fy=-1;}
xe=abs(xe);ye=abs(ye);
ne=xe>ye ? xe : ye;
for (n=0;n<ne;n++)
{ if ((x*ne)<=(n*xe)){stepx(1,fx); x+=1;}
if ((y*ne)<=(n*ye)){stepy(1,fy); y+=1;} }
}
main()
{
WDTCN = 0xde;
WDTCN = 0xad;
for ( ; ; )
{ DELTIM=8;
stepx(12000,-1);
stepy(15000,-1);
stepx(2000,1);
stepx(5000,1);
DELTIM=70;
arcs13n24(0,1500,1500,0,1,1); // 逆四
DELTIM=7;
stepy(5000,1);
DELTIM=70;
arcn13s24(1500,0,0,1500,-1,1); // 逆一
DELTIM=7;
stepx(5000,-1);
DELTIM=70;
arcs13n24(0,1500,1500,0,-1,-1); // 逆二
DELTIM=7;
stepy(5000,-1);
DELTIM=70;
arcn13s24(1500,0,0,1500,1,-1); // 逆三
DELTIM=7;
stepx(2000,1);
stepy(2000,1);
DELTIM=70;
arcn13s24(1500,0,0,1500,1,-1); // 逆三 莲花程序
arcn13s24(1500,0,0,1500,-1,1); // 逆一
arcs13n24(0,1500,1500,0,-1,-1); // 逆二
arcs13n24(0,1500,1500,0,1,1); // 逆四
arcn13s24(1500,0,0,1500,1,1); // 顺二
arcn13s24(1500,0,0,1500,-1,-1); // 顺四
arcs13n24(0,1500,1500,0,1,-1); // 顺三
arcs13n24(0,1500,1500,0,-1,1); // 顺一
DELTIM=7;
stepx(1500,1);
stepy(1500,1);
stepx(3000,-1);
stepy(3000,-1);
stepx(3000,1);
stepy(1500,1);
} }
void stepxy(long xe,ye)//任意直线插补,设当前为坐标原点。
{ long n,f=0;
for (n=0;n<labs(xe)+labs(ye);n++)
{if (f>=0){ stepx(1,xe>0?1:-1);f-=labs(ye);}
else {stepy(1,ye>0?1:-1);f+=labs(xe);}
}
}
/*void stepz(int sz)
{code unsigned char
ddd[]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6} ;
static char di;
unsigned int i;
for (i=0;i<abs(sz);i++)
{
if (di>7) di=0;if (di<0) di=7;
P0=ddd[di];
if (sz<0){di--;} else {di++;}
delay(DELTIM);
}}*/
void stepy (unsigned long sx,int szf)//Y轴直线
{static code unsigned char
ddd[]={0x8,0x10,0x20,0x08,0x10,0x20} ;
static unsigned long sa;
static unsigned char p1bak;
static int di;
for (sa=0;sa<sx;sa++)
{ di+=szf ;
if (di>5) di=0; if (di<0) di=5;
p1bak=P1; p1bak=p1bak&0xc7; p1bak=p1bak|ddd[di];
P1=p1bak; delay(DELTIM);
} }
void stepx (unsigned long sx,int szf)//X轴直线
{static code unsigned char ddd[]={0x1,0x2,0x4,0x1,0x2,0x4} ;
static unsigned long sa;
static unsigned char p1bak;
static int di;
for (sa=0;sa<sx;sa++)
{ di+=szf ;
if (di>5) di=0; if (di<0) di=5;
p1bak=P1; p1bak=p1bak&0xf8; p1bak=p1bak|ddd[di];
P1=p1bak; delay(DELTIM);
} }
void delay(unsigned long tim)
{unsigned long tt;
for (tt=0;tt<tim;tt++);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -