找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4363|回复: 1
收起左侧

单片机步进电机控制实现直线插补的仿真与程序源码

[复制链接]
ID:367089 发表于 2018-7-10 14:36 | 显示全部楼层 |阅读模式
单片机控制步进电机实现直线插补仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
0.png

单片机源程序如下:
  1. #include<reg51.h>
  2. #define uint unsigned int
  3. #define uchar unsigned char

  4. uchar code bux[]={0xf0,0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
  5. uchar code buy[]={0x00,0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90};
  6. uchar code comx[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  7. uchar code comy[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

  8. sbit SX=P3^4;
  9. sbit SY=P3^5;
  10. sbit ST=P3^6;

  11. uint countx=0;
  12. uint county=0;
  13. uint count,flag=0;
  14. uint x=0;
  15. uint y=0;
  16. uint a=0,b=0,k=0,m=0;
  17. uint st=0;
  18. char Fm[20];

  19. void Key_Scan(void);
  20. void Key_Action(void);
  21. void motor();
  22. void stepx();
  23. void stepy();
  24. void star();
  25. void time0(void);

  26. void delayms(uint t)
  27. {
  28. uint i,j;
  29. for(i=0;i<t;i++)
  30.   for(j=0;j<112;j++);
  31. }



  32. void main()
  33. {
  34.         while(1)
  35.         {
  36.                 Key_Scan();
  37.                 time0();
  38.                 while(!flag);
  39.                 flag=0;
  40.                 motor();
  41.                 if(1==st)
  42.                 {       
  43.                         star();
  44.                         st=0;
  45.                 }
  46.        
  47.         }
  48. }


  49. void Key_Scan(void)
  50. {
  51.         if(0==SX)
  52.         {
  53.                 delayms(10);
  54.                 if(0==SX)
  55.                 {
  56.                         countx++;
  57.                         while(!SX);       
  58.                 }       
  59.         }
  60.         if(0==SY)
  61.         {
  62.                 delayms(10);
  63.                 if(0==SY)
  64.                 {
  65.                         county++;
  66.                         while(!SY);       
  67.                 }       
  68.         }
  69.        
  70. }
  71. void time0(void)
  72. {
  73.         TMOD=0x01;
  74.         TH0=(65536-9216)/256;
  75.         TL0=(65536-9216)%256;
  76.         EA=1;
  77.         ET0=1;
  78.         TR0=1;
  79. }
  80. void TimeRS(void) interrupt 1
  81. {
  82.         TH0=(65536-9216)/256;
  83.         TL0=(65536-9216)%256;
  84.         count++;
  85.         if(count>=10)
  86.         {
  87.                 Key_Action();
  88.                 flag=1;       
  89.                 count=0;       
  90.         }


  91.        
  92. }
  93. void Key_Action(void)
  94. {
  95.         uint i,j;
  96.         if(countx>9)
  97.         {
  98.                 countx=0;
  99.         }
  100.         if(county>9)
  101.         {
  102.                 county=0;
  103.         }
  104.         P0=comx[countx];
  105.         //delayms(100);
  106.         P2=comy[county];
  107.         //delayms(100);
  108.        
  109.        
  110. }
  111. void motor()
  112. {
  113.         uint i,j;
  114.         if(0==ST)
  115.         {
  116.                 delayms(10);
  117.                 if(0==ST)
  118.                 {       
  119.                         st=1;
  120.                         while(!ST);       
  121.                 }       
  122.         }       
  123. }
  124. void star()
  125. {
  126.         a=countx;
  127.         b=county;
  128.         m=a+b;
  129.         Fm[k]=0;
  130.         while(m>0)
  131.         {
  132.                 if(Fm[k]>=0)
  133.                 {
  134.                         x++;
  135.                         m--;
  136.                         stepx();
  137.                         k++;
  138.                         Fm[k]=Fm[k-1]-b;

  139.                 }
  140.                 if(Fm[k]<0)
  141.                 {
  142.                         y++;
  143.                         m--;
  144.                         stepy();
  145.                         k++;
  146.                         Fm[k]=Fm[k-1]+a;
  147.        
  148.                 }
  149.         }
  150. }
  151. void stepx()
  152. {
  153.         P3=bux[x];
  154.         delayms(1000);
  155. }
  156. void stepy()
  157. {       
  158.         P1=bux[y];
  159.         delayms(1000);
  160. }

  161. #include<reg51.h>
  162. #define uint unsigned int
  163. #define uchar unsigned char

  164. uchar code bux[]={0xf0,0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};
  165. uchar code buy[]={0x00,0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90};
  166. uchar code comx[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  167. uchar code comy[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

  168. sbit SX=P3^4;
  169. sbit SY=P3^5;
  170. sbit ST=P3^6;

  171. uint countx=0;
  172. uint county=0;
  173. uint count,flag=0;
  174. uint x=0;
  175. uint y=0;
  176. uint a=0,b=0,k=0,m=0;
  177. uint st=0;
  178. char Fm[20];

  179. void Key_Scan(void);
  180. void Key_Action(void);
  181. void motor();
  182. void stepx();
  183. void stepy();
  184. void star();
  185. void time0(void);

  186. void delayms(uint t)
  187. {
  188. uint i,j;
  189. for(i=0;i<t;i++)
  190.   for(j=0;j<112;j++);
  191. }



  192. void main()
  193. {
  194.         while(1)
  195.         {
  196.                 Key_Scan();
  197.                 time0();
  198.                 while(!flag);
  199.                 flag=0;
  200.                 motor();
  201.                 if(1==st)
  202.                 {       
  203.                         star();
  204.                         st=0;
  205.                 }
  206.        
  207.         }
  208. }


  209. void Key_Scan(void)
  210. {
  211.         if(0==SX)
  212.         {
  213.                 delayms(10);
  214.                 if(0==SX)
  215.                 {
  216.                         countx++;
  217.                         while(!SX);       
  218.                 }       
  219.         }
  220.         if(0==SY)
  221.         {
  222.                 delayms(10);
  223.                 if(0==SY)
  224.                 {
  225.                         county++;
  226.                         while(!SY);       
  227.                 }       
  228.         }
  229.        
  230. }
  231. void time0(void)
  232. {
  233.         TMOD=0x01;
  234.         TH0=(65536-9216)/256;
  235.         TL0=(65536-9216)%256;
  236.         EA=1;
  237.         ET0=1;
  238.         TR0=1;
  239. }
  240. void TimeRS(void) interrupt 1
  241. {
  242.         TH0=(65536-9216)/256;
  243.         TL0=(65536-9216)%256;
  244.         count++;
  245.         if(count>=10)
  246.         {
  247.                 Key_Action();
  248.                 flag=1;       
  249.                 count=0;       
  250.         }


  251.        
  252. }
  253. void Key_Action(void)
  254. {
  255.         uint i,j;
  256.         if(countx>9)
  257.         {
  258.                 countx=0;
  259.         }
  260.         if(county>9)
  261.         {
  262.                 county=0;
  263.         }
  264.         P0=comx[countx];
  265.         //delayms(100);
  266.         P2=comy[county];
  267.         //delayms(100);
  268.        
  269.        
  270. }
  271. void motor()
  272. {
  273.         uint i,j;
  274.         if(0==ST)
  275.         {
  276.                 delayms(10);
  277.                 if(0==ST)
  278.                 {       
  279.                         st=1;
  280.                         while(!ST);       
  281.                 }       
  282.         }       
  283. }
  284. void star()
  285. {
  286.         a=countx;
  287.         b=county;
  288.         m=a+b;
  289.         Fm[k]=0;
  290.         while(m>0)
  291.         {
  292.                 if(Fm[k]>=0)
  293.                 {
  294.                         x++;
  295.                         m--;
  296.                         stepx();
  297.                         k++;
  298.                         Fm[k]=Fm[k-1]-b;

  299.                 }
  300.                 if(Fm[k]<0)
  301.                 {
  302.                         y++;
  303.                         m--;
  304.                         stepy();
  305.                         k++;
  306.                         Fm[k]=Fm[k-1]+a;
  307.        
  308.                 }
  309.         }
  310. }
  311. void stepx()
  312. {
  313.         P3=bux[x];
  314.         delayms(1000);
  315. }
  316. void stepy()
  317. {       
  318.         P1=bux[y];
  319.         delayms(1000);
  320. }

复制代码
0.png

所有资料51hei提供下载:
步进电机.rar (32.22 KB, 下载次数: 81)
回复

使用道具 举报

ID:435943 发表于 2018-12-3 21:30 | 显示全部楼层
感谢分享 值得借鉴
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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