找回密码
 立即注册

QQ登录

只需一步,快速开始

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

c8051f410单片机步进电机控制系统

[复制链接]
跳转到指定楼层
楼主
c8051f410单片机做的步进电机控制系统


源码下载:
c8051f410.zip (33.63 KB, 下载次数: 14)


部分源码预览:
  1. /***********************************************
  2. 功能:定时器控制步进电机延时,定时器0控速
  3. 芯片C8051F410
  4. 晶振24.5MHz
  5. P0推挽控制步进电机
  6. L298N驱动
  7. 作者:JZ
  8. ***********************************************/
  9. #include"C8051F410.h"
  10. #include"stepping_motor410.h"
  11. // Peripheral specific initialization functions,
  12. // Called from the Init_Device() function
  13. #define uint unsigned int
  14. #define uchar unsigned char
  15. uchar code single_pos[4]={0xee,0xdd,0xbb,0x77};//单四拍驱动方式正转表A-B-C-D
  16. uchar code single_rev[4]={0x77,0xbb,0xdd,0xee};//单四拍驱动方式反转表D-C-B-A  

  17. uchar code double_pos[4]={0x99,0x33,0x66,0xcc};//双四拍驱动方式正转表AB-BC-CD-DA
  18. uchar code double_rev[4]={0xcc,0x66,0x33,0x99};//双四拍驱动方式反转表AD-DC-CB-BA
  19. uchar code eight_pos[8]={0xee,0xcc,0xdd,0x99,0xbb,0x33,0x77,0x66}; //八拍驱动方式正转表A-AB-B-BC-C-CD-D-DA
  20. uchar code eight_rev[8]={0x66,0x77,0x33,0xbb,0x99,0xdd,0xcc,0xee}; //八拍驱动方式反转表AD-D-DC-C-CB-B-BA-A
  21. uchar code eight_right_pos[8]={0x0e,0x0c,0x0d,0x09,0x0b,0x03,0x07,0x06}; //八拍驱动方式右轮正转表A-AB-B-BC-C-CD-D-DA
  22. uchar code eight_right_rev[8]={0x06,0x07,0x03,0x0b,0x09,0x0d,0x0c,0x0e}; //八拍驱动方式右反轮转表AD-D-DC-C-CB-B-BA-A  
  23. uchar code eight_left_pos[8]={0xe0,0xc0,0xd0,0x90,0xb0,0x30,0x70,0x60}; //八拍驱动方式左轮正转表A-AB-B-BC-C-CD-D-DA
  24. uchar code eight_left_rev[8]={0x60,0x70,0x30,0xb0,0x90,0xd0,0xc0,0xe0}; //八拍驱动方式右轮反转表AD-D-DC-C-CB-B-BA-A
  25. uint times;//时间次数记录
  26. unsigned int step; //记录脉冲数,即要走的步数
  27. uint beat; //步进电机每种驱动方式下的拍数
  28. char *p1,*p2;//存储运行方式表
  29. void PCA_Init()
  30. {
  31.     PCA0MD    &= ~0x40;
  32.     PCA0MD    = 0x00;
  33. }

  34. void Timer_Init()
  35. {
  36.     TCON      = 0x10;
  37.     TMOD      = 0x02;
  38.     TL0       = 0x38;
  39.     TH0       = 0x38;
  40. }

  41. void Port_IO_Init()
  42. {


  43.     P0MDOUT   = 0xFF;
  44.     XBR1      = 0x40;
  45. }

  46. void Oscillator_Init()
  47. {
  48.     OSCICN    = 0x87;
  49. }

  50. void Interrupts_Init()
  51. {
  52.     IE        = 0x82;
  53. }

  54. // Initialization function for device,
  55. // Call Init_Device() from your main program
  56. void Init_Device(void)
  57. {
  58.     PCA_Init();
  59.     Timer_Init();
  60.     Port_IO_Init();
  61.     Oscillator_Init();
  62.     Interrupts_Init();
  63. }
  64. //单四拍驱动正转(N*360/200)度
  65. void m_single_pos(unsigned int N)
  66. {
  67.         beat=4;        //拍数
  68.         p1=single_pos;
  69.         p2=single_pos+beat;
  70.         step=N;
  71.         TR0=1;//开定时器0,电机运行
  72.         while(1)
  73.         {
  74.                 P0=*p1;       
  75.                 if(step==0)
  76.                 {
  77.                 P0=0x00;
  78.                 TR0=0;//关定时器0
  79.                 break;
  80.                 }
  81.         }
  82. }
  83. //单四拍驱动反转(N*360/200)度
  84. void m_single_rev(unsigned int N)
  85. {
  86.         beat=4;
  87.         p1=single_rev;
  88.         p2=single_rev+beat;
  89.         step=N;
  90.         TR0=1;//        开定时器0,电机运行
  91.         while(1)
  92.         {
  93.                 P0=*p1;       
  94.                
  95.                 if(step==0)
  96.                 {
  97.                 P0=0x00;
  98.                 TR0=0;//关定时器0
  99.                 break;
  100.                 }
  101.         }
  102. }
  103. //双四拍驱动正转(N*360/200)度
  104. void m_double_pos(unsigned int N)
  105. {
  106.         beat=4;
  107.         p1=double_pos;
  108.         p2=double_pos+beat;
  109.         step=N;
  110.         TR0=1;//        开定时器0,电机运行
  111.         while(1)
  112.         {
  113.                 P0=*p1;       
  114.                
  115.                 if(step==0)
  116.                 {
  117.                 P0=0x00;
  118.                 TR0=0;//关定时器0
  119.                 break;
  120.                 }
  121.         }
  122. }
  123. //双四拍驱动反转(N*360/200)度
  124. void m_double_rev(unsigned int N)
  125. {
  126.         beat=4;
  127.         p1=double_rev;
  128.         p2=double_rev+beat;
  129.         step=N;
  130.         TR0=1;//        开定时器0,电机运行
  131.         while(1)
  132.         {
  133.                 P0=*p1;       
  134.                
  135.                 if(step==0)
  136.                 {
  137.                 P0=0x00;
  138.                 TR0=0;//关定时器0
  139.                 break;
  140.                 }
  141.         }
  142. }
  143. //八拍驱动正转(N*360/400)度
  144. void m_eight_pos(unsigned int N)
  145. {
  146.         beat=8;
  147.         p1=eight_pos;
  148.         p2=eight_pos+beat;
  149.         step=N;
  150.         TR0=1;//        开定时器0,电机运行
  151.         while(1)
  152.         {
  153.                 P0=*p1;       
  154.                
  155.                 if(step==0)
  156.                 {
  157.                 P0=0x00;
  158.                 TR0=0;//关定时器0
  159.                 break;
  160.                 }
  161.         }
  162. }
  163. //八拍驱动反转(N*360/400)度
  164. void m_eight_rev(unsigned int N)
  165. {
  166.         beat=8;
  167.         p1=eight_rev;
  168.         p2=eight_rev+beat;
  169.         step=N;
  170.         TR0=1;//        开定时器0,电机运行
  171.         while(1)
  172.         {
  173.                 P0=*p1;       
  174.                
  175.                 if(step==0)
  176.                 {
  177.                 P0=0x00;
  178.                 TR0=0;//关定时器0
  179.                 break;
  180.                 }
  181.         }
  182. }
  183. //八拍驱动右轮正转(N*360/400)度
  184. void m_eight_right_pos(unsigned int N)
  185. {
  186.         beat=8;
  187.         p1=eight_right_pos;
  188.         p2=eight_right_pos+beat;
  189.         step=N;
  190.         TR0=1;//        开定时器0,电机运行
  191.         while(1)
  192.         {
  193.                 P0=*p1;       
  194.                
  195.                 if(step==0)
  196.                 {
  197.                 P0=0x00;
  198.                 TR0=0;//关定时器0
  199.                 break;
  200.                 }
  201.         }
  202. }
  203. //八拍驱动右轮反转(N*360/400)度
  204. void m_eight_right_rev(unsigned int N)
  205. {
  206.         beat=8;
  207.         p1=eight_right_rev;
  208.         p2=eight_right_rev+beat;
  209.         step=N;
  210.         TR0=1;//        开定时器0,电机运行
  211.         while(1)
  212.         {
  213.                 P0=*p1;       
  214.                
  215.                 if(step==0)
  216.                 {
  217.                 P0=0x00;
  218.                 TR0=0;//关定时器0
  219.                 break;
  220.                 }
  221.         }
  222. }
  223. //八拍驱动左轮正转(N*360/400)度
  224. void m_eight_left_pos(unsigned int N)
  225. {
  226.         beat=8;
  227.         p1=eight_left_pos;
  228.         p2=eight_left_pos+beat;
  229.         step=N;
  230.         TR0=1;//        开定时器0,电机运行
  231.         while(1)
  232.         {
  233.                 P0=*p1;       
  234.                
  235.                 if(step==0)
  236.                 {
  237.                 P0=0x00;
  238.                 TR0=0;//关定时器0
  239.                 break;
  240.                 }
  241.         }
  242. }
  243. //八拍驱动左轮反转(N*360/400)度
  244. void m_eight_left_rev(unsigned int N)
  245. {
  246.         beat=8;
  247.         p1=eight_left_rev;
  248.         p2=eight_left_rev+beat;
  249.         step=N;
  250.         TR0=1;//        开定时器0,电机运行
  251.         while(1)
  252.         {
  253.                 P0=*p1;       
  254.                
  255.                 if(step==0)
  256.                 {
  257.                 P0=0x00;
  258.                 TR0=0;//关定时器0
  259.                 break;
  260.                 }
  261.         }
  262. }
  263. void motor_timer0() interrupt 1  //定时器0中断,产生电机驱动脉冲的延时
  264. {                                                                                                  
  265.         times++;
  266.         if(times==102)   //TH0=0x38时,times最小为5           tinmes=102延时10ms
  267.         {
  268.                 times=0;
  269.                 p1++;
  270.                 if(p1==p2)
  271.                 p1=p1-beat;
  272.                 step--;

  273.         }

  274. }
复制代码


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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