📄 code.vhd
字号:
---**the copyright has clear the shake problems
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
--*****************************************************
ENTITY CODE IS
PORT(
clk,user_admin,clear,enter,chgcode,res:IN STD_LOGIC;
num:IN STD_LOGIC_VECTOR(9 DOWNTO 0);
openlock:buffer std_logic;
alarm,o_ua,o_chgcode,chgcode_ua:OUT STD_LOGIC;
s_out:out std_logic_vector(3 downto 0)
);
END CODE;
--******************************************************
ARCHITECTURE behave OF CODE IS
TYPE STATES IS (ss,s1,s2,s3,s4,check,sa_open,su_open,chos_ua,s_chgcode,Uchgcode,back);
SIGNAL state:STATES;
SIGNAL one_key,code1,code2,code3,code4:STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL user_code,admin_code:STD_LOGIC_VECTOR(15 DOWNTO 0);
signal temp_code,old_code,new1_code,new2_code:STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL u_err,chg_num:STD_LOGIC_VECTOR(1 DOWNTO 0);
signal key:std_logic_vector(13 downto 0);
signal inkey,outopen,ifnum,s_ua,chg_c,a_ua,back_time:std_logic;
signal sure_key,shake_0key,x,y,dout:std_logic;
signal time:STD_LOGIC_VECTOR(7 DOWNTO 0);
signal shake_key:std_logic_vector(3 downto 0);
BEGIN
temp_code<=code4&code3&code2&code1;--
key<=user_admin&chgcode&enter&clear#--
inkey<=user_admin or chgcode or enter or clear or num(0) or num(1) or num(2) or num(3) or num(4) or num(5) or num(6) or num(7) or num(8) or num(9);
ifnum<=not(one_key(3) and ((one_key(2) or one_key(1))));--ifnum='1',the key is a num
o_ua<=s_ua;--
chgcode_ua<=a_ua;--
o_chgcode<=chg_c;--
back_time<= time(7);--
main:PROCESS(res,clk)
BEGIN
if (res='1')then
state<=ss;
user_code<="0001000100010001";--1111
admin_code<="1000100010001000";--8888
u_err<="00";
chg_num<="00";
s_ua<='0';
a_ua<='0';
chg_c<='0';----when the code keep the changing state,the signal is 1
elsif clk'EVENT AND clk='1' then
case state is
when ss=>
if ifnum='1' then --
code1<=one_key;
state<=s1;
elsif (one_key="1101" and chg_c='0')then
s_ua<=not s_ua;
state<=ss;
elsif (one_key="1110") then state<=ss;
elsif(back_time='1')then state<=back;
end if;
when s1=>
if(ifnum='1')then --
code2<=one_key;
state<=s2;
elsif (one_key="1010")then --clear
state<=ss;
elsif(back_time='1')then state<=back;
elsif (one_key="1110") then state<=s1;
end if;
when s2=>
if(ifnum='1')then --
code3<=one_key;
state<=s3;
elsif (one_key="1010")then --clear
state<=s1;
elsif(back_time='1')then state<=back;
elsif (one_key="1110") then state<=s2;
end if;
when s3=>
if(ifnum='1')then --
code4<=one_key;
state<=s4;
elsif (one_key="1010")then --clear
state<=s2;
elsif(back_time='1')then state<=back;
elsif (one_key="1110") then state<=s3;
end if;
when s4=>
if (one_key="1011" and chg_c='0')then --enter
state<=check;
elsif (one_key="1010")then --clear
state<=s3;
elsif (one_key="1011" and chg_c='1' and s_ua='1' and a_ua='0')then
state<=s_chgcode;
elsif (one_key="1011" and chg_num="01")then
chg_num<="10";
old_code<=temp_code;
state<=ss;
elsif (one_key="1011" and chg_num="10")then
chg_num<="11";
new1_code<=temp_code;
state<=ss;
elsif (one_key="1011" and chg_num="11")then
chg_num<="00";
new2_code<=temp_code;
state<=s_chgcode;
elsif(back_time='1')then state<=back;
elsif (one_key="1110") then state<=s4;
end if;
when check=>
if(s_ua='1' and temp_code=admin_code) then state<=sa_open;
elsif(s_ua='1' and temp_code/=admin_code)then state<=ss;
elsif(s_ua='0' and u_err="11")then state<=ss;
elsif(s_ua='0' and temp_code=user_code)then state<=su_open;
elsif(s_ua='0' and temp_code/=user_code and u_err/="11")then u_err<=u_err+1; state<=ss;
else state<=ss;
end if;
when su_open=>
u_err<="00";--clear the error times for user
if (one_key="1011" or one_key="1101")then
--press enter or u_a key back to wait state
state<=ss;
elsif (one_key="1100") then
chg_c<='1';
chg_num<="01";
state<=ss;
else state<=su_open;
end if;
when sa_open=>
u_err<="00";
if (one_key="1011")then --enter
state<=ss;
elsif (one_key="1100")then --chgcode
chg_c<='1';
state<=chos_ua;
elsif one_key="1110" then state<=sa_open;
end if;
when chos_ua=>
if (one_key="1101")then--u_a
a_ua<=not a_ua;
elsif (one_key="1011" and a_ua='1' )then --enter
chg_num<="01";
state<=ss;
elsif (one_key="1011" and a_ua='0' )then --enter
state<=ss;
elsif one_key="1110" then state<=chos_ua;
end if;
when s_chgcode=>chg_c<='0';
if (a_ua='0' and s_ua='1')then
user_code<=temp_code;
state<=ss;
elsif (s_ua='0')then state<=Uchgcode;
elsif(a_ua='1' and old_code=admin_code and new1_code=new2_code)then
admin_code<=new1_code;
a_ua<='0';
state<=ss;
else state<=ss;
end if;
when Uchgcode=>chg_c<='0';
if (old_code=user_code and new1_code=new2_code)then
user_code<=new1_code;
state<=ss;
else state<=ss;
end if;
when back=>
chg_c<='0';
s_ua<='0';
a_ua<='0';
chg_num<="00";
state<=ss;
when others =>null;
end case;
END IF;
END PROCESS main;
--********************************
--********************************
INPUT:PROCESS(clk)
BEGIN
if dout='0' then one_key<="1110";
elsif clk'event and clk='0' then
CASE key IS
WHEN "00000000000001"=>one_key<="0000";--0
WHEN "00000000000010"=>one_key<="0001";--1
WHEN "00000000000100"=>one_key<="0010";--2
WHEN "00000000001000"=>one_key<="0011"; --3
WHEN "00000000010000"=>one_key<="0100"; --4
WHEN "00000000100000"=>one_key<="0101"; --5
WHEN "00000001000000"=>one_key<="0110"; --6
WHEN "00000010000000"=>one_key<="0111"; --7
WHEN "00000100000000"=>one_key<="1000"; --8
WHEN "00001000000000"=>one_key<="1001"; --9
when "00010000000000"=>one_key<="1010";--clear
when "00100000000000"=>one_key<="1011";--enter
when "01000000000000"=>one_key<="1100";--chgcode
when "10000000000000"=>one_key<="1101";--u_a
when "00000000000000"=>one_key<="1110";--u_1
WHEN OTHERS =>one_key<="1111";
END CASE;
end if;
END PROCESS INPUT;
--*******************************
--******************************
process(inkey,clk)--sampling
begin
if inkey='0' then
shake_key<="0000";
elsif clk'event and clk='1' then
if shake_key(3)='1' then
shake_key<=shake_key;
else shake_key<=shake_key+1;
end if;
end if;
end process;
--*****************************
--*******************************
process(clk)
begin
if clk'event and clk='1' then
x<=shake_key(3);
y<=x;
end if;
dout<=x and( not y);
end process;
--*******************************
process(clk)
begin
alarm<=u_err(0) and u_err(1);--to show alarm
case state is
when sa_open=>openlock<='1';
when su_open=>openlock<='1';
when s1=>s_out<="0001";
when s2=>s_out<="0010";
when s3=>s_out<="0100";
when s4=>s_out<="1000";
when others=>openlock<='0';s_out<="0000";
end case;
end process;
--******************************
process(clk)
begin
if one_key/="1110" or openlock='1' then time<="00000000";
elsif clk'EVENT AND clk='1' then time<=time+1;
end if;
end process;
--******************************
END behave;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -