《電子技術(shù)應(yīng)用》
您所在的位置:首頁(yè) > 嵌入式技術(shù) > 解決方案 > 步進(jìn)電機(jī)定位控制系統(tǒng)VHDL程序與仿真

步進(jìn)電機(jī)定位控制系統(tǒng)VHDL程序與仿真

2008-06-27

步進(jìn)電機(jī)定位控制系統(tǒng)VHDL程序與仿真
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;

entity step_motor is
port (reset:in STD_LOGIC; --系統(tǒng)復(fù)位信號(hào)
      dir: in STD_LOGIC; --方向控制信號(hào)
      clk: in STD_LOGIC; --系統(tǒng)時(shí)鐘信號(hào)
      ini: in STD_LOGIC; --初始化使能信號(hào)
      manner: in STD_LOGIC_VECTOR (1 downto 0); --激磁方式的選擇開關(guān)
      angle: in INTEGER range 255 downto 0; --步進(jìn)角的倍數(shù)設(shè)定輸入
      baBA: out STD_LOGIC_VECTOR (3 downto 0)); --步進(jìn)電機(jī)狀態(tài)輸出
end step_motor;

architecture stepmotor_arch of step_motor is
signal count: INTEGER range 0 to 7; --計(jì)數(shù)器
signal cntInc: INTEGER range -2 to 2; --設(shè)定累加器所需的累(加/減)計(jì)數(shù)值
signal cc : integer range 0 to 3;
signal cntIni: INTEGER range -1 to 0; --設(shè)定累加器所需的計(jì)數(shù)初值
signal angleDnCount: INTEGER range 255 downto 0;  --計(jì)算已經(jīng)轉(zhuǎn)過(guò)的步進(jìn)角
signal angleDnCntDec: INTEGER range 2 downto 1;
begin

  process(dir, manner, angle)--, ini)
  begin
      --if ini='1' then
      cc<=conv_integer(manner);
      if dir='0' then 
          case cc is
              when 1 => -- 1-?相激勵(lì)
                  --count<=0;
                  cntIni<=0;
                  cntInc<=2;
                  angleDnCntDec<=2;--"10";
              when 2 => -- 2-?相激勵(lì)
                  --count<=7;
                  cntIni<=-1;
                  cntInc<=2;
                  angleDnCntDec<=2;--"10";  
              when 3 => -- 1-2?相激勵(lì)
                  --count<=0;
                  cntIni<=0;
                  cntInc<=1;
                  angleDnCntDec<=1;--"01";   
              when 0 => --manner="00" autodetect            
                  if (angle rem 2) =1 then -- 2-?相激勵(lì)
                  --count<=7;
                  cntIni<=-1;
                      cntInc<=2;
                      angleDnCntDec<=2;--"10"; 
                  else -- 1-?相激勵(lì)                  
                  --count<=0;
                  cntIni<=0;
                      cntInc<=2;
                      angleDnCntDec<=2;--"10";
                  end if; --angle
          end case; --manner
      else -- if dir='1'
          case cc is
               when 1 => -- 1-?相激勵(lì)
                  --count<=0;
                  cntIni<=0;
                  cntInc<=-2;
                  angleDnCntDec<=2;--"10";
              when 2 => -- 2-?相激勵(lì)
                  --count<=7;
                  cntIni<=-1;
                  cntInc<=-2;
                  angleDnCntDec<=2;--"10";  
              when 3 => -- 1-2?相激勵(lì)
                  --count<=0;
                  cntIni<=0;
                  cntInc<=-1;
                  angleDnCntDec<=1;--"01";   
              when 0 => --manner="00" autodetect            
                  if (angle rem 2) = 1 then -- 2-?相激勵(lì)
                      cntIni<=-1;
                      cntInc<=-2;
                      angleDnCntDec<=2;--"10"; 
                  else -- 1-?相激勵(lì)                  
                      cntIni<=0;
                      cntInc<=-2;
                      angleDnCntDec<=2;--"10";
                  end if; --angle
          end case; --manner
      end if; -- else dir=0
      --end if; -- ini
  end process;

  counting_reset: process(reset,ini, angle, clk)
  begin
      if reset='1' then
         count<=0;
         angleDnCount<=0;
      elsif clk'event and clk='1' then
          if ini='0' then
              count<=0+cntIni;
              angleDnCount<=angle;
          else
              count <= count+cntInc;
              if angleDnCount > angleDnCntDec then
                  angleDnCount <= angleDnCount-angleDnCntDec;
              else
                  angleDnCount <= 0;
              end if;
          end if;
      end if;
  end process;


  baBA <="0000" when angleDnCount=0 else
         "0001" when count=0 else
         "0011" when count=1 else
         "0010" when count=2 else
         "0110" when count=3 else
         "0100" when count=4 else
         "1100" when count=5 else
         "1000" when count=6 else
         "1001";-- when count>=7;

end stepmotor_arch;

本站內(nèi)容除特別聲明的原創(chuàng)文章之外,轉(zhuǎn)載內(nèi)容只為傳遞更多信息,并不代表本網(wǎng)站贊同其觀點(diǎn)。轉(zhuǎn)載的所有的文章、圖片、音/視頻文件等資料的版權(quán)歸版權(quán)所有權(quán)人所有。本站采用的非本站原創(chuàng)文章及圖片等內(nèi)容無(wú)法一一聯(lián)系確認(rèn)版權(quán)者。如涉及作品內(nèi)容、版權(quán)和其它問(wèn)題,請(qǐng)及時(shí)通過(guò)電子郵件或電話通知我們,以便迅速采取適當(dāng)措施,避免給雙方造成不必要的經(jīng)濟(jì)損失。聯(lián)系電話:010-82306118;郵箱:aet@chinaaet.com。