1、(4)密码修改:正确输入两次密码后,按下KEY4一次,将当前输入设为新的密码;(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、端口。2底层设计a)由于按键为机械按键,故按下一次会产生多次脉冲,当产生第一次下降沿的时候延时一段时间后,再继续检测是否有下降沿产生。主体程序如下:b)数码管译码显示进程:将0至9的4位矢量型数字转化为相应8位数码管显示数字c)电子密码锁控制电路:i.数码管位置选择进程:当数码管位置选择时钟到来时,数码管显示位左移一位,利用人眼的视觉余辉效果,产生4位数码管同时点亮的效果。ii.数码管位置选择时钟信号发生进程:将系统时钟分频,产生500HZ占空比为1:1的方波,用于控制数码管位置选择。iii.整型密码转化为矢量型密码,位选选择进程:利用CASE语句将不同位置的密码值显示在相应的数码管上。iv.
3、外部输入密码变更进程:当KEY1按下时外部输入密码加1,并在数码管上显示出来,数字从1至9循环;后半部分为当清屏信号到来时,外部输入密码置0。v.按键位置选择进程:KEY2按下时数码管位置选择端左移一位,后半部分为当清屏信号到来时,位置置0。vi.密码设置,重置进程:当密码连续正确两次后,按下KEY4键,将现在显示的密码置为新密码。后半部分为恢复出厂设置密码。vii.清屏,恢复出厂设置进程:当KEY5按下时产生清屏信号,当KEY5连续按下三次后产生恢复出厂设置信号。后半部分为清屏与恢复出厂设置信号的清除程序,用于清除产生的清屏与恢复出厂设置信号。viii.密码比较进程:比较当前输入密码与部密码
4、的值,如相同则LED长亮,如不同则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.控制电路
5、源程序library 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 : 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 std_lo
6、gic);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 :=2;signal bit_three_in :=3;signal bit_four_in :=
7、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 : -inside location of numbersignal number_led :integer range 0 to 5000000;signal number_shuma :integer range
8、 0 to 50000;signal number_error :integer range 0 to 3;signal twice :integer range 0 to 2;signal clear_num:begin show_location_set:process(temp_shuma) -the location of shuma_guanbeginif(temp_shumaevent and temp_shuma=1)then show_shuma_location num_shuma=conv_std_logic_vector(bit_one_out,4); set_shuma
9、=111001=conv_std_logic_vector(bit_two_out,4);110110=conv_std_logic_vector(bit_three_out,4);101111=conv_std_logic_vector(bit_four_out,4);0111 end case; if(reset_all= num_shuma0000 -xian shi zhi 0 end if;shuma_200:process(clk) -use to delay location setif(clkevent and clk= if(number_shuma=50000)then n
10、umber_shuma=0; temp_shuma=not temp_shuma; else number_shuma=number_shuma+1;end process shuma_200; add_outside:process(clk,reset_all) -key1 add numif(key1event and key1=key1_clearcase location is when bit_one_out=bit_one_out+1; if(bit_one_out=9)then bit_one_out end if;bit_two_out=bit_two_out+1; if(bi
11、t_two_out=9)then bit_two_outbit_three_out=bit_three_out+1; if(bit_three_out=9)then bit_three_outbit_four_out=bit_four_out+1; if(bit_four_out=9)then bit_four_outend case;if(clear_num=0)then key1_clearif(reset_all= bit_one_out bit_two_out bit_three_out bit_four_outend process add_outside;location_set:
12、process(clk,reset_all) -key2 location_set if(key2event and key2= -led key2_clear location=location+1;if(clear_num=0)then key2_clearset_mima:process(clk,clear_num) -key4 set mimaif(twice=2)then if(key4event and key4= key4_clear bit_one_in=bit_one_out; bit_two_in=bit_two_out; bit_three_in=bit_three_ou
13、t; bit_four_in=bit_four_out;if(clear_num=0)then key4_clearif(clear_num=3)then bit_one_in bit_two_in bit_three_in bit_four_inreset:process(clk,temp_reset,key1_clear,key2_clear,key3_clear,key4_clear) -key5 clear and resetif(key5event and key5= reset_all if(clear_num=3)then clear_num else clear_num=cle
14、ar_num+1;if(location= and bit_one_out=0 and bit_two_out=0 and bit_three_out=0 and bit_four_out=0)then reset_allif(temp_reset=if(key1_clear= or key2_clear= or key3_clear= or key4_clear=)then -if other key clear num clear_numcompare:process(clk,reset_all) -key3 compareif(key3event and key3= key3_clear
15、 if(bit_one_in=bit_one_out and bit_two_in=bit_two_out and bit_three_in=bit_three_out and bit_four_in=bit_four_out)then led temp_1 temp_reset number_error if(twice=2)then twice=twice; else=twice+1; else twice if(number_error=3)then number_error temp_1 else =number_error+1; ledif(clear_num=0)then key3
16、_clear 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_resetif(temp_1= led=temp_led; end process compare ;one_second:process(clk) if(number_led=5000000)then number_led temp_led=not temp_led; else number_led=number_led+1;end process one_second;end
17、architecture lock;2.消抖源程序entity xiao_dou is port(clk: in std_logic; din: dout: out std_logic);end entity xiao_dou;architecture a of xiao_dou issignal num :integer range 0 to 1000000;signal temp_10,temp,temp1 :std_logic; if(din= if(num=1000000) then num=num; else num=num+1; if(num=999999)then dout else dout else numend architecture a;3.译码源程序entity yima isport(yuanma:in std_logic_vector(3 downto 0); shuma: out std_logic_vector(7 downto 0);end entity yima;architecture a of yima isprocess(yuanma)iscase yuanma is whenshumaend architecture a ;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1