EDA课设(数字系统设计)--数字密码锁

目录

1,注意

2,可能遇到的问题

3,题目描述

4,实现前期准备

5,实现代码

6,引脚设置

7,部分验证


1,注意

该博客是根据自己的课设报告写的,所以大家不要抄袭,仅用作给大家提供实现思路以及一些经验,希望大家根据我写的东西,理解关键的代码,较为熟练的掌握VHDL语言的语法,规则以及流程,学会如何自己实现所有的功能;

2,可能遇到的问题

1,对于不会安装quartusII 9.0的同学可以参考前一篇博客:EDA课设(数字系统设计)--quartusII 9.0安装及altera usb-blaster驱动识别失败解决

2,如果大家不知道如何使用板载的led灯,8段数码管,clk信号,可以参考前一篇博客里面下载的资源,在目录:数字系统设计实验\实验,下的EDA-I便携式数字系统实验与设计平台说明书.pdf文件;

3,题目描述

完成一简易密码锁的设计,实现6位密码的设定与开锁。

1)使用6个按键进行密码输入,K0-K5,分别代表数字键0-5,用右边6个数码管显示;

2)密码初始值为555555;开锁方式:xxxxxx(x代表密码数字,位数等于6位);上电后,初始显示:"PP------";输入一个数字就在最右数码管显示,前面的数字均左移一个数码管。输入正确显示“--OPEN--”,输入错误显示“--EEEE--”。

3)设计一个重新输入按钮K6,在输入未全或者错误(没达到3次)时,恢复输入,按下后显示“PP------”

4)工作时钟1khz;连续3次输错密码则锁死,只有重启电路;连续2次错误点亮警报灯。

5) 用按键k7设置密码,设定方式:旧密码,输入两次,输入前显示为“OP------”,正确后提示输入新密码:“NP------”,连续输入2次。以上出错均显示“--EEEE--”,可按K7恢复设置,或者K6。

4,实现前期准备

根据题目的描述,我们可以将大体的设计分成两个主要部分,第一个部分是开锁,第二个部分是重置密码;

想明白自己要实现的功能之后,下面就是设计程序的流程了,因为VHDL内部绝大部分是并行执行的,但是我习惯了写顺序的代码,所以在编写程序时犯了许多的逻辑错误,比如对一些信号赋值时,本来想要的是一个一个改变,但是由于是并行执行,它总是全部一起赋值,导致数码管只能显示一样的内容,不能得到我想要的结果,后面我自己想了很久,也参考了一些别人的文章,经过自己的反复测试,发现状态的转换可以实现我想要的功能,所以我最后决定用类似状态转换的方式来实现我想要的效果,实现这个题目的要求并非几句话就可以说清楚的,过程是非常痛苦的,但是大家也不要因为一些困难就选择摆烂,应该要了解VHDL的结构,尽量理解书上的知识以及参考代码,达到能自己实现所有功能的目的,这应该也是课设的目的;

下面这个是我做这个题目的自己做思维导图,这个图是我实现密码锁所有之后才做的,算是后期的东西,放在这里主要是便于大家理解下面的代码,了解思路;

5,实现代码

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity seg is
	port(
		clk : in std_logic;
		key0,key1,key2,key3,key4,key5,key6,key7 : in std_logic;
		led_warning : out std_logic_vector(7 downto 0);
		smg_led : out std_logic_vector(7 downto 0);--数码管8段led
		smg_code : out std_logic_vector(7 downto 0)--8个数码管
);
end entity seg;
architecture display of seg is
	type smg_led_array is array (7 downto 0) of std_logic_vector(7 downto 0);
	signal smg_leds : smg_led_array;
	signal smg_index : integer range 0 to 7 :=0;
	
	type smg_code_array is array (0 to 9) of std_logic_vector(7 downto 0);
	signal smg_codes : smg_code_array;				--存放字符
	
	signal password : std_logic_vector(17 downto 0);
	signal password_code:std_logic_vector(17 downto 0) :="101101101101101101";
	
	type smg_int_array is array (7 downto 0) of integer;
	signal smg_int : smg_int_array :=(7,7,6,6,6,6,6,6);	--存放字符对应的数字
	
	signal counter,count : std_logic_vector(1 downto 0) := "00";
	signal i : std_logic_vector(2 downto 0) := "000"; 
	signal state : std_logic_vector(3 downto 0) := "0000";
	signal k0,k1,k2,k3,k4,k5,k6,k7 :  std_logic;
	
begin
	smg_codes(0)<="00111111";--0					--字符对应的数字数组
	smg_codes(1)<="00000110";--1
	smg_codes(2)<="01011011";--2
	smg_codes(3)<="01001111";--3
	smg_codes(4)<="01100110";--4
	smg_codes(5)<="01101101";--5
	smg_codes(6)<="01000000";--'-'
	smg_codes(7)<="01110011";--'P'
	smg_codes(8)<="01111001";--'E'
	smg_codes(9)<="00110111";--'N'
	--动态扫描数码管的进程
	smg_saomiao:process(clk)
	begin
		if clk'event and clk='1' then 
			smg_led<=smg_leds(smg_index);--shu_ma_guan_shu_chu
			smg_code <= not(conv_std_logic_vector(2**smg_index,8));
			if smg_index=7 then smg_index<=0;
			else smg_index<=smg_index+1;
			end if;
		end if;
	end process;
	--数码管显示进程
	smg_show:process(clk)
	begin
		smg_leds(7) <= smg_codes(smg_int(7));
		smg_leds(6) <= smg_codes(smg_int(6));
		smg_leds(5) <= smg_codes(smg_int(5));
		smg_leds(4) <= smg_codes(smg_int(4));
		smg_leds(3) <= smg_codes(smg_int(3));
		smg_leds(2) <= smg_codes(smg_int(2));
		smg_leds(1) <= smg_codes(smg_int(1));
		smg_leds(0) <= smg_codes(smg_int(0));
	end process;
	--处理按键的进程
	handle_key:process(clk,state,key0,key1,key2,key3,key4,key5,key6,key7)
	variable int_temp : smg_int_array ;
	begin
		if clk'event and clk='1' then						--统一时钟信号		k0<=key0;k1<=key1;k2<=key2;k3<=key3;k4<=key4;k5<=key5;k6<=key6;k7<=key7;
			--state0:										--状态0:初始化
			if state="0000" then
			int_temp:=(7,7,6,6,6,6,6,6);
			state<="0001";								--进入状态1
			i<="000";
			password<="000000000000000000";
			end if;
			--state0										--状态0结束
			--state1:										--状态1:确定密码
			if state="0001" then
				if i="110" then
					i<="000";
					if password=password_code then 		--密码正确
						led_warning<="00000000";
						counter<="00";
						state<="0010";					--进入状态2
						int_temp:=(6,6,0,7,8,9,6,6);		--显示“--OPEN--”
					else --mi_ma_false;
						counter<=counter+"01";
						--password<="000000000000000000";
						if counter="01" then 
							led_warning<="11111111"; 	--报警
							state<="0010";				--进入状态2
							int_temp:=(6,6,8,8,8,8,6,6);	--显示“--EEEE--”
						elsif counter="10" then 			--密码错误三次
							state<="1000";				--进入死锁状态
							int_temp:=(8,8,8,8,8,8,8,8);	--显示“EEEEEEEE”
						else 
							led_warning<="00000000";
							state<="0010";				--进入状态2
							int_temp:=(6,6,8,8,8,8,6,6);	--显示“--EEEE--”
						end if;
					end if;
				else 
					--判断是哪个key
					if (k0='0')and(key0='1') then			--key0
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="000";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),0);
					end if;
					if (k1='0')and(key1='1') then 			--key1
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="001";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),1);
					end if;
					if (k2='0')and(key2='1')then			--key2
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="010";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),2);
					end if;
					if (k3='0')and(key3='1') then 			--key3
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="011";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),3);
					end if;
					if (k4='0')and(key4='1') then 			--key4
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="100";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),4);
					end if;
					if (k5='0')and(key5='1') then			--key5
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="101";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),5);
					end if;
					if (k6='0')and(key6='1') then 			--key6
						state<="0000";					--回到状态0
						counter<="00";					--计数器清0
					end if;
					if (k7='0')and(key7='1') then
						state<="0011";					--进入状态3
					end if;
				end if;
			end if;
			--state1										--判断按键状态结束
			--state2:		--恢复状态,按key6重新输入,按key7进入重置密码
			if state="0010" then 
				password<="000000000000000000";
				if (k6='0')and(key6='1') then
					state<="0000";						--回到状态0
				end if;
				if (k7='0')and(key7='1') then
					state<="0011";						--进入状态3
				end if;
			end if;
			--state2										--状态2结束		
			--state3:									--状态3:重置密码中间态
			if state="0011" then
				int_temp:=(0,7,6,6,6,6,6,6);				显示“OP-----”
				state<="0100";							--进入状态4
				i<="000";
				password<="000000000000000000";
			end if;
			--state3										--状态3结束
			--state4:										--状态4:确认旧密码
			if state="0100" then
				if i="110" then
					i<="000";
					count<=count+"01";
					if password=password_code then --mi_ma_true
						counter<=counter+"01";
						count<="00";
						led_warning<="00000000";
						if counter="01" then			--连续两次输入正确旧密码
							counter<="00";
							int_temp:=(9,7,6,6,6,6,6,6);	--显示“NP------”
							state<="0110";				--进入状态6
						else 
							state<="0011";			--正确一次,回到状态3
						end if;
					else 
						if count="01" then 				--连续两次错误
							state<="1000";				--进入死锁状态
							int_temp:=(8,8,8,8,8,8,8,8);	--显示“EEEEEEEE”
						else 
							led_warning<="11111111";	--报警
							state<="0101";		--输入错误旧密码,进入状态5
							int_temp:=(6,6,8,8,8,8,6,6);	--显示“--EEEE--”
							i<="000";
						end if;
					end if;
				else 
					--判断key
					if (k0='0')and(key0='1') then 			--key0
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="000";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),0);
					end if;
					if (k1='0')and(key1='1') then 			--key1
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="001";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),1);
					end if;
					if (k2='0')and(key2='1')then 			--key2
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="010";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),2);
					end if;
					if (k3='0')and(key3='1') then			--key3
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="011";		int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),3);
					end if;
					if (k4='0')and(key4='1') then 			--key4
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="100";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),4);
					end if;
					if (k5='0')and(key5='1') then 			--key5
						i<=i+"001";
						password(17 downto 3)<=password(14 downto 0);
						password(2 downto 0)<="101";		int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),5);
					end if;
				end if;
			end if;
			--state4										--状态4结束
			--state5:	--状态5:确认旧密码中间态,按key6或key7回到状态3
			if state="0101" then 
				if (k6='0')and(key6='1') then			
					state<="0011";						--回到状态3
				end if;
				if (k7='0')and(key7='1') then
					state<="0011";						--回到状态3
				end if;
			end if;
			--state5										--状态5结束
			--state6:								--状态6:设置新密码中间态
			if state = "0110" then 
				state<="0111";							--进入状态7
				i<="000";
			end if;
			--state6										--状态6结束
			--state7:										--状态7:设置新密码
			if state="0111" then 
				if i="110" then
					state<="0000";				--成功设置新密码,回到状态0
					int_temp:=(7,7,6,6,6,6,6,6);			--显示“PP--------”
				else
					--判断key
					if (k0='0')and(key0='1') then			--key0
						i<=i+"001";
					password_code(17 downto 3)<=password_code(14 downto 0);
						password_code(2 downto 0)<="000";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),0);
					end if;
					if (k1='0')and(key1='1') then 			--key1
						i<=i+"001";
					password_code(17 downto 3)<=password_code(14 downto 0);
						password_code(2 downto 0)<="001";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),1);
					end if;
					if (k2='0')and(key2='1')then 			--key2
						i<=i+"001";
					password_code(17 downto 3)<=password_code(14 downto 0);
						password_code(2 downto 0)<="010";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),2);
					end if;
					if (k3='0')and(key3='1') then 			--key3
						i<=i+"001";
					password_code(17 downto 3)<=password_code(14 downto 0);
						password_code(2 downto 0)<="011";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),3);
					end if;
					if (k4='0')and(key4='1') then 			--key4
						i<=i+"001";
					password_code(17 downto 3)<=password_code(14 downto 0);
						password_code(2 downto 0)<="100";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),4);
					end if;
					if (k5='0')and(key5='1') then 			--key5
						i<=i+"001";
					password_code(17 downto 3)<=password_code(14 downto 0);
						password_code(2 downto 0)<="101";
	int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),5);
					end if;
				end if;
			end if;
			--state7										--状态7结束
		end if;
		--把中间结果赋值给数码管对应字符的数字数组
		smg_int<=int_temp;
	end process;
end architecture display;

6,引脚设置

根据题目要求,需要用到的板载资源有时钟信号clk:pin128(1khz);8个数码管:pin135,pin133,pin132,pin131,pin130,pin121,pin120,pin119;数码管的8段led灯:pin144,pin143,pin142,pin141,pin140,pin138,pin137,pin136;8个按键:K7:pin43,K6:pin42,K5:pin41,K4:pin39,K3:pin38,K2:pin37,K1:pin36,K0:pin35;8个报警的led灯:pin95,pin92,pin91,pin90,pin89,pin88,pin87,pin86。 

7,部分验证

1,初始显示:

 2,打开锁:

3,重置密码:

 4,输入新密码:

  • 33
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
基于VHDL设计用PGA实现一款简易电子密码锁QUARTUS工程源码+文档说明 library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_unsigned.all; use IEEE.std_logic_arith.all; entity time_counter is port( clk:in std_logic; --50M时钟输入 reset_n:in std_logic; --复位信号输入 password1_in:in std_logic_vector(3 downto 0); -- password2_in:in std_logic_vector(3 downto 0); -- password3_in:in std_logic_vector(3 downto 0); -- password4_in:in std_logic_vector(3 downto 0); -- ok_signal_counter_in:in std_logic_vector(2 downto 0); seg_duan:out std_logic_vector(7 downto 0); --数码管段信号输出 seg_wei:out std_logic_vector(7 downto 0) --数码管位信号输出 ); end time_counter; architecture time_counter_behave of time_counter is signal clk_1hz: std_logic; signal count: std_logic_vector(24 downto 0); signal clk_scan: std_logic; signal seg_select: std_logic_vector(2 downto 0); signal scan_count: std_logic_vector(13 downto 0); begin -- //**************************************************************************************************** -- // 模块名称:50M时钟分频至1HZ模块 -- // 功能描述: -- //**************************************************************************************************** process(clk,reset_n) begin if(reset_n = '0')then clk_1hz <= '0'; count <= "0000000000000000000000000"; elsif(clk'event and clk = '1')then--上升沿触发 if(count = "1011111010111100001000000")then-- count <= "0000000000000000000000000"; clk_1hz <= not clk_1hz; else count <= count + '1'; end if; end if; end process; -- //**************************************************************************************************** -- // 模块名称:数码管扫描时钟产生模块 -- // 功能描述: -- //************************************************************************************

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值