基于51单片机的XY轴激光打印机电路原理图,内含51单片机原理图相关的库文件,以及该打印机的测试代码。
原理图
单片机源程序如下:
- #include<reg52.h>
- #include<math.h>
- #include<data.h>
- #include<delay.h>
- #include<delayms.h>
- #define work_pl 2000 //计时器初值
- #define dl 71.5 //脉冲当量
- #define wid 10.0 //数字字宽
- #define wid_1 130.0 //汉字字宽
- #define rad 15 //所打印圆半径
- #define len 33 //所打印圆的圆心基点
- #define high 1.73205 //根号3对应常数
- #define on 0 //激光开关
- #define off 1
- sbit x_move=P2^4;
- sbit x_direct=P2^5;
- sbit y_move=P2^2;
- sbit y_direct=P2^3;
- sbit light=P2^0; //激光开关
- sbit turnlight=P2^1; //打印开始或结束
- sbit x_stp=P1^1; //限位开关
- sbit y_stp=P1^0;
- sbit start=P1^3; //启动开关
- sbit contin=P1^4; //暂停
- // 脉冲发生器
- void time0make(unsigned char a,unsigned char b)
- {
- TMOD=0x01;
- TH0=(65536-work_pl)/256;
- TL0=(65536-work_pl)%256;
- EA=1;
- TR0=1;
- while(1)
- {
- if(TF0)
- {
- TF0=0;
- if(a==0)
- {
- x_direct=b;
- x_move=!x_move;
- TH0=(65536-work_pl)/256;
- TL0=(65536-work_pl)%256;
- while(!TF0);
- x_move=!x_move;
- TF0=0;
- return;
- }
- else
- {
- y_direct=b;
- y_move=!y_move;
- TH0=(65536-work_pl)/256;
- TL0=(65536-work_pl)%256;
- while(!TF0);
- y_move=!y_move;
- TF0=0;
- return;
- }
- TH0=(65536-work_pl)/256;
- TL0=(65536-work_pl)%256;
- while(!TF0)
- TF0=0;
- return;
- }
- }
- }
- //直线插补
- void math_zhixian(float x_e,float y_e)
- {
- float f0,x0,y0,sum;
- unsigned char a,b;
- if(x_e>=0&&y_e>=0) //相对坐标象限判断
- {a=1;b=1;}
- else if(x_e<=0&&y_e>=0)
- {a=0;b=1;}
- else if(x_e<=0&&y_e<=0)
- {a=0,b=0;}
- else
- {a=1,b=0;};
- x0=fabs(dl*x_e);
- y0=fabs(dl*y_e);
- f0=0;
- sum=x0+y0;
- while(sum>=0)
- {
- if(f0>=0)
- {
- time0make(0,a);
- f0=f0-y0;
- sum=sum-1;
- }
- else
- {
- time0make(1,b);
- f0=f0+x0;
- sum=sum-1;
- }
- }
- }
- //第一象限逆圆弧插补
- void math_yuanhu(float x0,float y0,float x1,float y1)
- {
- float f0,sum,temp;
- f0=0;
- x0=dl*x0;
- y0=dl*y0;
- x1=dl*x1;
- y1=dl*y1;
- sum=x0+y1;
- temp=sum;
- while(sum>0) //第一象限
- {
- if(f0>=0)
- {
- time0make(0,0);
- f0=f0-2*x0+1;
- x0=x0-1;
- sum=sum-1;
- }
- else
- {
- time0make(1,1);
- f0=f0+2*y0+1;
- y0=y0+1;
- sum=sum-1;
- }
- }
- sum=temp;
- f0=0;
- while(sum>0) //第二象限
- {
- if(f0>=0)
- {
- time0make(1,0);
- f0=f0-2*y0+1;
- y0=y0-1;
- sum=sum-1;
- }
- else
- {
- time0make(0,0);
- f0=f0-2*x0+1;
- x0=x0-1;
- sum=sum-1;
- }
- }
- sum=temp;
- f0=0;
- while(sum>0) //第三象限
- {
- if(f0>=0)
- {
- time0make(0,1);
- f0=f0+2*x0+1;
- x0=x0+1;
- sum=sum-1;
- }
- else
- {
- time0make(1,0);
- f0=f0-2*y0+1;
- y0=y0-1;
- sum=sum-1;
- }
- }
- sum=temp;
- f0=0;
- while(sum>0) //第四象限
- {
- if(f0>=0)
- {
- time0make(1,1);
- f0=f0+2*y0+1;
- y0=y0+1;
- sum=sum-1;
- }
- else
- {
- time0make(0,1);
- f0=f0+2*x0+1;
- x0=x0+1;
- sum=sum-1;
- }
- }
- }
- //回到初始点
- void init0()
- {
- delay();
- while(1)
- {
- time0make(0,0);
- if(x_stp==0)
- {
- if(x_stp==0)
- break;
- }
- }
- while(1)
- {
- time0make(1,0);
- if(y_stp==0)
- {
- if(y_stp==0)
- break;
- }
- }
- }
- void main() //主函数
- {
- char p,m,order;
- int k;
- float i,j,length;
- EA=1;
- IT0=1; //脉冲触发外部中断控制
- // start=1;
- // while(start)
- // {
- // delay();
- // if(start==0)
- // break;
- // }
- light=off;
- delayms();
- turnlight=off;
- init0(); //归零
- i=len+rad;
- math_zhixian(i,len+40); //预留打印数字和汉字的高度
- light=on; //打印圆
- delay();
- turnlight=on;
- math_yuanhu(rad,0,0,rad);
- light=off;
- delay();
- turnlight=off;
- length=rad*2*high;
- i=0-(rad+length/2);
- j=0-rad;
- math_zhixian(i,j);
- i=length/2; //打印等边三角形
- j=length*high/2;
- light=on;
- delay();
- turnlight=on;
- math_zhixian(i,j);
- i=length/2;
- j=0-length*high/2;
- math_zhixian(i,j);
- i=0-length;
- j=0;
- math_zhixian(i,j);
- light=off;
- delay();
- turnlight=off;
- i=0-20;
- j=0-10;
- math_zhixian(i,j);
- for(m=0;m<12;m++)
- {
- order=xuehao[m];
- if(order==6)
- {
- k=0;
- for(;;) //打印数字 6
- {
-
- p=num6[k];
- if(p==0)
- break;
- k++;
- i=num6[k];
- i=(2/wid)*i;
- k++;
- j=num6[k];
- j=0-(2/wid)*j; //Y取反
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- }
- else if(order==4)
- {
- k=0;
- for(;;) //打印数字 4
- {
-
- p=num4[k];
- if(p==0)
- break;
- k++;
- i=num4[k];
- i=(2/wid)*i;
- k++;
- j=num4[k];
- j=0-(2/wid)*j;
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- }
- else if(order==3)
- {
- k=0;
- for(;;) //打印数字 3
- {
-
- p=num3[k];
- if(p==0)
- break;
- k++;
- i=num3[k];
- i=(2/wid)*i;
- k++;
- j=num3[k];
- j=0-(2/wid)*j;
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- }
- else if(order==2)
- {
- k=0;
- for(;;) //打印数字2
- {
-
- p=num2[k];
- if(p==0)
- break;
- k++;
- i=num2[k];
- i=(2/wid)*i;
- k++;
- j=num2[k];
- j=0-(2/wid)*j;
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- }
- else if(order==1)
- {
- k=0;
- for(;;) //打印数字 1
- {
-
- p=num1[k];
- if(p==0)
- break;
- k++;
- i=num1[k];
- i=(2/wid)*i;
- k++;
- j=num1[k];
- j=0-(2/wid)*j;
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- }
- else if(order==0)
- {
- k=0;
- for(;;) //打印数字 0
- {
-
- p=num0[k];
- if(p==0)
- break;
- k++;
- i=num0[k];
- i=(2/wid)*i;
- k++;
- j=num0[k];
- j=0-(2/wid)*j;
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- }
- else
- continue;
- }
- light=off;
- delay();
- turnlight=off;
- i=0-5*12; //移动到打印字的起点且与数字同一个边开始
- j=0-20;
- math_zhixian(i,j);
- k=0;
- for(;;) //打印姓名首字
- {
-
- p=cai[k];
- if(p==0)
- break;
- k++;
- i=cai[k];
- i=(15/wid_1)*i;
- k++;
- j=cai[k];
- j=0-(15/wid_1)*j;
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- k=0;
- for(;;) //打印姓名第二字
- {
-
- p=ying[k];
- if(p==0)
- break;
- k++;
- i=ying[k];
- i=(15/wid_1)*i;
- k++;
- j=ying[k];
- j=0-(15/wid_1)*j;
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- k=0;
- for(;;) //打印姓名第三字
- {
-
- p=tao[k];
- if(p==0)
- break;
- k++;
- i=tao[k];
- i=(15/wid_1)*i;
- k++;
- j=tao[k];
- j=0-(15/wid_1)*j;
- k++;
- if(p==1)
- {light=on;
- delay();
- turnlight=on;}
- else if(p==2)
- {light=off;
- delay();
- turnlight=off;}
- math_zhixian(i,j);
- }
- while(1);
- }
- void stop() interrupt 0
- {
- char x;
- x=1;
- delay();
- while(x)
- {
- if(contin==0)
- {
- delay();
- if(contin==0)
- break;
- }
- }
- }
复制代码
所有资料51hei提供下载:
test.7z
(57.72 KB, 下载次数: 69)
|