找回密码
 立即注册

QQ登录

只需一步,快速开始

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

Verilog步进电机程序

[复制链接]
跳转到指定楼层
楼主
ID:821845 发表于 2021-3-24 10:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. module step1 (clk0,reset,out,der,x);//状态机module
  2. input clk0,reset,der,x;
  3. output[3:0] out;
  4. reg[3:0] out;
  5. reg[2:0] current;
  6. reg[2:0] current1;
  7. parameter s0=3'b000,s1=3'b001,s2=3'b010,
  8.     s3=3'b011,s4=3'b100,s5=3'b101,
  9.     s6=3'b110,s7=3'b111;
  10. always@(posedge clk0 or negedge reset )
  11. begin
  12.   if(!reset)
  13.    begin
  14.     current<=s0;
  15.    end
  16.   else   
  17.    case(current)
  18.     s0:
  19.         begin
  20.       if(!der)
  21.        begin
  22.         current<=s7;
  23.         current1<=s7;
  24.        end
  25.       else
  26.        begin
  27.         current<=s1;
  28.         current1<=s1;
  29.        end
  30.         end
  31.     s1:
  32.      begin
  33.       if(!der)
  34.        begin
  35.         current<=s0;
  36.         current1<=s0;
  37.        end
  38.       else
  39.        begin
  40.         current<=s2;
  41.         current1<=s2;
  42.        end
  43.      end
  44.     s2:
  45.      begin
  46.       if(!der)
  47.        begin
  48.         current<=s1;
  49.         current1<=s1;
  50.        end
  51.       else
  52.        begin
  53.         current<=s3;
  54.         current1<=s3;
  55.        end
  56.      end
  57.     s3:
  58.      begin
  59.       if(!der)
  60.        begin
  61.         current<=s2;
  62.         current1<=s2;
  63.        end
  64.       else
  65.        begin
  66.         current<=s4;
  67.         current1<=s4;
  68.        end      
  69.      end
  70.     s4:
  71.      begin
  72.       if(!der)
  73.        begin
  74.         current<=s3;
  75.         current1<=s3;
  76.        end
  77.       else
  78.        begin
  79.         current<=s5;
  80.         current1<=s5;
  81.        end
  82.      end
  83.     s5:
  84.      begin
  85.       if(!der)
  86.        begin
  87.         current<=s4;
  88.         current1<=s4;
  89.        end
  90.       else
  91.        begin
  92.         current<=s6;
  93.         current1<=s6;
  94.        end
  95.      end
  96.     s6:
  97.      begin
  98.       if(!der)
  99.        begin
  100.         current<=s5;
  101.         current1<=s5;
  102.        end
  103.       else
  104.        begin
  105.         current<=s7;
  106.         current1<=s7;
  107.        end
  108.      end
  109.     s7:
  110.      begin
  111.       if(!der)
  112.        begin
  113.         current<=s6;
  114.         current1<=s6;
  115.        end
  116.       else
  117.        begin
  118.         current<=s0;
  119.         current1<=s0;
  120.        end
  121.      end
  122.    endcase     
  123. end
  124. always@(current1 )//or clk0
  125. begin
  126.   case(current1)
  127.    s0:
  128.     begin
  129.      out<=4'b1001;     
  130.     end
  131.    s1:
  132.     begin
  133.      out<=4'b0001;   
  134.     end
  135.    s2:
  136.     begin
  137.      out<=4'b0011;
  138.     end
  139.    s3:
  140.     begin
  141.      out<=4'b0010;
  142.     end
  143.    s4:
  144.     begin
  145.      out<=4'b0110;
  146.     end
  147.    s5:
  148.     begin
  149.      out<=4'b0100;
  150.     end
  151.    s6:
  152.     begin
  153.      out<=4'b1100;
  154.     end
  155.    s7:
  156.     begin
  157.      out<=4'b1000;
  158.     end
  159.   endcase  
  160. end
  161. endmodule
  162. module step2 (clk1,a,adj);//分频module
  163. input clk1;
  164. input[5:0] adj;
  165. output a;
  166. reg a;
  167. reg[25:0] cnt;
  168. reg[15:0] counter;
  169. wire[5:0] adj;
  170. reg[2:0] temp;
  171. always @(posedge clk1)
  172. begin
  173. temp=adj[5]+adj[4]+adj[3]+adj[2]+adj[1]+adj[0];
  174. if(temp==3'd6)
  175.   begin
  176.   if(cnt <= 32000)  cnt <= cnt+1'b1;//0.5MS 1MS 2MS 4MS  8MS   4000 8000 16000 32000 64000
  177.        else
  178.      begin
  179.     cnt <=26'b0;
  180.     a=~a;
  181.      end
  182.      end
  183. else if(temp==3'd5)
  184.    begin
  185.     if(cnt <= 16000)  cnt <= cnt+1'b1;//0.5MS 1MS 2MS 4MS  8MS   4000 8000 16000 32000 64000
  186.     else
  187.      begin
  188.       cnt <=26'b0;
  189.       a=~a;
  190.      end
  191.    end
  192.   else if(temp==3'd4)
  193.     begin
  194.      if(cnt <= 12000)  cnt <= cnt+1'b1;//0.5MS 1MS 2MS 4MS  8MS   4000 8000 16000 32000 64000
  195.      else
  196.       begin
  197.        cnt <=26'b0;
  198.        a=~a;
  199.       end
  200.     end
  201.    else if(temp==3'd3)
  202.      begin
  203.       if(cnt <= 10000)  cnt <= cnt+1'b1;//0.5MS 1MS 2MS 4MS  8MS   4000 8000 16000 32000 64000
  204.       else
  205.        begin
  206.         cnt <=26'b0;
  207.         a=~a;
  208.        end
  209.      end
  210.     else if(temp==3'd2)
  211.       begin
  212.        if(cnt <= 8000)  cnt <= cnt+1'b1;//0.5MS 1MS 2MS 4MS  8MS   4000 8000 16000 32000 64000
  213.        else
  214.         begin
  215.          cnt <=26'b0;
  216.          a=~a;
  217.         end
  218.       end
  219.      else if(temp==3'd1)
  220.      begin
  221.       if(cnt <= 6000)  cnt <= cnt+1'b1;//0.5MS 1MS 2MS 4MS  8MS   4000 8000 16000 32000 64000
  222.       else
  223.        begin
  224.         cnt <=26'b0;
  225.         a=~a;
  226.        end
  227.      end
  228.      else if(temp==3'd0)
  229.       begin
  230.        if(cnt <= 4000)  cnt <= cnt+1'b1;//0.5MS 1MS 2MS 4MS  8MS   4000 8000 16000 32000 64000
  231.        else
  232.         begin
  233.          cnt <=26'b0;
  234.          a=~a;
  235.         end
  236.       end

  237. end
  238. endmodule
  239. module step (clk,reset,out,der,d);//总体模块d[5:0]调速控制端口 用6个拨码开关表示 全1速度最慢 全0时速度最快 其中1(0)的个数能表示电机速度的快慢
  240. input clk ,reset,der,d;
  241. output [3:0]out;
  242. wire p;
  243. wire[5:0] d;
  244. step2 l1 (clk,p,d);//clk系统时钟
  245. step1 l2 (p,reset ,out,der);//reset停止转动  out[3:0]电机控制输出端口  der正反方向控制端
  246. endmodule
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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