小白萌新,思路仅供参考,问题较多,有待改善
- 设计要求
4个按键为密码输入键,可以用另一个按键作为复位,当按下复位按键时可以重新开始输入新的密码。利用一位数码管显示输入密码的次数,另一位显示正确与否,如果正确数码管显示“H”,如果错误,数码管显示“E”.当超过3次时如果密码仍然输入不正确 - 程序
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_misc.all;
-- **************************************
entity Lock is
generic
(
mima:std_logic_vector(7 downto 0):="11011001" --默认密码
);
port
(
clk:in std_logic; --时钟
key:in std_logic_vector(3 downto 0); --按键输入
reset: in std_logic; --按键复位
beep:out std_logic; --蜂鸣器
tf:out std_logic_vector(6 downto 0); --正确错误信息
num:out std_logic_vector(6 downto 0) --次数
);
end Lock;
-- **************************************
architecture behv of Lock is
type state is(st0,st1,st2,st3,st4,st5,ss0,ss1,ss2,ss3); --状态1
signal current_state,next_state:state:=st0; --当前状态,下一状态
signal secret_temp:std_logic_vector(7 downto 0):="00000000";
signal tf_cnt:integer range 0 to 7:=0; --错误计数
signal beep_cnt:integer range 0 to 7:=0 ; --错误计数
signal key_temp:std_logic;--按键释放
begin
COM1:process(clk) --时钟
begin
if clk'event and clk='1' then
current_state<=next_state;
end if;
end process COM1;
-- **************************************
COM2:process(current_state,key,reset) --按键读取
begin
case current_state is
when st0=>
if key/="1111" then
case key is
when "0111" =>secret_temp(0)<='0';secret_temp(1)<='0';
when "1011" =>secret_temp(0)<='0';secret_temp(1)<='1';
when "1101" =>secret_temp(0)<='1';secret_temp(1)<='0';
when "1110" =>secret_temp(0)<='1';secret_temp(1)<='1';
when others => null;
end case;
next_state<=ss0;
else
next_state<=st0;
end if;
if reset='0' then
next_state<=st0;
end if;
beep<='0';
-- **************************************
when ss0=>
if key="1111" then
next_state<=st1;
else
next_state<=current_state;
end if;
beep<='0';
-- **************************************
when st1 =>
if key/="1111" then
case key is
when "0111" =>secret_temp(2)<='0';secret_temp(3)<='0';
when "1011" =>secret_temp(2)<='0';secret_temp(3)<='1';
when "1101" =>secret_temp(2)<='1';secret_temp(3)<='0';
when "1110" =>secret_temp(2)<='1';secret_temp(3)<='1';
when others => null;
end case;
next_state<=ss1;
else
next_state<=st1;
end if;
if reset='0' then
next_state<=st0;
end if;
beep<='0';
-- **************************************
when ss1=>
if key="1111" then
next_state<=st2;
else
next_state<=current_state;
end if;
beep<='0';
-- **************************************
when st2=>
if key/="1111" then
case key is
when "0111" =>secret_temp(4)<='0';secret_temp(5)<='0';
when "1011" =>secret_temp(4)<='0';secret_temp(5)<='1';
when "1101" =>secret_temp(4)<='1';secret_temp(5)<='0';
when "1110" =>secret_temp(4)<='1';secret_temp(5)<='1';
when others => null;
end case;
next_state<=ss2;
else
next_state<=st2;
end if;
if reset='0' then
next_state<=st0;
end if;
beep<='0';
-- **************************************
when ss2=>
if key="1111" then
next_state<=st3;
else
next_state<=current_state;
end if;
beep<='0';
-- **************************************
when st3=>
if key/="1111" then
case key is
when "0111" =>secret_temp(6)<='0';secret_temp(7)<='0';
when "1011" =>secret_temp(6)<='0';secret_temp(7)<='1';
when "1101" =>secret_temp(6)<='1';secret_temp(7)<='0';
when "1110" =>secret_temp(6)<='1';secret_temp(7)<='1';when others => null;
end case;
next_state<=ss3;
else
next_state<=st3;
end if;
if reset='0' then
next_state<=st0;
end if;
beep<='0';
-- **************************************
when ss3=>
if key="1111" then
next_state<=st4;
else
next_state<=current_state;
end if;
beep<='0';
-- **************************************
when st4=>
if mima=secret_temp then
next_state<=st0;
tf<="0110110";
else
next_state<=st5;
tf<="1111001";
end if;
beep<='0';
-- **************************************
when st5=>
if tf_cnt<3 then
next_state<=st0;
elsif tf_cnt=3 then
if beep_cnt<3 then
next_state<=st5;
beep<='1';
elsif beep_cnt=3 then
next_state<=st0;
beep<='0';
end if;
end if;
end case;
-- **************************************
case tf_cnt is
when 0 => num<="1000000";
when 1 => num<="1111001";
when 2 => num<="0100100";
when 3 => num<="0110000";
when others=> null;
end case;
end process COM2;
COM3:process(clk)
begin
if clk'event and clk='1' then
case current_state is
when st5=>
if tf_cnt<3 then
tf_cnt<=tf_cnt+1;
elsif tf_cnt=3 then
if beep_cnt<3 then
beep_cnt<=beep_cnt+1;
elsif beep_cnt=3 then
tf_cnt<=0;
beep_cnt<=0;
end if;
end if;
when others=> null;
end case;
end if;
end process COM3;
end behv;
- 波形
- 分析