找回密码
 立即注册

QQ登录

只需一步,快速开始

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

FPGA步进电机控制程序

[复制链接]
跳转到指定楼层
楼主
ID:564791 发表于 2020-1-1 13:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. //******模块一:分频器*****************//
  2. /*en1为使能端,clk为输入频率,outdiv(0-9)为输出频率,
  3. outdiv(0)为不进行分频,outdiv(1)进行2分频,
  4. outdiv(2)进行3分频,outdiv(3)进行4分频,
  5. outdiv(4)进行5分频,outdiv(5)进行6分频,
  6. outdiv(6)进行7分频,outdiv(7)进行8分频,
  7. outdiv(8)进行9分频,outdiv(9)进行10分频。*/
  8. library ieee;
  9. use ieee.std_logic_1164.all;
  10. entity FPQ is
  11.               port(clk,en1: in std_logic;                           
  12.                               outdiv : out std_logic_vector(9 downto 0));
  13. end FPQ;
  14. architecture bh of FPQ is
  15. begin
  16.               process(en1,clk)
  17.               variable t1 : integer range 0 to 2;
  18.               variable t2 : integer range 0 to 3;
  19.               variable t3 : integer range 0 to 4;
  20.               variable t4 : integer range 0 to 5;
  21.               variable t5 : integer range 0 to 6;
  22.               variable t6 : integer range 0 to 7;
  23.               variable t7 : integer range 0 to 8;
  24.               variable t8 : integer range 0 to 9;
  25.               variable t9 : integer range 0 to 10;
  26.               begin
  27.               outdiv(0) <= clk;
  28.               if clk'event and clk = '1' then
  29.                             if en1 = '1' then
  30.                                           if (t1 >= 2) then t1:=0;outdiv(1)<='0';
  31.                                           elsif t1<1 then outdiv(1)<='0';
  32.                                           else outdiv(1) <= '1';
  33.                                           end if;
  34.                                           t1:=t1+1;
  35.                                           if (t2 >= 3) then t2:=0;outdiv(2)<='0';
  36.                                           elsif t2<2 then outdiv(2)<='0';
  37.                                           else outdiv(2) <= '1';
  38.                                           end if;
  39.                                           t2:=t2+1;                                         
  40.                                           if (t3 >= 4) then t3:=0;outdiv(3)<='0';
  41.                                           elsif t3<2 then outdiv(3)<='0';
  42.                                           else outdiv(3) <= '1';
  43.                                           end if;
  44.                                           t3:=t3+1;                                         
  45.                                           if (t4 >= 5) then t4:=0;outdiv(4)<='0';
  46.                                           elsif t4<3 then outdiv(4)<='0';
  47.                                           else outdiv(4) <= '1';
  48.                                           end if;
  49.                                           t4:=t4+1;                           
  50.                                           if (t5 >= 6) then t5:=0;outdiv(5)<='0';
  51.                                           elsif t5<3 then outdiv(5)<='0';
  52.                                           else outdiv(5) <= '1';
  53.                                           end if;
  54.                                           t5:=t5+1;
  55.                                           if (t6 >= 7) then t6:=0;outdiv(6)<='0';
  56.                                           elsif t6<4 then outdiv(6)<='0';
  57.                                           else outdiv(6) <= '1';
  58.                                           end if;
  59.                                           t6:=t6+1;
  60.                                           if (t7 >= 8) then t7:=0;outdiv(7)<='0';
  61.                                           elsif t7<4 then outdiv(7)<='0';
  62.                                           else outdiv(7) <= '1';
  63.                                           end if;
  64.                                           t7:=t7+1;
  65.                                           if (t8 >= 9) then t8:=0;outdiv(8)<='0';
  66.                                           elsif t8<5 then outdiv(8)<='0';
  67.                                           else outdiv(8) <= '1';
  68.                                           end if;
  69.                                           t8:=t8+1;
  70.                                           if (t9 >= 10) then t9:=0;outdiv(9)<='0';
  71.                                           elsif t9<5 then outdiv(9)<='0';
  72.                                           else outdiv(9) <= '1';
  73.                                           end if;
  74.                                           t9:=t9+1;
  75.                             end if;
  76.               end if;
  77.               end process;
  78. end bh;










  79. //***********模块二:频率选择器**************//
  80. /*P0、P1、P2、P3为频率选择信号,indiv(0-9)为分频器分频之后的频率信号,
  81. outclk为要选择的频率。
  82. 当p(3-0)为"0000"时,outclk为indiv(0);当p(3-0)为"0001"时,outclk为indiv(1);
  83. 当p(3-0)为"0010"时,outclk为indiv(2);当p(3-0)为"0011"时,outclk为indiv(3);
  84. 当p(3-0)为"0100"时,outclk为indiv(4);当p(3-0)为"0101"时,outclk为indiv(5);
  85. 当p(3-0)为"0110"时,outclk为indiv(6);当p(3-0)为"0111"时,outclk为indiv(7);
  86. 当p(3-0)为"1000"时,outclk为indiv(8);当p(3-0)为"1001"时,outclk为indiv(9);
  87. 当p(3-0)为其它时,outclk为indiv(0)。*/
  88. library ieee;
  89. use ieee.std_logic_1164.all;
  90. entity PLXZQ is
  91.               port(p: in std_logic_vector(3 downto 0);
  92.                             indiv : in std_logic_vector(9 downto 0);
  93.                             outclk : out std_logic);
  94. end PLXZQ;
  95. architecture bh1 of PLXZQ is
  96. begin
  97.               process(p,indiv)
  98.               begin
  99.                             case p is
  100.                                           when "0000" => outclk <= indiv(0);
  101.                                           when "0001" => outclk <= indiv(1);
  102.                                           when "0010" => outclk <= indiv(2);
  103.                                           when "0011" => outclk <= indiv(3);
  104.                                           when "0100" => outclk <= indiv(4);
  105.                                           when "0101" => outclk <= indiv(5);
  106.                                           when "0110" => outclk <= indiv(6);
  107.                                           when "0111" => outclk <= indiv(7);
  108.                                           when "1000" => outclk <= indiv(8);
  109.                                           when "1001" => outclk <= indiv(9);
  110.                                           when others => outclk <= indiv(0);
  111.                             end case;
  112.               end process;
  113. end bh1;











  114. //***********模块三:方向锁存器**************//
  115. /*Zz、Fz、Tz为方向和开关信号,Modxz为六拍和三拍选择切换信号,
  116. outctrl为输出的控制信号。
  117. 当Modxz为1时,为六拍;当Modxz为0时,为双三拍。
  118. 当Tz为1时,为停止电机,当Tz为0时,为启动电机。
  119. 当Zz为1、Fz为0时,为电机正转,
  120. 当Zz为0、Fz为1时,为电机反转,
  121. 当Zz为1、Fz为1时,为电机停止,
  122. 当Zz为0、Fz为0时,为电机停止。
  123. outctrl(0)对应Tz;outctrl(1)对应Fz;
  124. outctrl(2)对应Zz;outctrl(3)对应Modxz。*/
  125. library ieee;
  126. use ieee.std_logic_1164.all;
  127. entity FXSCQ is
  128.               port(modxz,zz,fz,tz: in std_logic;
  129.                             outctrl: out std_logic_vector(3 downto 0));
  130. end FXSCQ;
  131. architecture bh2 of FXSCQ is
  132. begin
  133.               process(modxz,zz,fz,tz)
  134.               begin
  135.                             outctrl(0) <= tz;
  136.                             outctrl(1) <= fz;
  137.                             outctrl(2) <= zz;
  138.                             outctrl(3) <= modxz;
  139.                             if zz = '1' and fz = '1' then
  140.                                           outctrl(0) <= '1';
  141.                             elsif zz = '0' and fz = '0' then
  142.                                           outctrl(0) <= '1';
  143.                             end if;
  144.               end process;
  145. end bh2;






  146. //***********模块四:中央控制器**************//
  147. /*inclk为经过频率选择器选择之后的输入频率,
  148. inctrl为经过方向锁存器之后的输入控制信号,
  149. outDj为三相电机的的三相电压,
  150. 当inctrl为“0100”时,电机为双三相正转;
  151. 当inctrl为“0010”时,电机为双三相反转;
  152. 当inctrl为“1100”时,电机为单六相正转;
  153. 当inctrl为“1010”时,电机为单六相正转;
  154. 当inctrl为“xxx1”时,电机停止。*/
  155. library ieee;
  156. use ieee.std_logic_1164.all;
  157. use ieee.std_logic_unsigned.all;
  158. entity CPU is
  159.               port(inclk: in std_logic;
  160.                             inctrl: in std_logic_vector(3 downto 0);
  161.                             outDJ: out std_logic_vector(2 downto 0));
  162. end CPU;
  163. architecture bh3 of CPU is
  164.               signal Tmp : std_logic_vector(2 downto 0);
  165. begin
  166.               process(inclk,inctrl)
  167.               begin
  168.                             if inclk'event and inclk = '1' then
  169.                             if inctrl = "1100" then
  170.                                           if Tmp = 6 then
  171.                                                         Tmp <= "001";
  172.                                           else
  173.                                           Tmp <= Tmp+1;
  174.                                           end if;
  175.                             elsif inctrl = "1010" then
  176.                                           if Tmp = 1 then
  177.                                                         Tmp <= "110";
  178.                                           else
  179.                                                         Tmp <= Tmp-1;
  180.                                           end if;
  181.                             elsif inctrl = "1000" then            
  182.                                           Tmp <= "000";
  183.                             elsif inctrl = "0100" then
  184.                                           if Tmp = 6 then
  185.                                                         Tmp <= "010";
  186.                                           else
  187.                                                         Tmp <= Tmp+2;
  188.                                           end if;
  189.                             elsif inctrl = "0010" then
  190.                                           if Tmp = 2 then
  191.                                                         Tmp <="110";
  192.                                           else
  193.                                                         Tmp <= Tmp-2;
  194.                                           end if;
  195.                             elsif inctrl = "0000" then
  196.                                           Tmp <= "000";
  197.                             end if;
  198.                             end if;
  199.               end process;
  200.               with Tmp select
  201.               outDJ <= "001" when "001",
  202.                                             "011" when "010",
  203.                                             "010" when "011",
  204.                                             "110" when "100",
  205.                                             "100" when "101",
  206.                                             "101" when "110",
  207.                                             "000" when others;
  208. end bh3;



  209. //***********模块五:译码器**************//
  210. /*xyk(3-0)为输入信号,输入数值,dnf(8-0)为输出控制数码管信号。
  211. //dnf(8)为数码管的位选信号。
  212. 当xyk为“0000”时,dnf输出为“100111111”, 数码管显示‘0’;
  213. 当xyk为“0001”时,dnf输出为“100000110”, 数码管显示‘1’;
  214. 当xyk为“0010”时,dnf输出为“101011011”, 数码管显示‘2’;
  215. 当xyk为“0011”时,dnf输出为“101001111”, 数码管显示‘3’;
  216. 当xyk为“0100”时,dnf输出为“101100110”, 数码管显示‘4’;
  217. 当xyk为“0101”时,dnf输出为“100101001”, 数码管显示‘5’;
  218. 当xyk为“0110”时,dnf输出为“101111101”, 数码管显示‘6’;
  219. 当xyk为“0111”时,dnf输出为“100000111”, 数码管显示‘7’;
  220. 当xyk为“1000”时,dnf输出为“101111111”, 数码管显示‘8’;
  221. 当xyk为“1001”时,dnf输出为“101100111”, 数码管显示‘9’;
  222. 当xyk为其它时,dnf输出为“100111111”, 数码管显示‘0’。*/
  223. library ieee;
  224. use ieee.std_logic_1164.all;
  225. entity YMQ is
  226.               port(xyk: in std_logic_vector(3 downto 0);
  227.                             dnf: out std_logic_vector(8 downto 0));
  228. end YMQ;
  229. architecture bh4 of YMQ is
  230. begin
  231.               process(xyk)
  232.               begin
  233.                             case xyk is
  234.                             when "0000" => dnf <= "100111111";
  235.                             when "0001" => dnf <= "100000110";
  236.                             when "0010" => dnf <= "101011011";
  237.                             when "0011" => dnf <= "101001111";
  238.                             when "0100" => dnf <= "101100110";
  239.                             when "0101" => dnf <= "100101001";
  240.                             when "0110" => dnf <= "101111101";
  241.                             when "0111" => dnf <= "100000111";
  242.                             when "1000" => dnf <= "101111111";
  243.                             when "1001" => dnf <= "101100111";
  244.                             when others => dnf <= "100111111";
  245.                             end case;
  246.               end process;
  247. end bh4;




  248. //********总体:模块的例化*************//
  249. library ieee;
  250. use ieee.std_logic_1164.all;
  251. entity zong is
  252.               port(clk0,en0,Modxz0,Zz0,Fz0,Tz0 : in std_logic;
  253.                             P0 : in std_logic_vector(3 downto 0);
  254.                             DNF0 : out std_logic_vector(8 downto 0);
  255.                             OutDJ0 : out std_logic_vector(2 downto 0));
  256. end zong;
  257. architecture bh0 of zong is
  258. component FPQ
  259. port(clk,en1: in std_logic;                           
  260.               outdiv : out std_logic_vector(9 downto 0));
  261. end component;
  262. component PLXZQ
  263. port(p: in std_logic_vector(3 downto 0);
  264.               indiv : in std_logic_vector(9 downto 0);
  265.               outclk : out std_logic);
  266. end component;
  267. component YMQ
  268. port(xyk: in std_logic_vector(3 downto 0);
  269.               dnf: out std_logic_vector(8 downto 0));
  270. end component;
  271. component CPU
  272. port(inclk: in std_logic;
  273.               inctrl: in std_logic_vector(3 downto 0);
  274.               outDJ: out std_logic_vector(2 downto 0));
  275. end component;
  276. component FXSCQ
  277. port(modxz,zz,fz,tz: in std_logic;
  278.               outctrl: out std_logic_vector(3 downto 0));
  279. end component;
  280. signal DX1 :std_logic;
  281. signal DX2 :std_logic_vector(9 downto 0);
  282. signal DX3 :std_logic_vector(3 downto 0);
  283. begin
  284. U0 : FPQ port map (clk0,en0,DX2);
  285. U1 : PLXZQ port map (p0,DX2,DX1);
  286. U2 : YMQ port map (p0,dnf0);
  287. U3 : CPU port map (DX1,DX3,OutDJ0);
  288. U4 : FXSCQ port map (Modxz0,Zz0,Fz0,Tz0,DX3);
  289. end bh0;
复制代码

文档.doc

684.99 KB, 下载次数: 26, 下载积分: 黑币 -5

评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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