找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3674|回复: 2
打印 上一主题 下一主题
收起左侧

基于51单片机的XY轴激光打印机电路原理图+程序

  [复制链接]
跳转到指定楼层
楼主
基于51单片机的XY轴激光打印机电路原理图,内含51单片机原理图相关的库文件,以及该打印机的测试代码。


单片机源程序如下:
  1. #include<reg52.h>
  2. #include<math.h>
  3. #include<data.h>
  4. #include<delay.h>
  5. #include<delayms.h>
  6. #define        work_pl 2000        //计时器初值
  7. #define dl 71.5   //脉冲当量
  8. #define wid 10.0   //数字字宽
  9. #define wid_1 130.0   //汉字字宽
  10. #define rad 15          //所打印圆半径
  11. #define len 33     //所打印圆的圆心基点
  12. #define high 1.73205  //根号3对应常数
  13. #define on   0                 //激光开关
  14. #define off  1
  15. sbit x_move=P2^4;
  16. sbit x_direct=P2^5;
  17. sbit y_move=P2^2;
  18. sbit y_direct=P2^3;
  19. sbit light=P2^0;    //激光开关
  20. sbit turnlight=P2^1;  //打印开始或结束
  21. sbit x_stp=P1^1;        //限位开关
  22. sbit y_stp=P1^0;
  23. sbit start=P1^3;        //启动开关
  24. sbit contin=P1^4;  //暂停
  25. // 脉冲发生器
  26. void time0make(unsigned char a,unsigned char b)
  27. {
  28. TMOD=0x01;
  29. TH0=(65536-work_pl)/256;
  30. TL0=(65536-work_pl)%256;
  31. EA=1;
  32. TR0=1;
  33. while(1)
  34. {
  35.    if(TF0)
  36.    {
  37.          TF0=0;
  38.          if(a==0)
  39.          {
  40.           x_direct=b;
  41.           x_move=!x_move;
  42.           TH0=(65536-work_pl)/256;
  43.       TL0=(65536-work_pl)%256;
  44.           while(!TF0);
  45.           x_move=!x_move;
  46.           TF0=0;
  47.           return;
  48.          }
  49.         else
  50.          {
  51.           y_direct=b;
  52.           y_move=!y_move;
  53.           TH0=(65536-work_pl)/256;
  54.       TL0=(65536-work_pl)%256;
  55.           while(!TF0);
  56.           y_move=!y_move;
  57.           TF0=0;
  58.           return;
  59.          }
  60.          TH0=(65536-work_pl)/256;
  61.      TL0=(65536-work_pl)%256;
  62.          while(!TF0)
  63.          TF0=0;
  64.          return;
  65.    }
  66. }
  67. }
  68. //直线插补
  69. void math_zhixian(float x_e,float y_e)
  70. {
  71.   float f0,x0,y0,sum;
  72.   unsigned char a,b;
  73.   if(x_e>=0&&y_e>=0)  //相对坐标象限判断
  74.          {a=1;b=1;}
  75.          else if(x_e<=0&&y_e>=0)
  76.          {a=0;b=1;}
  77.            else if(x_e<=0&&y_e<=0)
  78.             {a=0,b=0;}
  79.                   else
  80.                   {a=1,b=0;};
  81.   x0=fabs(dl*x_e);
  82.   y0=fabs(dl*y_e);
  83.   f0=0;
  84.   sum=x0+y0;
  85.   while(sum>=0)
  86.   {
  87.   if(f0>=0)
  88.    {
  89.    time0make(0,a);
  90.    f0=f0-y0;
  91.    sum=sum-1;
  92.    }
  93.   else
  94.    {
  95.    time0make(1,b);
  96.    f0=f0+x0;
  97.    sum=sum-1;
  98.    }
  99.   }
  100. }
  101. //第一象限逆圆弧插补
  102. void math_yuanhu(float x0,float y0,float x1,float y1)
  103. {
  104. float f0,sum,temp;
  105. f0=0;
  106. x0=dl*x0;
  107. y0=dl*y0;
  108. x1=dl*x1;
  109. y1=dl*y1;
  110. sum=x0+y1;
  111. temp=sum;
  112. while(sum>0)         //第一象限
  113. {
  114.    if(f0>=0)
  115.    {
  116.    time0make(0,0);
  117.    f0=f0-2*x0+1;
  118.    x0=x0-1;
  119.    sum=sum-1;
  120.    }
  121.    else
  122.    {
  123.         time0make(1,1);
  124.         f0=f0+2*y0+1;
  125.         y0=y0+1;
  126.         sum=sum-1;
  127.    }
  128. }
  129. sum=temp;
  130. f0=0;
  131. while(sum>0)          //第二象限
  132. {
  133.   if(f0>=0)
  134.    {
  135.     time0make(1,0);
  136.         f0=f0-2*y0+1;
  137.         y0=y0-1;
  138.         sum=sum-1;
  139.    }
  140.   else
  141.   {
  142.    time0make(0,0);
  143.    f0=f0-2*x0+1;
  144.    x0=x0-1;
  145.    sum=sum-1;
  146.   }
  147. }
  148. sum=temp;
  149. f0=0;
  150. while(sum>0)  //第三象限
  151. {
  152.   if(f0>=0)
  153.    {
  154.     time0make(0,1);
  155.         f0=f0+2*x0+1;
  156.         x0=x0+1;
  157.         sum=sum-1;
  158.    }
  159.   else
  160.   {
  161.            time0make(1,0);
  162.         f0=f0-2*y0+1;
  163.         y0=y0-1;
  164.         sum=sum-1;
  165.   }
  166. }
  167. sum=temp;
  168. f0=0;
  169. while(sum>0)  //第四象限
  170. {
  171.   if(f0>=0)
  172.    {
  173.     time0make(1,1);
  174.         f0=f0+2*y0+1;
  175.         y0=y0+1;
  176.         sum=sum-1;
  177.    }
  178.   else
  179.   {
  180.     time0make(0,1);
  181.         f0=f0+2*x0+1;
  182.         x0=x0+1;
  183.         sum=sum-1;
  184.   }
  185. }
  186. }
  187. //回到初始点
  188. void init0()
  189. {
  190.   delay();
  191.   while(1)
  192.   {
  193.     time0make(0,0);
  194.         if(x_stp==0)
  195.         {
  196.           if(x_stp==0)
  197.           break;
  198.         }
  199.   }
  200.   while(1)
  201.   {
  202.    time0make(1,0);
  203.    if(y_stp==0)
  204.         {
  205.           if(y_stp==0)
  206.           break;
  207.         }
  208.    }
  209. }
  210. void main()                  //主函数
  211. {
  212. char p,m,order;
  213. int k;
  214. float i,j,length;
  215. EA=1;
  216. IT0=1;     //脉冲触发外部中断控制
  217. // start=1;
  218. // while(start)
  219. // {
  220. //        delay();
  221. //        if(start==0)
  222. //        break;
  223. // }
  224. light=off;
  225. delayms();
  226. turnlight=off;
  227. init0();     //归零
  228. i=len+rad;
  229. math_zhixian(i,len+40);  //预留打印数字和汉字的高度
  230. light=on;              //打印圆
  231. delay();
  232. turnlight=on;
  233. math_yuanhu(rad,0,0,rad);
  234. light=off;
  235. delay();
  236. turnlight=off;
  237. length=rad*2*high;
  238. i=0-(rad+length/2);
  239. j=0-rad;       
  240. math_zhixian(i,j);
  241. i=length/2;           //打印等边三角形
  242. j=length*high/2;
  243. light=on;
  244. delay();
  245. turnlight=on;
  246. math_zhixian(i,j);
  247. i=length/2;
  248. j=0-length*high/2;
  249. math_zhixian(i,j);
  250. i=0-length;
  251. j=0;
  252. math_zhixian(i,j);
  253. light=off;
  254. delay();
  255. turnlight=off;
  256. i=0-20;
  257. j=0-10;
  258. math_zhixian(i,j);
  259. for(m=0;m<12;m++)
  260. {
  261.   order=xuehao[m];
  262.    if(order==6)
  263.    {  
  264.     k=0;  
  265.     for(;;)  //打印数字   6
  266.       {
  267.         
  268.            p=num6[k];
  269.            if(p==0)
  270.            break;
  271.            k++;
  272.            i=num6[k];
  273.            i=(2/wid)*i;
  274.            k++;
  275.            j=num6[k];
  276.            j=0-(2/wid)*j;          //Y取反
  277.            k++;
  278.            if(p==1)
  279.            {light=on;
  280.                    delay();
  281.            turnlight=on;}
  282.            else if(p==2)
  283.            {light=off;
  284.                    delay();
  285.            turnlight=off;}
  286.            math_zhixian(i,j);
  287.        }
  288.         }
  289.         else if(order==4)
  290.    {   
  291.     k=0;
  292.     for(;;)  //打印数字  4
  293.       {
  294.         
  295.            p=num4[k];
  296.            if(p==0)
  297.            break;
  298.            k++;
  299.            i=num4[k];
  300.            i=(2/wid)*i;
  301.            k++;
  302.            j=num4[k];
  303.            j=0-(2/wid)*j;
  304.            k++;
  305.            if(p==1)
  306.            {light=on;
  307.                    delay();
  308.            turnlight=on;}
  309.            else if(p==2)
  310.            {light=off;
  311.                    delay();
  312.            turnlight=off;}
  313.            math_zhixian(i,j);
  314.        }
  315.         }
  316.         else if(order==3)
  317.    {
  318.     k=0;   
  319.     for(;;)  //打印数字  3
  320.       {
  321.          
  322.            p=num3[k];
  323.            if(p==0)
  324.            break;
  325.            k++;
  326.            i=num3[k];
  327.            i=(2/wid)*i;
  328.            k++;
  329.            j=num3[k];
  330.            j=0-(2/wid)*j;
  331.            k++;
  332.            if(p==1)
  333.            {light=on;
  334.                    delay();
  335.            turnlight=on;}
  336.            else if(p==2)
  337.            {light=off;
  338.                    delay();
  339.            turnlight=off;}
  340.            math_zhixian(i,j);
  341.        }
  342.         }
  343.         else if(order==2)
  344.    {
  345.      k=0;  
  346.      for(;;)  //打印数字2
  347.       {
  348.          
  349.            p=num2[k];
  350.            if(p==0)
  351.            break;
  352.            k++;
  353.            i=num2[k];
  354.            i=(2/wid)*i;
  355.            k++;
  356.            j=num2[k];
  357.            j=0-(2/wid)*j;
  358.            k++;
  359.            if(p==1)
  360.            {light=on;
  361.                    delay();
  362.            turnlight=on;}
  363.            else if(p==2)
  364.            {light=off;
  365.                    delay();
  366.            turnlight=off;}
  367.            math_zhixian(i,j);
  368.        }
  369.         }
  370.         else if(order==1)
  371.    {  
  372.       k=0;
  373.       for(;;)  //打印数字 1
  374.       {
  375.          
  376.            p=num1[k];
  377.            if(p==0)
  378.            break;
  379.            k++;
  380.            i=num1[k];
  381.            i=(2/wid)*i;
  382.            k++;
  383.            j=num1[k];
  384.            j=0-(2/wid)*j;
  385.            k++;
  386.            if(p==1)
  387.            {light=on;
  388.                    delay();
  389.            turnlight=on;}
  390.            else if(p==2)
  391.            {light=off;
  392.                    delay();
  393.            turnlight=off;}
  394.            math_zhixian(i,j);
  395.        }
  396.         }
  397.         else if(order==0)
  398.    {
  399.     k=0;   
  400.     for(;;)  //打印数字 0
  401.       {
  402.          
  403.            p=num0[k];
  404.            if(p==0)
  405.            break;
  406.            k++;
  407.            i=num0[k];
  408.            i=(2/wid)*i;
  409.            k++;
  410.            j=num0[k];
  411.            j=0-(2/wid)*j;
  412.            k++;
  413.            if(p==1)
  414.            {light=on;
  415.                    delay();
  416.            turnlight=on;}
  417.            else if(p==2)
  418.            {light=off;
  419.                    delay();
  420.            turnlight=off;}
  421.            math_zhixian(i,j);
  422.        }
  423.         }
  424.         else
  425.         continue;
  426. }
  427.          light=off;
  428.          delay();
  429.      turnlight=off;
  430.          i=0-5*12;  //移动到打印字的起点且与数字同一个边开始
  431.          j=0-20;
  432.          math_zhixian(i,j);
  433.          k=0;
  434.      for(;;)                //打印姓名首字
  435.       {
  436.          
  437.            p=cai[k];
  438.            if(p==0)
  439.            break;
  440.            k++;
  441.            i=cai[k];
  442.            i=(15/wid_1)*i;
  443.            k++;
  444.            j=cai[k];
  445.            j=0-(15/wid_1)*j;
  446.            k++;
  447.            if(p==1)
  448.            {light=on;
  449.                    delay();
  450.            turnlight=on;}   
  451.            else if(p==2)
  452.            {light=off;
  453.                    delay();
  454.            turnlight=off;}
  455.            math_zhixian(i,j);
  456.        }
  457.          k=0;
  458.          for(;;)                //打印姓名第二字
  459.       {
  460.          
  461.            p=ying[k];
  462.            if(p==0)
  463.            break;
  464.            k++;
  465.            i=ying[k];
  466.            i=(15/wid_1)*i;
  467.            k++;
  468.            j=ying[k];
  469.            j=0-(15/wid_1)*j;
  470.            k++;
  471.            if(p==1)
  472.            {light=on;
  473.                    delay();
  474.            turnlight=on;}   
  475.            else if(p==2)
  476.            {light=off;
  477.                    delay();
  478.            turnlight=off;}
  479.            math_zhixian(i,j);
  480.        }
  481.          k=0;
  482.      for(;;)                //打印姓名第三字
  483.       {
  484.          
  485.            p=tao[k];
  486.            if(p==0)
  487.            break;
  488.            k++;
  489.            i=tao[k];
  490.            i=(15/wid_1)*i;
  491.            k++;
  492.            j=tao[k];
  493.            j=0-(15/wid_1)*j;
  494.            k++;
  495.            if(p==1)
  496.            {light=on;
  497.                    delay();
  498.            turnlight=on;}   
  499.            else if(p==2)
  500.            {light=off;
  501.                    delay();
  502.            turnlight=off;}
  503.            math_zhixian(i,j);
  504.        }
  505. while(1);
  506. }
  507. void stop() interrupt 0
  508. {
  509.   char x;
  510.   x=1;
  511.   delay();
  512.   while(x)
  513.   {
  514.   if(contin==0)
  515.   {
  516.    delay();
  517.    if(contin==0)
  518.    break;
  519.   }
  520.   }
  521. }
复制代码

所有资料51hei提供下载:
test.7z (57.72 KB, 下载次数: 65)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏6 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:685250 发表于 2023-7-11 15:16 | 只看该作者
没币没币没币没币没币没币没币没币没币没币没币没币没币没币没币没币没币
回复

使用道具 举报

板凳
ID:72224 发表于 2023-11-14 11:49 | 只看该作者
有上位机吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表