原创 【转】VHDL设计举例:直流电机控制器

2010-11-5 01:08 1256 2 2 分类: FPGA/CPLD

LIBRARY IEEE;


USE IEEE.std_logic_1164.ALL;


USE IEEE.std_logic_unsigned.all;


USE IEEE.std_logic_arith.all;


ENTITY dccount IS


     port (


                clk    : IN STD_LOGIC;


              AI : IN STD_LOGIC_VECTOR(3 DOWNTO 0);


              CO : out STD_LOGIC_VECTOR(3 DOWNTO 0);


                pulse: IN STD_LOGIC;


              driverA,driverB: OUT STD_LOGIC;


              S : OUT STD_LOGIC_VECTOR(6 DOWNTO 0);


              P : OUT STD_LOGIC_VECTOR(5 DOWNTO 0);


               sp  : out STD_LOGIC);


END dccount;


ARCHITECTURE a OF dccount IS


SIGNAL f:  STD_LOGIC_VECTOR(5 downto 0);


SIGNAL f_hz: STD_LOGIC;


SIGNAL OSC: STD_LOGIC;


SIGNAL OSC1: STD_LOGIC;


SIGNAL C: STD_LOGIC_VECTOR(3 DOWNTO 0);


SIGNAL A: STD_LOGIC_VECTOR(3 DOWNTO 0);


SIGNAL CODE: STD_LOGIC_VECTOR(2 DOWNTO 0);


SIGNAL bcd:STD_LOGIC_VECTOR(23 downto 0);


SIGNAL COUNT:STD_LOGIC_VECTOR(23 downto 0);


SIGNAL SUM: STD_LOGIC_VECTOR(4 DOWNTO 0);


SIGNAL D: STD_LOGIC_VECTOR(3 DOWNTO 0):="0000";


SIGNAL ind_coil: STD_LOGIC_VECTOR(3 downto 0) := "0001";


SIGNAL Hz:STD_LOGIC;


SIGNAL spo: STD_LOGIC;


SIGNAL SW:STD_LOGIC;


SIGNAL KEY:STD_LOGIC;


SIGNAL DRA,DRB:STD_LOGIC;


BEGIN


DRIVERA<=DRA;


DRIVERB<=DRB;


p(5 downto 0) <= f(5 downto 0);


CO(3 DOWNTO 0) <=C(3 DOWNTO 0);


A (3 DOWNTO 0)<=AI(3 DOWNTO 0);


SP<=SPO;


-----------------div------------------------


PROCESS(OSC)


     VARIABLE d_ff: STD_LOGIC_VECTOR(20 downto 0);


    VARIABLE DELAY: STD_LOGIC_VECTOR(23 DOWNTO 0);


  BEGIN


 WAIT UNTIL clk = '1';


         IF d_ff(20 downto 0) >= 2E6 THEN


             d_ff(20 downto 0) := "000000000000000000000";


         ELSE


             d_ff(20 downto 0) := d_ff + 1;


         END IF;


         f_hz <= not d_ff(6); 


        OSC <= not d_ff(10);


         OSC1 <= not d_ff(17);


        IF DELAY>=500000 THEN


           DELAY:="000000000000000000000000";HZ<=NOT Hz;


        ELSE DELAY:=DELAY+1;


        END IF;


END PROCESS ;


----------------------------------------------


     PROCESS (F_HZ,OSC,OSC1,HZ,PULSE)


    VARIABLE B: STD_LOGIC;


    VARIABLE INT: STD_LOGIC;


     VARIABLE Data: STD_LOGIC_VECTOR(3 DOWNTO 0);


    VARIABLE UP: STD_LOGIC_VECTOR(2 DOWNTO 0);


     VARIABLE  ST:STD_LOGIC_VECTOR(2 DOWNTO 0);


    VARIABLE seq: STD_LOGIC_VECTOR(1 DOWNTO 0);


    VARIABLE sound: STD_LOGIC_VECTOR(1 DOWNTO 0);


    VARIABLE STOP: STD_LOGIC;


    VARIABLE direction: STD_LOGIC;


    BEGIN


---------------7seg scan-------------------------------


     IF (f_HZ='1' AND F_HZ'EVENT) THEN


 IF ST(2 DOWNTO 0)= "101" THEN


   ST(2 DOWNTO 0):="000" ;


 ELSE ST(2 DOWNTO 0):=ST(2 DOWNTO 0)+1;


 END IF;


 END IF;


-----------------------------------------------


IF (OSC = '1' and OSC'EVENT) THEN


     if a="1111" then


     IF seq="11" then seq:="00" ;


    else seq:=seq+1;


     end if;


     else seq:=seq;


    end if;


IF seq="00" then C(3 DOWNTO 0)<="1110" ;


elsif seq="01" then C(3 DOWNTO 0)<="1101" ;


elsif seq="10" then C(3 DOWNTO 0)<="1011" ;


elsif seq="11" then C(3 DOWNTO 0)<="0111" ;


end if;


end if;


IF A="1111" THEN INT:='0';


ELSE INT:='1';


END IF;


IF (HZ'EVENT AND HZ='1') THEN


IF STOP='1' THEN UP:=UP+1;


ELSE UP:="000";


END IF;


END IF;


IF KEY='1' AND COUNT/=0 THEN


  IF DIRECTION='0' THEN DRA<='1';DRB<='0';STOP:='0';


   ELSE DRA<='0';DRB<='1';STOP:='0';


  END IF;


 ELSIF KEY='1' AND COUNT=0 THEN STOP:='1';


 ELSIF STOP='1' AND UP<"110"  THEN DRA<=NOT DRA ;DRB<=NOT DRB;


ELSE  DRA<='0';DRB<='0';STOP:='0';


END IF;


iF  (OSC1'EVENT AND OSC1='1') THEN B:=(INT and (b and INT) );


----------------direction choice-----------------------


IF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1010" THEN


BCD(23 DOWNTO 0)<=BCD(23 DOWNTO 0);B:='0';direction:='1';


ELSIF SW='0' AND  B='1' AND Data(3 DOWNTO 0)="1011" THEN


BCD(23 DOWNTO 0)<=BCD(23 DOWNTO 0);B:='0';direction:='0';


------------------KEY LOCK ON/OFF------------------------------------


ELSIF B='1' AND Data(3 DOWNTO 0)="1100" THEN


BCD<=BCD;B:='0';SW<=NOT SW;


-------------------BACK-------------------------------


ELSIF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1101" THEN


BCD(23 DOWNTO 0)<=("0000" & bcd(23 downto 4) );B:='0';


-------------------CLEAR---------------------------------


ELSIF B='1' AND Data(3 DOWNTO 0)="1110" THEN


BCD<="000000000000000000000000" ;B:='0';SW<='0';


------------------DOWNCOUNT  0 ------------------------------------


ELSIF BCD=0  AND  KEY='1' THEN SW<='0';KEY<='0';BCD<=BCD;


------------------------------------------------------------------


ELSIF SW='1' AND KEY='1' AND DATA(3 DOWNTO 0)/="1110" THEN BCD<=COUNT;


ELSIF SW='1' AND DATA(3 DOWNTO 0)="1110" THEN SW<='0';


BCD<="000000000000000000000000";KEY<='0';


-------------------ENTER------------------------------


ELSIF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1111" AND BCD/=0 THEN


B:='0';SW<='1';KEY<='1';


ELSIF SW='0' AND B='1' AND Data(3 DOWNTO 0)="1111" AND BCD=0 THEN


BCD<=BCD;B:='0';SW<='0';KEY<='0';


----------------------NUMBER SHIFT---------------------------


ELSIF SW='0' AND B='1' THEN bcd(23 downto 0)<= bcd(19 downto 0) & DATA(3 DOWNTO 0);B:='0';


elsif INT='0' then bcd<=bcd;


b:='1';


END IF;


END IF;


------------------------------------------------------


SUM(1 DOWNTO 0)<= seq;


sum(4 downto 2)<=code;


---------------------SOUND-----------------------------


if (osc'event and osc='1') then


if INT='1' AND SW='0' then sound:=sound+1;spo<=sound(1) ;


else spo<='0';


end if;


end if;


-------------------------------------------------------


---------------------------DOWNCOUNT--------------------


IF SW='0' THEN COUNT<=BCD;


ELSE


If (PULSE = '1' and PULSE'EVENT) THEN


IF KEY='1' AND COUNT/=0 THEN


----------------------------1---------------------------------------------------


  IF COUNT(3 DOWNTO 0)= 0 AND COUNT(23 DOWNTO 4)>=1 THEN


      COUNT(3 DOWNTO 0) <="1001" ;


  ELSE COUNT(3 DOWNTO 0) <= COUNT(3 DOWNTO 0)-1;


  END IF;


----------------------------2---------------------------------------------------------  


    IF  COUNT(7 DOWNTO 4)= 0 AND COUNT(23 DOWNTO 8)>=1  AND COUNT(3 DOWNTO 0)=0 THEN


     COUNT(7 DOWNTO 4) <= "1001" ;


     ELSIF COUNT(3 DOWNTO 0)=0 AND COUNT(23 DOWNTO 4)>=1  THEN


    COUNT(7 DOWNTO 4) <=COUNT(7 DOWNTO 4 )-1;


 END IF;


----------------------------3-------------------------------------------------------


     IF  COUNT(11 DOWNTO 8)= 0 AND COUNT(23 DOWNTO 12)>=1 AND COUNT(7 DOWNTO 0)=0 THEN


     COUNT(11 DOWNTO 8) <="1001";


     ELSIF COUNT(7 DOWNTO 0)=0 AND COUNT(23 DOWNTO 8)>=1 THEN


    COUNT(11 DOWNTO 8) <=COUNT(11 DOWNTO 8 )-1;


END IF;


---------------------------4--------------------------------------------------------------


     IF  COUNT(15 DOWNTO 12)= 0 AND COUNT(23 DOWNTO 16)>=1 AND COUNT(11 DOWNTO 0)=0 THEN


     COUNT(15 DOWNTO 12) <="1001";


     ELSIF COUNT(11 DOWNTO 8)=0 AND COUNT(23 DOWNTO 12)>=1 AND COUNT(7 DOWNTO 0)=0 THEN


    COUNT(15 DOWNTO 12) <=COUNT(15 DOWNTO 12 )-1;


END IF;


-------------------------5------------------------------------------------------------


     IF  COUNT(19 DOWNTO 16)= 0 AND COUNT(23 DOWNTO 20)>=1 AND COUNT(15 DOWNTO 0)=0 THEN


     COUNT(19 DOWNTO 16) <="1001";


     ELSIF COUNT(15 DOWNTO 12)=0 AND COUNT(23 DOWNTO 16)>=1 AND COUNT(11 DOWNTO 0)=0 THEN


    COUNT(19 DOWNTO 16) <=COUNT(19 DOWNTO 16 )-1;


END IF;


-------------------------6-------------------------------------------------


     IF  COUNT(23 DOWNTO 20)= 0  THEN


     COUNT(23 DOWNTO 20) <="0000";


     ELSIF COUNT(19 DOWNTO 16)=0 AND COUNT(23 DOWNTO 20 )>=1 AND COUNT(15 DOWNTO 0)=0 THEN


    COUNT(23 DOWNTO 20) <=COUNT(23 DOWNTO 20 )-1;


    END IF;


END IF;


END IF;


END IF;


------------------7seg scan---------------------------


CASE ST IS                                                                     --?@潮


         WHEN "000"=>   D(3 DOWNTO 0) <= bcd(3 DOWNTO 0)  ; f(5 downto 0)<="111110";


        WHEN "001"=>   D(3 DOWNTO 0) <= bcd(7 DOWNTO 4)  ; f(5 downto 0)<="111101";


         WHEN "010"=>   D(3 DOWNTO 0) <= bcd(11 DOWNTO 8)  ; f(5 downto 0)<="111011";


         WHEN "011"=>   D(3 DOWNTO 0) <= bcd(15 DOWNTO 12)  ; f(5 downto 0)<="110111";


         WHEN "100"=>   D(3 DOWNTO 0) <= bcd(19 DOWNTO 16)  ; f(5 downto 0)<="101111";


         WHEN "101"=>   D(3 DOWNTO 0) <= bcd(23 DOWNTO 20)  ; f(5 downto 0)<="011111";


         WHEN OTHERS => D(3 DOWNTO 0)  <= "1100"  ; f(5 downto 0)<="111111";


END CASE;


------------------key scan-------------------------


CASE a(3 DOWNTO 0) IS


              WHEN"1110" => CODE<="100";


              WHEN"1101" => CODE<="101";


              WHEN"1011" => CODE<="110";


              WHEN"0111" => CODE<="111";


              WHEN OTHERS =>  CODE<="000";


end case;


---------------------key table------------------------


CASE SUM(4 DOWNTO 0) IS


 


              WHEN"10000"=> Data:="0001";--0


              WHEN"10001"=> Data:="0101";--1


              WHEN"10010"=> Data:="1001";--2


              WHEN"10011"=> Data:="1100";--3


              WHEN"10100"=> Data:="0010";--4


              WHEN"10101"=> Data:="0110";--5


              WHEN"10110"=> Data:="0000";--6


              WHEN"10111"=> Data:="1101";--7


              WHEN"11000"=> Data:="0011";--8


              WHEN"11001"=> Data:="0111";--9


----------------------------------------------------       


            WHEN"11010"=> Data:="1010";--A


              WHEN"11011"=> Data:="1110";--B-->


              WHEN"11100"=> Data:="0100";--C


              WHEN"11101"=> Data:="1000";--D


              WHEN"11110"=> Data:="1011";--E


              WHEN"11111"=> Data:="1111";--F


              WHEN OTHERS =>   Data:=Data;


END CASE;



--------------------7seg table----------------------------



CASE D IS


              WHEN"0000"=> S<="0111111"; --0


            WHEN"0001"=> S<="0000110"; --1


              WHEN"0010"=> S<="1011011"; --2


              WHEN"0011"=> S<="1001111"; --3


            WHEN"0100"=> S<="1100110"; --4


              WHEN"0101"=> S<="1101101"; --5


              WHEN"0110"=> S<="1111101"; --6


            WHEN"0111"=> S<="0000111"; --7


            WHEN"1000"=> S<="1111111"; --8


            WHEN"1001"=> S<="1100111"; --9


              WHEN"1010"=> S<="0111111"; --0


          --WHEN"1010"=> S<="1110111"; --a


            WHEN"1011"=> S<="1111100"; --b


            WHEN"1100"=> S<="0111001"; --c


            WHEN"1101"=> S<="1011110"; --d


            WHEN"1110"=> S<="1111001"; --e


            WHEN"1111"=> S<="1110001"; --f


            WHEN OTHERS => S<="ZZZZZZZ";


END CASE;


----------------------------------------------------


END PROCESS;


END a;

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
2
关闭 站长推荐上一条 /3 下一条