--设计一个三位数的电子密码锁,通过输入的数字控制密码锁的开关。 
--开锁器件用户可通过change键自行设置密码; 
--开锁时,先输入密码,按下test键检测,密码正确时开锁:输出lockopen为高电平,lockclose 
--为低电平,密码锁开启;否则lockopen为低电平,lockclose为高电平,密码锁关闭; 
--lockopen和lockclose分别用来驱动绿色和红色发光二极管,作为密码锁的状态显示; 
--任何状态下输入的三位数字将在七段共阴极数码管显示。 
 
--主控制模块 
library ieee; 
use ieee.std_logic_1164.all; 
entity lock is 
port(numh,numt,numo:in std_logic_vector(9 downto 0);--百十个位数输入 
         displayh,displayt,displayo ut std_logic_vector(6 downto 0);--数码管驱动百十个位数 
         clk:in std_logic; 
         change,test:in std_logic;--密码设定和密码检测 
         lockopen,lockclose ut std_logic--开锁和关锁信号 
        ); 
end lock; 
 
architecture rt1 of lock is 
component decoder is 
        port( 
                clk:in std_logic; 
                data:in std_logic_vector(9 downto 0); 
                q ut std_logic_vector(6 downto 0);   --七位数码管驱动 
                q1 ut std_logic_vector(3 downto 0)   --数值译码 
                ); 
end component; 
signal enable ,c0,c1,s,enable1:std_logic; 
signal temph,tempt,tempo,deco_h,deco_t,deco_o:std_logic_vector(3 downto 0); 
begin 
        enable<=change and (not test); 
        enable1<=test and (not change); 
u0:decoder 
        port map(clk=>clk,data=>numh,q=>displayh,q1=>deco_h); --百位输入译码 
u1:decoder 
        port map(clk=>clk,data=>numt,q=>displayt,q1=>deco_t); --十位输入译码 
u2:decoder 
        port map(clk=>clk,data=>numo,q=>displayo,q1=>deco_o);--个位输入译码 
 
        process(clk,deco_h,deco_t,deco_o) 
begin 
        if rising_edge(clk) then 
                if enable='1' then 
                        temph<=deco_h; 
                        tempt<=deco_t; 
                        tempo<=deco_o; 
                end if ; 
                if enable1='1' then 
                        if temph=deco_h and tempt=deco_t and tempo=deco_o then 
                                lockopen<='1'; 
                                lockclose<='0'; 
                        else 
                                lockopen<='0'; 
                                lockclose<='1'; 
                        end if; 
                end if; 
        end if; 
        end process; 
end rt1; 
 
         
 
 
 
--输入译码模块 
library ieee; 
use ieee.std_logic_1164.all; 
entity decoder is 
port( 
        clk:in std_logic; 
        data:in std_logic_vector(9 downto 0); 
        q ut std_logic_vector(6 downto 0); 
        q1 ut std_logic_vector(3 downto 0) 
        ); 
end decoder; 
 
architecture rt1 of decoder is 
begin 
        process(clk,data) 
        begin 
                if rising_edge(clk) then 
                case data is 
                        when "0000000000"=>q<="0110000";q1<="0000"; 
                        when "0000000001"=>q<="0110000";q1<="0000"; 
                        when "0000000010"=>q<="0111111";q1<="0001"; 
                        when "0000000100"=>q<="1101101";q1<="0010"; 
                        when "0000001000"=>q<="1111001";q1<="0011"; 
                        when "0000010000"=>q<="0110011";q1<="0100"; 
                        when "0000100000"=>q<="1011011";q1<="0101"; 
                        when "0001000000"=>q<="0011111";q1<="0110"; 
                        when "0010000000"=>q<="1110000";q1<="0111"; 
                        when "0100000000"=>q<="1111111";q1<="1000"; 
                        when "1000000000"=>q<="1110011";q1<="1001"; 
                        when others=>q<="0000000";q1<="0000"; 
                end case; 
        end if; 
        end process; 
end rt1; |