1、基于VHDL的电子密码锁华南师范大学课程设计实验报告课程名称:可编程数字系统设计课程设计题目:电子密码锁姓 名:程硕学院:物理与电信工程学院专 业:理综一班年 级:2011学 号:20112600104一、设计原理用VHDL设计电子密码锁方案:作为通用电子密码锁,主要由 3 个部分组成:数字密码按键输入电路、密码锁控制电路和密码锁显示电路,作为电子密码锁的输入电路。可供选择的方案有数字机械式键盘和触摸式数字键盘等多种。(1)密码锁输入电路包括时序产生电路、键盘检测电路、键盘消抖电路等几个小的功能电路。(2)密码锁控制电路包括按键数据存储电路,恢复出厂密码、两次正确输入后可修改密码、密码核对,多
2、次错误报警,清屏等几个小的功能电路。(3)八段数码管显示电路主要将待显示数据的BCD码转换成数码器的八段显示驱动编码。二、系统分析本次课程设计成功地设计了一个简单的数字电子密码锁,密码为 4 位。将电子密码锁分为以下几个模块:按键消抖模块、密码锁逻辑控制模块和密码锁显示模块,实现了以下功能: (1)密码输入:KEY1按下一次,数码管上显示数字加一,相应位置输入密码加一(2)位置选择:KEY2按下一次,数码管选择位左移一位(3)密码确认:KEY3按下一次,比较外部输入密码与原密码,正确LED长亮,错误LED变暗,同时显示密码置0; (4)密码修改:正确输入两次密码后,按下KEY4一次,将当前输入
3、设为新的密码; (5)清屏:KEY5按下一次,外部输入置0;(6)恢复出厂设置:连续按下KEY5三次后恢复出厂密码“1234”;三、程序设计1 由于程序稍显庞大,故采取分层次设计的方法,顶层采用画图法设计,底层采用VHDL语言进行设计。顶层电路图如图1所示 图1Xiao_dou模块为按键消抖模块,main为密码锁控制模块,yima为数码管显示译码模块。Set_shuma端口为数码管段选端口,用于选择数码管。Led端口为led灯端口,key1,key2,key3,key4,key5为五个按键接口,show_shuma端口为数码管位选选择端口。2 底层设计a) 由于按键为机械按键,故按下一次会产生
4、多次脉冲,当产生第一次下降沿的时候延时一段时间后,再继续检测是否有下降沿产生。主体程序如下:b) 数码管译码显示进程:将0至9的4位矢量型数字转化为相应8位数码管显示数字c) 电子密码锁控制电路:i. 数码管位置选择进程:当数码管位置选择时钟到来时,数码管显示位左移一位,利用人眼的视觉余辉效果,产生4位数码管同时点亮的效果。ii. 数码管位置选择时钟信号发生进程:将系统时钟分频,产生500HZ占空比为1:1的方波,用于控制数码管位置选择。iii. 整型密码转化为矢量型密码,位选选择进程:利用CASE语句将不同位置的密码值显示在相应的数码管上。iv. 外部输入密码变更进程:当KEY1按下时外部输
5、入密码加1,并在数码管上显示出来,数字从1至9循环;后半部分为当清屏信号到来时,外部输入密码置0。v. 按键位置选择进程:KEY2按下时数码管位置选择端左移一位,后半部分为当清屏信号到来时,位置置0。vi. 密码设置,重置进程:当密码连续正确两次后,按下KEY4键,将现在显示的密码置为新密码。后半部分为恢复出厂设置密码。vii. 清屏,恢复出厂设置进程:当KEY5按下时产生清屏信号,当KEY5连续按下三次后产生恢复出厂设置信号。后半部分为清屏与恢复出厂设置信号的清除程序,用于清除产生的清屏与恢复出厂设置信号。viii. 密码比较进程:比较当前输入密码与内部密码的值,如相同则LED长亮,如不同则
6、LED暗,如连续3次输入错误密码则LED闪烁报警。ix. LED闪烁脉冲发生进程:用于产生LED闪烁所需方波脉冲四、仿真/实验结果1. 密码正确:2. 密码错误:3. 清屏:五、结论或分析各项实验结果均成功,能够很好的实现,密码比较,设置密码,清屏,恢复出厂设置等各项功能。六、使用说明1. S5:KEY1按下后外部输入密码加1;2. S4:KEY2按下后输入选择位置左移一位;3. S3:KEY3按下后比较输入密码与内部密码;4. S4:KEY4连续输入两次正确密码后按下,将当前显示密码设为新密码;5. S5:KEY5按下一次清屏,连续按下三次后恢复出厂设置。七、源程序1. 控制电路源程序lib
7、rary ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;use ieee.std_logic_arith.all;entity main is port(key1,key2,key3,key4,key5 :in std_logic; clk :in std_logic; num_shuma :out std_logic_vector(3 downto 0); set_shuma : out std_logic_vector(3 downto 0); led :out std_logic:=0; sel :out
8、 std_logic);end entity main;architecture lock of main issignal temp_1,temp_led, temp_shuma,reset_all,reset_mima ,temp_reset,key1_clear,key2_clear,key3_clear,key4_clear: std_logic;signal bit_one_in :integer range 0 to 9:=1; -inside of numbersignal bit_two_in :integer range 0 to 9:=2;signal bit_three_
9、in :integer range 0 to 9:=3;signal bit_four_in :integer range 0 to 9:=4;signal show_shuma_location:std_logic_vector(1 downto 0); -location of shumasignal bit_one_out,bit_two_out,bit_three_out,bit_four_out :integer range 0 to 9; -outside of numbersignal location :std_logic_vector(1 downto 0); -inside
10、 location of numbersignal number_led :integer range 0 to 5000000;signal number_shuma :integer range 0 to 50000;signal number_error :integer range 0 to 3;signal twice :integer range 0 to 2;signal clear_num:integer range 0 to 3;begin show_location_set:process(temp_shuma) -the location of shuma_guanbeg
11、inif(temp_shumaevent and temp_shuma=1)then show_shuma_location num_shuma=conv_std_logic_vector(bit_one_out,4); set_shuma num_shuma=conv_std_logic_vector(bit_two_out,4); set_shuma num_shuma=conv_std_logic_vector(bit_three_out,4); set_shuma num_shuma=conv_std_logic_vector(bit_four_out,4); set_shuma=01
12、11; end case; if(reset_all=1)then num_shuma=0000; -xian shi zhi 0 end if; end process;shuma_200:process(clk) -use to delay location setbeginif(clkevent and clk=1)then if(number_shuma=50000)then number_shuma=0; temp_shuma=not temp_shuma; else number_shuma=number_shuma+1; end if;end if;end process shu
13、ma_200; add_outside:process(clk,reset_all) -key1 add numbeginif(key1event and key1=0)thenkey1_clearbit_one_out=bit_one_out+1; if(bit_one_out=9)then bit_one_outbit_two_out=bit_two_out+1; if(bit_two_out=9)then bit_two_outbit_three_out=bit_three_out+1; if(bit_three_out=9)then bit_three_outbit_four_out=
14、bit_four_out+1; if(bit_four_out=9)then bit_four_out=0; end if;end case;end if;if(clear_num=0)then key1_clear=0;end if;if(reset_all=1)then bit_one_out=0; bit_two_out=0; bit_three_out=0; bit_four_out=0;end if;end process add_outside;location_set:process(clk,reset_all) -key2 location_setbegin if(key2ev
15、ent and key2=0)then -led=0; key2_clear=1; location=location+1; end if; if(reset_all=1)then location=00; end if; if(clear_num=0)then key2_clear=0;end if;end process;set_mima:process(clk,clear_num) -key4 set mimabeginif(twice=2)then if(key4event and key4=0)then -led=0; key4_clear=1; bit_one_in=bit_one
16、_out; bit_two_in=bit_two_out; bit_three_in=bit_three_out; bit_four_in=bit_four_out; end if;end if;if(clear_num=0)then key4_clear=0;end if;if(clear_num=3)then bit_one_in=1; bit_two_in=2; bit_three_in=3; bit_four_in=4;end if;end process;reset:process(clk,temp_reset,key1_clear,key2_clear,key3_clear,key
17、4_clear) -key5 clear and resetbeginif(key5event and key5=0)then -led=0; reset_all=1; if(clear_num=3)then clear_num=0; else clear_num=clear_num+1; end if;end if;if(location=00 and bit_one_out=0 and bit_two_out=0 and bit_three_out=0 and bit_four_out=0)then reset_all=0;end if;if(temp_reset=1)then reset
18、_all=1;end if;if(key1_clear=1 or key2_clear=1 or key3_clear=1 or key4_clear=1)then -if other key clear num clear_num=0;end if;end process;compare:process(clk,reset_all) -key3 comparebeginif(key3event and key3=0)then key3_clear=1; if(bit_one_in=bit_one_out and bit_two_in=bit_two_out and bit_three_in=
19、bit_three_out and bit_four_in=bit_four_out)then led=1; temp_1=0; temp_reset=1; number_error=0; if(twice=2)then twice=twice; else twice=twice+1; end if; else twice=0; if(number_error=3)then number_error=1; temp_1=1; else number_error=number_error+1; led=0; temp_1=0; end if; end if;end if;if(clear_num
20、=0)then key3_clear=0;end if;if(location=00 and bit_one_out=0 and bit_two_out=0 and bit_three_out=0 and bit_four_out=0)then -clear tong xin temp_reset=0;end if; if(temp_1=1)then led=temp_led;end if; end process compare ; one_second:process(clk)beginif(clkevent and clk=1)then if(number_led=5000000)the
21、n number_led=0; temp_led=not temp_led; else number_led=number_led+1; end if;end if;end process one_second; end architecture lock;2. 消抖源程序library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity xiao_dou is port(clk: in std_logic; din: in std_logic; dout: out std_logic);end ent
22、ity xiao_dou;architecture a of xiao_dou issignal num :integer range 0 to 1000000;signal temp_10,temp,temp1 :std_logic;beginprocess(clk)beginif(clkevent and clk=1)then if(din=0)then if(num=1000000) then num=num; else num=num+1; end if; if(num=999999)then dout=1; else dout=0; end if; else numshumashumashumashumashumashumashumashumashumashumashuma=00000001;end case;end process;end architecture a ;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1