新闻  |   论坛  |   博客  |   在线研讨会
VHDL MOTORCTRL 参考
larrybird | 2010-06-05 23:31:17    阅读:1693   发布文章

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;

library SYNOPSYS;
use SYNOPSYS.attributes.all;

entity motorctrl is
  port (clk: in STD_LOGIC;
        reset: in STD_LOGIC;
        speed_now: in STD_LOGIC_VECTOR (7 downto 0);
        target_speed: in STD_LOGIC_VECTOR (7 downto 0);
        th_speed: in STD_LOGIC_VECTOR (7 downto 0);
        pwme: out STD_LOGIC);
end;

architecture motorctrl_arch of motorctrl is
--define the signal_structure and flow of the device
signal cnt125: INTEGER range 0 to 124;
signal cnt375: INTEGER range 0 to 374;
signal cnt500: INTEGER range 0 to 499;
signal dth: STD_LOGIC_VECTOR (7 downto 0);
signal pwm: STD_LOGIC;

-- USER DEFINED ENCODED state machine: phase
type phase_type is (phB1, phB2);
attribute enum_encoding of phase_type: type is
 "0 " &  -- phB1
 "1 ";  -- phB2
signal phase: phase_type;
--------------------------------------------

-- USER DEFINED ENCODED state machine: speeding
type speeding_type is (UPA, UPB ,Dn ,final);
attribute enum_encoding of speeding_type: type is
        "00 " &          --UPA
        "01 " &          --UPB
        "10 " &          --Dn
        "11 ";           --final
signal speeding: speeding_type;
-----------------------------------------------

begin
-- concurrent signals assignments
--MotorControl
peeding_machine: process (reset,clk)
begin
   if reset='1' then
      speeding <= UPA;
      dth<="00000000";
   elsif (clk'event and clk='1') then
         dth<=target_speed -  th_speed;
      case speeding is
           when UPA =>
               if (speed_now = target_speed) then
     speeding <= final;
        elsif (speed_now > target_speed) then
        speeding <= Dn;
        elsif (speed_now < dth) then
        speeding <= UPA;
        elsif (speed_now >=dth) and (speed_now < target_speed) then
        speeding <= UPB;
        end if;
       
      when UPB =>
          if (speed_now = target_speed) then
     speeding <= final;
        elsif (speed_now < dth) then
        speeding <= UPA;
        elsif (speed_now >=dth) and (speed_now < target_speed) then
        speeding <= UPB;
        elsif (speed_now > target_speed) then
        speeding <= Dn;
        end if;
       
    when Dn =>
              if (speed_now = target_speed) then
    speeding <= final;
       elsif (speed_now < dth) then
       speeding <= UPA;
              elsif (speed_now > target_speed) then
              speeding <= Dn;
       elsif (speed_now >=dth) and (speed_now < target_speed) then
       speeding <= UPB;
       end if;
      
    when final =>
          if (speed_now > target_speed) then
       speeding <= Dn;
   elsif (speed_now >=dth) and (speed_now < target_speed) then
          speeding <= UPB;
   elsif (speed_now = target_speed) then
          speeding <= final;
   elsif (speed_now < dth) then
          speeding <= UPA;
   end if;
   
    when others =>
              null;
      end case;
   end if;
end process;

phase_machine: process (reset,clk)
begin
   if reset='1' then
      cnt500 <= 0;
      cnt375 <= 0;
      cnt125 <= 0;
      pwm<='0';
      pwme<='0';
      phase<=phB1;
   elsif (clk'event and clk='1') then
         pwme<=pwm;
      case phase is
    when phB1 =>
  case speeding is
              when UPA =>
           cnt375<=374;
    cnt125<=124;
    cnt500<=499;
    pwm<='1';
    
       when UPB =>
    cnt500<=499;
    cnt375<=cnt375-1;
    cnt125<=124;
    pwm<='1';
    
       when Dn =>
          cnt500<=499;
          cnt375<=cnt375-1;
          cnt125<=124;
          pwm<='0';
         
       when final =>
             cnt500<=cnt500-1;
      cnt375<=374;
      cnt125<=124;
      pwm<='1';
     
      when others =>
                    null;
                end case;
               
                if (cnt375=0 or cnt500=0) then
                    cnt500<=499;
                    cnt375<=374; 
      phase <= phB2;
  end if;
  
    when phB2 =>
  case speeding is
       when UPA =>
    cnt375<=374;
    cnt125<=124;
    cnt500<=499;
    pwm<='1';
    
       when UPB =>
    cnt500<=499;
    cnt375<=374;
    cnt125<=cnt125-1;
    pwm<='0';
    
       when Dn =>
          cnt500<=499;
          cnt375<=374;
          cnt125<=cnt125-1;
          pwm<='1';
         
       when final =>
             cnt500<=cnt500-1;
      cnt375<=374;
      cnt125<=124;
      pwm<='0';
     
       when others =>
              null;             
  end case;
  
  if (cnt125=0 or cnt500=0) then
                    cnt500<=499;
                    cnt125<=124; 
      phase <= phB1;
  end if;
  
    when others =>
     null;
      end case;
   end if;
end process;

end motorctrl_arch;

 

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客