找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5345|回复: 7
收起左侧

51单片机控制步进电机的仿真LCD12864显示 带源程序

  [复制链接]
ID:202690 发表于 2017-5-20 08:35 | 显示全部楼层 |阅读模式
里面含有单片机控制步进电机的C语言程序及proteus仿真图。

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
0.png 0.png 0.png

单片机源程序如下:
  1. #include<reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. sbit CS1=P1^6;    /*片选1*/
  5. sbit CS2=P1^7;    /*片选2*/
  6. sbit SEG_LE=P1^0;
  7. sbit LCD_LE=P1^1;
  8. sbit SEG_OE=P1^2;
  9. sbit LCD_OE=P1^3;

  10. uchar code seg[]=//数码管:正向位置;共阳极;正向连接
  11. {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//0-9

  12. uchar code table_speed_num1[]=
  13. {
  14. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,/*1*/
  15. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,/*5*/
  16. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*0*/
  17. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
  18. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00/*0*/
  19. };                                                        //150.0
  20. uchar code table_speed_num2[]={
  21. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*7*/
  22. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,/*5*/
  23. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
  24. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*0*/
  25. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00/*0*/
  26. };                                                  //75.00
  27. uchar code table_speed_num3[]={
  28. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,/*3*/
  29. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*7*/
  30. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
  31. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,/*5*/
  32. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00/*0*/
  33. };                                                 //37.50
  34. uchar code table_speed_num4[]={
  35. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,/*1*/
  36. 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,/*8*/
  37. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
  38. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*7*/
  39. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00/*5*/
  40. };                                                //18.75
  41. uchar code table_speed_num5[]={
  42. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,/*9*/
  43. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*.*/
  44. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,/*3*/
  45. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*7*/
  46. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00/*5*/
  47. };                                           //9.375

  48. uchar code table_direct_state_Y[]=
  49. {
  50. 0x00,0xFE,0x00,0xFC,0x00,0xFE,0x00,0xF2,0x12,0x1A,0x96,0x12,0x12,0xF2,0x02,0x00,0x20,0x1F,0x00,0x0F,0x00,0x7F,0x00,0x47,
  51. 0x20,0x10,0x0F,0x10,0x20,0x67,0x00,0x00,

  52. 0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,
  53. 0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,

  54. 0x40,0x30,0x2C,0xEB,0x28,0x28,0x08,0x40,0x40,0x40,0xFF,0x40,0x40,0x40,0x40,0x00,0x01,0x01,0x01,0x7F,0x21,0x11,0x09,0x00,
  55. 0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00
  56. };                                          //顺时针

  57. uchar code table_direct_state_N[]=
  58. {
  59. 0x40,0x42,0xCC,0x00,0x00,0x08,0xC9,0x0E,0x08,0xF8,0x0C,0x0B,0xC8,0x08,0x00,0x00,0x40,0x20,0x1F,0x20,0x40,0x40,0x51,0x49,
  60. 0x47,0x41,0x41,0x41,0x41,0x40,0x40,0x00,

  61. 0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,
  62. 0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,

  63. 0x40,0x30,0x2C,0xEB,0x28,0x28,0x08,0x40,0x40,0x40,0xFF,0x40,0x40,0x40,0x40,0x00,0x01,0x01,0x01,0x7F,0x21,0x11,0x09,0x00,
  64. 0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00
  65. };                                          //逆时针



  66. uchar code table_state_roll[]=
  67. {
  68. 0x40,0x41,0xCE,0x04,0x00,0x20,0x22,0xA2,0x62,0x22,0xA2,0x22,0x22,0x22,0x20,0x00,0x40,0x20,0x1F,0x20,0x28,0x4C,0x4A,0x49,
  69. 0x48,0x4C,0x44,0x45,0x5E,0x4C,0x40,0x00,

  70. 0x10,0x08,0x84,0xC6,0x73,0x22,0x40,0x44,0x44,0x44,0xC4,0x44,0x44,0x44,0x40,0x00,0x02,0x01,0x00,0xFF,0x00,0x00,0x00,0x00,
  71. 0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,

  72. 0x00,0x00,0xFC,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x08,0xFC,0x08,0x00,0x00,0x00,0x00,0x07,0x02,0x02,0x02,0x02,0xFF,
  73. 0x02,0x02,0x02,0x02,0x07,0x00,0x00,0x00,

  74. 0x00,0xC0,0xC0,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  75. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  76. };                                         //运行中...

  77. uchar code table_state_pause[]=
  78. {
  79. 0x80,0x92,0x9A,0x96,0xFB,0x52,0x52,0x80,0x7E,0x12,0x12,0x12,0xF2,0x12,0x10,0x00,0x00,0x00,0x00,0x7E,0x4B,0x4A,0x4B,0x4A,
  80. 0x4A,0x4A,0x4A,0x7E,0x01,0x00,0x00,0x00,

  81. 0x80,0x40,0x20,0xF8,0x07,0x02,0x04,0x74,0x54,0x55,0x56,0x54,0x74,0x04,0x04,0x00,0x00,0x00,0x00,0xFF,0x00,0x03,0x01,0x05,
  82. 0x45,0x85,0x7D,0x05,0x05,0x05,0x03,0x00,

  83. 0x00,0x00,0xFC,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x08,0xFC,0x08,0x00,0x00,0x00,0x00,0x07,0x02,0x02,0x02,0x02,0xFF,
  84. 0x02,0x02,0x02,0x02,0x07,0x00,0x00,0x00,

  85. 0x00,0xC0,0xC0,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  86. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  87. };                                        //暂停中...

  88. uchar code stepmotor_Y_table[]={0x02,0x06,0x04,0x0C,0x08,0x09,0x01,0x03};//B-BC-C-CD-D-DA-A-AB-B 顺时针 八拍
  89. uchar code stepmotor_N_table[]={0x01,0x09,0x08,0x0C,0x04,0x06,0x02,0x03};//A-AD-D-DC-C-CB-B-BA-A 逆时针 八拍
  90. uchar p; //控制转一圈的八次转动
  91. uchar direct;        //转动方向标志
  92. uchar speed=3,delay=0;        //转动速度标志

  93. extern display(uchar page,uchar column,uchar code *hzk,uchar num);        //显示汉字函数
  94. extern display_char(uchar page,uchar column,uchar code *hzk,uchar num);         //显示字符函数
  95. extern Keys_Scan();                 //按键检测函数
  96. extern LCD_DIS();                 //LCD初始化函数
  97. //x=1时延时2ms
  98. void DelayMS(unsigned int x)
  99. {
  100.         unsigned int i,j;
  101.          for(i=x;i>0;i--)
  102.          {
  103.                  for(j=0;j<247;j++)
  104.                 ;
  105.         }       
  106. }
  107. /*********显示速度数值函数****************/
  108. void dis_speed_num(uchar *v)       
  109. {
  110.         CS1=0;
  111.         CS2=1;
  112.         display_char(2,48,v,2);       

  113.         CS1=1;
  114.         CS2=0;
  115.         display_char(2,0,&v[32],3);
  116. }
  117. /*********显示方向函数****************/
  118. void dis_direct_Y_N(uchar dir)
  119. {
  120.         CS1=1;
  121.         CS2=0;
  122.         if(dir==1)
  123.                 display(4,0,table_direct_state_Y,3);
  124.         if(dir==2)
  125.                 display(4,0,table_direct_state_N,3);
  126. }
  127. /*********显示电机状态函数****************/
  128. void dis_state_P_R(uchar *buffer)
  129. {
  130.         CS1=1;
  131.         CS2=0;
  132.         display(6,0,buffer,4);
  133. }

  134. void main()
  135. {
  136.         uchar k=0,i=0,j=0,key=0;
  137.         TMOD=0x11;
  138.         TH0=(65536-50000)/256;
  139.         TL0=(65536-50000)%256;
  140.         EA=1;
  141.         ET0=1;
  142.         SEG_OE=1;          //关数码管输出使能
  143.         LCD_OE=0;          //开LCD输出使能
  144.         LCD_LE=1;          //关LCD锁存输出
  145.         LCD_DIS();
  146. //        TR0=1;       
  147.         while(1)
  148.         {
  149.                 while(key==0)
  150.                         key=Keys_Scan();   //检测按键键值
  151.                 if(key==3)
  152.                 {
  153.                         direct=1;         //转动方向标志为1,顺时针转动
  154.                         dis_direct_Y_N(direct);          
  155.                         key=0;
  156.                 }
  157.                 if(key==4)
  158.                 {
  159.                         direct=2;           //转动方向标志为2,逆时针转动
  160.                         dis_direct_Y_N(direct);
  161.                         key=0;
  162.                 }
  163.                 if(key==5)
  164.                 {
  165.                         TR0=0;                //关T0中断,电机暂停转动
  166.                         dis_state_P_R(table_state_pause);
  167.                         key=0;
  168.                 }
  169.                 if(key==6)
  170.                 {
  171.                         TR0=1;           //开T0中断,电机开始转动
  172.                         dis_state_P_R(table_state_roll);
  173.                         key=0;
  174.                 }
  175.                 if(key==1)
  176.                 {                       
  177.                         speed=speed-1;;        //转动一次所需延时时间减少,转动加速
  178.                         if(speed<=1)
  179.                                 speed=1;        //最少延时一次
  180.                         key=0;
  181.                 }
  182.                 if(key==2)
  183.                 {         
  184.                         speed=speed+1; //转动一次所需延时时间增加,转动减速
  185.                         if(speed>=5)
  186.                                 speed=5;   //最多延时五次
  187.                         key=0;
  188.                 }
  189.                 if(speed==1)          //显示相应延时时间的速度数值
  190.                         dis_speed_num(table_speed_num1);
  191.                 if(speed==2)
  192.                         dis_speed_num(table_speed_num2);
  193.                 if(speed==3)
  194.                         dis_speed_num(table_speed_num3);
  195.                 if(speed==4)
  196.                         dis_speed_num(table_speed_num4);
  197.                 if(speed==5)
  198.                         dis_speed_num(table_speed_num5);
  199.                 LCD_LE=0;           //关LCD输出使能
  200.                 SEG_OE=0;           //开数码管输出使能
  201.                 SEG_LE=1;           //关数码管锁存输出
  202.                 P0=seg[speed]; //输出当前速度档位1-5,也就是延时时间
  203.                 SEG_LE=0;           //开数码管锁存输出,锁存输出值
  204.                 LCD_LE=1;           //关LCD锁存输出
  205.                 DelayMS(100);  //延时一会,等待外围器件工作,
  206.                 //没有延时,在加速加速时可能会产生多加速或多减速
  207.         }       
  208. }
  209. /**********电机转动函数************/
  210. ……………………

  211. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

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

使用道具 举报

ID:310802 发表于 2018-4-18 14:18 | 显示全部楼层
你的protues呢
回复

使用道具 举报

ID:202803 发表于 2018-4-18 15:47 | 显示全部楼层
6666666666666
回复

使用道具 举报

ID:308785 发表于 2018-5-11 10:44 | 显示全部楼层
只要是这个的 我都要学习
回复

使用道具 举报

ID:92810 发表于 2018-9-25 11:16 | 显示全部楼层
表示看不懂啊!!!总感觉有点难得
回复

使用道具 举报

ID:406128 发表于 2018-10-8 00:14 来自手机 | 显示全部楼层
感谢!待我学有所成定将回报社会。
回复

使用道具 举报

ID:301797 发表于 2018-10-20 17:12 | 显示全部楼层
谢谢 你的资料!
回复

使用道具 举报

ID:497052 发表于 2019-3-26 11:04 | 显示全部楼层
源程序,下载来看看
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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