VHDL課程設計:四位電子密碼鎖 c#
荒廢了一個假期,快要開學了,寫篇博客"慶祝慶祝",同時,今天心情也不是很好,算了,廢話很少說,下面進入正題吧。ide
1.題目要求:spa
本次博客的題目是利用VHDL設計一個四位密碼鎖,題目要求以下:設計
四位密碼,使用數據開關K1-K10分別表明數字0-9rest
輸入密碼用數碼管顯示,每輸入一位,密碼左移一位component
刪除的是最後一位數字,刪除一位,右移一位,空出位補充」0」blog
用一位輸出電平表示鎖開閉狀態ip
設置萬能密碼,在忘記密碼的狀況下能夠打開鎖get
2.源碼及註釋(文件附件下載):input
編譯的軟件爲Quartus II13.0,工程如何創建你們應該都知道了,這塊很少講,就直接添加各個模塊相關源碼及註釋 PS:實在不想排版了,附件有源碼文件加載,須要用的直接下載就行。
頂層文件:fanzhen.vhd
--頂層文件。 LIBRARY ieee; USE ieee.std_logic_1164.all; LIBRARY work; --實體描述 ENTITY fangzhen IS port( rest : IN STD_LOGIC; shizhongxinhao : IN STD_LOGIC; gaimij : IN STD_LOGIC; querenj : IN STD_LOGIC; shanchuj : IN STD_LOGIC; jianpanshurru : IN STD_LOGIC_VECTOR(3 downto 0); zhenque_gaodianping : OUT STD_LOGIC; baojing : OUT STD_LOGIC; zhishideng : OUT STD_LOGIC; qimaguanxianshi : OUT STD_LOGIC_VECTOR(15 downto 0)); END fangzhen; --結構體描述 ARCHITECTURE bdf_type OF fangzhen IS --驗證改密元件定義 component yzgm PORT( clk : IN STD_LOGIC; gaimij2 : IN STD_LOGIC; queren : IN STD_LOGIC; input2 : IN STD_LOGIC_VECTOR(15 downto 0); output2 : OUT STD_LOGIC); end component; --移位寄存器元件定義 component ywjcq PORT( rest : IN STD_LOGIC; shanchu : IN STD_LOGIC; clk : IN STD_LOGIC; input : IN STD_LOGIC_VECTOR(3 downto 0); output : OUT STD_LOGIC_VECTOR(15 downto 0)); end component; --數碼管顯示元件定義 component yimasc PORT( clk : IN STD_LOGIC; datain : IN STD_LOGIC_VECTOR(15 downto 0); dataout : OUT STD_LOGIC_VECTOR(15 downto 0)); end component; --電鎖控制元件定義 component dskz PORT( clk : IN STD_LOGIC; input : IN STD_LOGIC; reset : IN STD_LOGIC; queren : IN STD_LOGIC; light : OUT STD_LOGIC; alarm : OUT STD_LOGIC); end component; --d4觸發器元件定義 component d4 port( clk:in std_logic; a: in std_logic_vector(3 downto 0); rest1,querenj1,gaimij1,shanchuj1:in std_logic; q:out std_logic_vector(3 downto 0); rest2,querenj2,gaimij2,shanchuj2:out std_logic; oclk:out std_logic); end component; --輸入密碼存放 signal SYNTHESIZED_WIRE_0 : STD_LOGIC_VECTOR(15 downto 0); --開鎖信號 signal SYNTHESIZED_WIRE_1 : STD_LOGIC:='0'; --按鍵 signal key:std_LOGIC_VECTOR(3 downto 0); --依次是重置、確認、改密、刪除、以及鍵盤輸入信號 signal rest3,querenj3,gaimij3,shanchuj3,ok: STD_LOGIC; BEGIN zhenque_gaodianping <= SYNTHESIZED_WIRE_1; --驗證改密元件例化 b2v_inst : yzgm PORT MAP(clk => shizhongxinhao, gaimij2 => gaimij3, queren => querenj3, input2 => SYNTHESIZED_WIRE_0, output2 => SYNTHESIZED_WIRE_1); --移位寄存器元件例化 b2v_inst1 : ywjcq PORT MAP(rest => rest3, shanchu => shanchuj3, clk => ok, input => key, output => SYNTHESIZED_WIRE_0); --數碼管顯示元件例化 b2v_inst2 : yimasc PORT MAP( clk => shizhongxinhao, datain => SYNTHESIZED_WIRE_0, dataout => qimaguanxianshi); --電鎖控制元件例化 b2v_inst3 : dskz PORT MAP(clk => shizhongxinhao, input => SYNTHESIZED_WIRE_1, reset => rest3, queren => querenj3, light => zhishideng, alarm => baojing ); --d4觸發器例化 b2v_inst4 : d4 port map( clk=>shizhongxinhao, a=>jianpanshurru, rest1=>rest, querenj1=>querenj, gaimij1=>gaimij, shanchuj1=>shanchuj, q=>key, rest2=>rest3, querenj2=>querenj3, gaimij2=>gaimij3, shanchuj2=>shanchuj3, oclk=>ok); END;
D4觸發器:d4.vhd
--D觸發器:實現消抖做用 library ieee; use ieee.std_logic_1164.all; --d4觸發器實體描述 entity d4 is port( clk:in std_logic; a: in std_logic_vector(3 downto 0); rest1,querenj1,gaimij1,shanchuj1:in std_logic; q:out std_logic_vector(3 downto 0); rest2,querenj2,gaimij2,shanchuj2:out std_logic; --表示按鍵按下信號 以及刪除鍵按下信號 oclk:out std_logic); end d4; --d4結構體描述 architecture f of d4 is signal qi:integer range 0 to 200; signal clk_temp,delay:std_logic; begin process(clk) begin if rising_edge(clk) then if qi=200 then qi<=0; clk_temp<= '1'; else qi<=qi+1; clk_temp<= '0'; end if; end if; end process; process(clk_temp) begin if clk_temp 'event and clk_temp ='1' then if a/="0000" then q <=a; oclk<='1'; elsif shanchuj1/='0' then shanchuj2<='1'; oclk<='1'; else shanchuj2<='0'; oclk<='0'; end if; rest2<=rest1;querenj2<=querenj1; gaimij2<=gaimij1; end if; end process; end if;
數碼管顯示模塊:yimasc.vhd
--數碼管顯示部分代碼 library ieee; use ieee.std_logic_1164.all; --實體描述 entity yimasc is port( --顯示數據 datain:in std_logic_vector(15 downto 0); clk:in std_logic; --數碼管輸出 dataout:out std_logic_vector(15 downto 0)); end yimasc; --結構體描述 architecture behave of yimasc is begin process(clk) begin if clk'event and clk='1' then dataout<=datain; end if; end process; end behave;
移位寄存器模塊:ywjcq.vhd
--移位寄存器,用於保存四位密碼、刪除密碼控制 library ieee; use ieee.std_logic_1164.all; --實體描述 entity ywjcq is port( --按鍵輸入(一位) input:in std_logic_vector(3 downto 0); --從新輸入,刪除,輸入脈衝 rest,shanchu,clk :in std_logic; --密碼輸出 output :out std_logic_vector(15 downto 0)); end ywjcq; --結構體描述 architecture behave of ywjcq is begin process(clk,shanchu) variable output_tmp: std_logic_vector(15 downto 0):="1111111111111111"; variable ok:std_logic:='0'; begin if rest='1' then output_temp:="1110111011101110"; elsif (rising_edge(clk)) then if shanchu='0' then output_temp(15 downto 12):=output_temp(11 downto 8); output_temp(11 downto 8):=output_temp(7 downto 4); output_temp(7 downto 4):=output_temp(3 downto 0); output_temp(3 downto 0):=input; else output_temp(3 downto 0):=output_temp(7 downto 4); output_temp(7 downto 4):=output_temp(11 downto 8); output_temp(11 downto 8):=output_temp(15 downto 12); output_temp(15 downto 12):="1110"; end if; end if; output<=output_temp; end process; end behave;
驗證改密模塊:yzgm.vhd
--驗證改密模,驗證密碼以及改密 library ieee; use ieee.std_logic_1164.all; --模塊試題描述 entity yzgm is port ( --時鐘信號,改密鍵,確認鍵 clk,gaimij2,queren:in std_logic; --輸入密碼 input2:in std_logic_vector(15 downto 0); --鎖信號,1:表示驗證經過 0:表示驗證未經過 output2:out std_logic); end yzgm; --結構體描述 architecture a of yzgm is begin process (clk,gaimij2) --設置初始密碼是:8421 variable input2_temp:std_logic_vector(15 downto 0):="1000010000100001"; --萬能密碼爲:8888 variable input2_temp1:std_logic_vector(15 downto 0):="1000100010001000"; begin if clk'event and clk='1' then --改密 if gaimij2='1' then input2_temp :=input2; end if; --驗證 if queren='1' then if input2_temp=input2 or input2=input2_temp1 then output2<='1'; else output2<='0'; end if; end if; end if; end process; end a;
以上就是工程須要創建的相關文件,工程創建之後,就直接編譯,而後配置好引腳燒錄就行。
3.答辯講解PPT(點擊下載PPT):
像這種課程設計類的答辯基本就是這種模式,首先講題目要求,而後方案,接下來就是源碼講解,最後就是試驗中遇到的問題以及隨便寫一些心得體會就行。
部分PPT截圖: