收藏 分销(赏)

吴超电子密码锁.docx

上传人:xrp****65 文档编号:6139607 上传时间:2024-11-28 格式:DOCX 页数:32 大小:460.83KB 下载积分:10 金币
下载 相关 举报
吴超电子密码锁.docx_第1页
第1页 / 共32页
吴超电子密码锁.docx_第2页
第2页 / 共32页


点击查看更多>>
资源描述
一、设计思想 本设计采用EDA技术和VHDL语言设计了一种按键输入密码并数码管回显,当输入正确密码时轰动绿灯亮、红灯熄灭表示开锁,而当输入错误密码时,红灯亮、绿灯熄灭表示关锁。根据系统设计要求,系统设计采用自顶向下的设计方法。 1.基本原理 在本实验中采用的是VHDL编程,通过文本编辑方式建立模块,通过原理图方式将生成的图形符号连接,然后再下载,进行硬件的仿真。密码锁要达到的功能为: 为达到密码锁的以上功能,可将电子密码锁分为以下几个模块:密码锁输入电路、密码锁控制电路、LED输出显示电路。 ⑴、密码输入:每按下一个数字键,就输入一个数值,并在显示器上显示出该数值。同时将先前输入的数据依次左移一个数字位置。程序下载后系统进入原始状态(原始密码000000),按下键8发光二极管8(绿灯)亮、法官二极管7(红灯)灭。当要重新设置密码时,直接输入自己想要设置的密码并按下键8,持续5S,数码管8由0变为1,此时7段数码管1、2、3、4、5、6对应显示新设置的密码。当要输入密码时,按下键7,先将密码清零,再随机输入一组6位密码,假如密码正确,发光二极管8(绿灯)立即亮;反之如不正确,等待5S,发光二极管7(红灯)亮并由蜂鸣器发出20S的报警信号。 ⑵、密码清除:按下清除键可清除前面所有的输入值,清除成为“0000”。 ⑶、密码更改:按下更改键可将目前数据设定为新的密码。 ⑷、密码上锁:按下上锁键可将密码锁上锁。 ⑸、密码解除:按下解除键首先检查输入的密码是否正确,密码正确即解锁。 2.设计框图 为达到密码锁的以上功能,可将电子密码锁分为以下几个模块:密码锁输入电路、密码锁控制电路、LED输出显示电路。和报警电路等四部分组成顶层设计采用原理图设计方式,系统的整体组装设计原理图如图1所示。 密 码 模 块 校 对 模 块 5S延时脉冲模块 红灯灭绿灯亮 绿灯灭红灯亮 20S报警 20延时脉冲模块 关锁 开锁 图1 系统整体组装设计原理图 二、设计步骤和调试过程 1、模块设计和相应模块代码 (1)分频 由于要产生5秒、20秒的计时信号,故对系统时钟clk_1k进行分频来得到1Hz的时钟clk。其模块及部分程序如下: 图2 分频模块 process(clk_1k) variable count:std_logic_vector(9 downto 0); begin if(clk_1k'event and clk_1k='1') then count:=count+1; end if; clk<=count(9); end process; 仿真波形如下: 图3 分频仿真图 (2) 设置密码 本模块主要是将设置的密码锁存到中间变量ram中去,同时控制灯的变化,由于这里的灯并不能作为最终的输出,所以这里先用led_r_temp1、led_r_temp2代替。其模块及部分程序如下: 图4 设置密码模块 process(enter1,set) begin if(enter1'event and enter1='1') then if(set='0') then ram<=datain; led_r_temp1<='1';led_g_temp1<='0'; else led_r_temp1<='0';led_g_temp1<='0'; end if; end if; end process; 仿真波形如下: 图5 设置密码仿真图 (3) 输入密码时第一个按键判断信号 本模块主要在密码输入下,当第一个按键按下时产生一个judge信号(高电平有效),其模块及部分程序如下: 图6 判断信号模块 process(set,clk_1k,datain) begin if(clk_1k'event and clk_1k='1') then if(set='0') then judge<='0'; elsif(set='1') then if((datain(0) or datain(1) or datain(2) or datain(3))='0') then judge<='0'; els if((datain(0) or datain(1) or datain(2) or datain(3))='1') then judge<='1'; else judge<='0'; end if; end if; end if; end process; 仿真波形如下: 图7 判断信号仿真图 从仿真波形上看,当按键按下后judge信号由0变为1,但是当按键重新弹回0时,judge信号又回到0,所以需对judge高电平信号进行锁存: 图8 锁存模块 signal temp:std_logic:='0'; begin process(judge) begin if(judge'event and judge='1') then temp<='1'; end if; end process; judge_load<=temp; 仿真波形如下: 图9 锁存模块仿真图 (4) 5秒计时信号 该模块是产生一个5秒计时的信号state,5秒期间为‘1’,当5秒时间过去后state变为‘0’,并作为报警信号。其模块及部分程序如下: 图10 5秒计时信号模块 signal count_5:std_logic_vector(2 downto 0); signal state1:std_logic; begin process(clk) begin if(clk'event and clk='1') then if(judge_load='1') then ——第一个按键按下 if(count_5="101") then count_5<="101"; else count_5<=count_5+1; end if; end if; end if; end process; process(count_5) begin case count_5 is when "000"=>state1<='1'; when "001"=>state1<='1'; when "010"=>state1<='1'; when "011"=>state1<='1'; when "100"=>state1<='1'; when others=>state1<='0'; end case; end process; state<=state1 and set; 确保在set置为‘1’而第一个按键没有按下时,state为高电平,即刚进入输入密码状态而第一个按键没有按下时state为高电平。 仿真波形如下: 图11 5秒计时模块仿真图 根据仿真波形可以看出,set为‘0’时设置密码,当set为‘1’时,在judge_load(按键识别信号)为‘0’的情况下,state为‘1’,保证下面将要提及的报警模块不会工作,而只有当judge_load为‘1’,且5秒过后state变为‘0’时才有可能发出报警信号(密码输错的情况下)。 (5) 开锁信号的产生 本模块主要是对输入的密码进行判断,一旦密码输入正确,产生一个开锁信号unlock(高电平有效)。其模块与部分程序如下: 图12 开锁信号模块 process(enter2) begin if(enter2'event and enter2='1') then ——输入密码确认 if(set='1') then if(state='1') then ——5秒期间 if(datain=ram) then unlock<='1'; ——开锁信号 else unlock<='0'; end if; end if; end if; end if; end process; 输入正确密码仿真波形: 图13 开锁信号仿真图 输入错误密码仿真波形: 图14 开锁信号仿真图 (6) 报警模块 本模块主要是在5秒限制时间结束时,如果还没有开锁,就产生报警信号。即长达20秒的声光信号。这里的报警信号指示灯用led_r_temp3表示,扬声器用speaker_temp2表示。其模块及部分程序如下: 图15 报警模块 signal count_20:std_logic_vector(4 downto 0); signal temp:std_logic; begin process(clk) begin if(clk'event and clk='1') then if(set='0')then temp<='0'; elsif(set='1') then if(state='0') then if(count_20="10100") then count_20<="10100";temp<='0'; else count_20<=count_20+1;temp<='1'; speaker_temp2<=clk_1k; end if; end if; end if; end if; end process; led_r_temp3<=temp and clk and (not unlock); speaker_temp2<=temp and clk_1k and (not unlock); 仿真波形如下: 图16 报警模块仿真图 可见当state由‘1’变为‘0’且没有unlock信号时,输出20秒的声光信号以示报警。 (7) 开锁信号控制指示灯变化模块 本模块是实现当开锁信号产生时,相应的指示灯由红亮绿灭变成红灭绿亮。这里同样先用led_r_temp2、led_g_temp2代表红灯和绿灯。其模块及部分程序如下: 图17 指示灯变化模块 process(clk_1k,state) begin if(clk_1k'event and clk_1k='1') then if(state='1') then if(unlock='1') then led_r_temp2<='0';led_g_temp2<='1'; else led_r_temp2<='1';led_g_temp2<='0'; end if; end if; end if; end process; 仿真波形如下: 图18 指示灯变化仿真图 (8) 开锁信号产生2秒提示音模块 当开锁信号产生时,扬声器发出两秒的提示音。这里用speaker_temp2代替。其模块及部分程序如下: 图19 2秒提示音模块 signal count_2:std_logic_vector(1 downto 0); signal temp:std_logic; begin process(clk) begin if(clk'event and clk='1') then ——1秒Hz if(unlock='1') then if(count_2="10") then count_2<="10";temp<='0'; else count_2<=count_2+1;temp<='1'; end if; end if; end if; end process; speaker_temp1<=clk_1k and temp; 仿真波形如下: 图20 2秒提示音仿真图 (9) 指示灯综合输出模块 本模块主要是将led_r_temp1、led_g_temp1、led_r_temp2、led_g_temp2经过条件判断选择输出,设置密码(set=‘0’)时输出led_r_temp1、led_g_temp1,输入密码(set=‘1’)时输出led_r_temp2、led_g_temp2。其模块及部分程序如下: 图21 指示灯综合输出模块 if(set='0') then led_r<=led_r_temp1; led_g<=led_g_temp1; else led_r<=led_r_temp2; led_g<=led_g_temp2; 仿真波形如下: 图22 指示灯综合输出仿真图 (10)扬声器综合输出模块 本模块主要是将报警音speaker_temp1和开锁提示音speaker_temp2经过条件选择判断输出,set=‘0’时,输出为‘0’;当set=‘1’且unlock=‘0’时输出speaker_temp1,当unlock=1时,输出speaker_temp2。其模块及部分程序如下: 图23 扬声器综合输出模块 if(set='0') then speaker<='0'; elsif(unlock='1') then speaker<=speaker_temp1; else speaker<=speaker_temp2; end if; 仿真波形如下: 图24 扬声器综合输出仿真图 2、仿真及仿真结果分析 将程序下载Cyclone系列芯片中,同时在EDA试验箱上进行硬件验证。本文提出的电子密码锁由于采用VHDL语言设计,用一片FPGA实现,因而体积小,功耗低,稍加修改就可以改变密码的位数和输入密码的次数,具有较好的应用前景。但由于结构还比较简单,有待进一步完善。 输入错误密码: 图25 输入错误密码仿真图 输入正确密码: 图26 输入正确密码仿真图 3、实验调试结果 密码锁的控制电路是整个电路的控制中心,主要完成对数字按键输入和功能按键输入的响应控制。 ⑴、数字按键输入的响应控制 如果按下数字键,第一个数字会从显示器的最右端开始显示,此后每新按下一个数字时,显示器上的数字必须左移一位,一边将新的数字显示出来。 假如要更改输入的数字,可以按倒退按键来清除前一个输入的数字,或者按清除键清除所有输入的数字,再重新输入四位数。 由于这里设计的是一个四位的电子密码锁,所以当输入的数字键超过四个时,电路不予理会,而且不再显示第四个以后的数字。 ⑵、功能按键输入响应控制 清除键:清除所有的输入数字,即做归零动作。 上锁键:按下此键时可将密码锁的门上锁(上锁前必须先设定一个四位的电子密码)。 电子密码锁的整合和验证。 三、结论及心得体会 本课程设计主要是基于VHDL文本输入法设计电子密码锁,随着社会物质财富的日益增长,安全防盗已成为全社会关注的问题。基于EDA技术设计的电子密码锁,以其价格便宜、安全可靠、使用方便,受到了人们的普遍关注。而以现场可编程逻辑器件(FPGA)为设计载体,以硬件描述语言(VHDE)为主要表达方式,以QuartusⅡ开发软件和GW48EDA开发系统为设计工具设计的电子密码锁,由于其能够实现数码输入、数码清除、密码解除、密码更改、密码上锁和密码解除等功能,因此,能够满足社会对安全防盗的要求。 通过本次课程设计我收获颇多,刚开始拿到题目后,没有一个明确的方案,查了很多资料,大部分都用到了矩阵键盘,并且比较复杂,后来根据实验箱设计了该方案。这就要求我们要根据现实的条件去设计适合自己硬件的程序。 在这次课程设计中,最头疼的问题就是各个模块仿真正确后,最后的顶层文件却并没有达到预期的效果,很是令人灰心,并无从查找。后来我试着把每一个模块逐渐添加到顶层中,每添加一个就仿真一下,并留意中间信号量的变化,根据波形去调试相应的模块,最终得到了想要的波形。当然期间遇到了很多的困难,通过向老师同学请教,并积极思考,最终这些难题都得到了解决,这也让我懂得了团队的重要性。 参考资料: [1] 潘松著.EDA技术实用教程(第二版). 北京:科学出版社,2005. [2] 康华光主编.电子技术基础 模拟部分. 北京:高教出版社,2006. [3] 阎石主编.数字电子技术基础. 北京:高教出版社,2003. [4] 谭会生、瞿遂春.EDA技术综合应用实例与分析.西安电子科技大学出版社,2004 [5] 高有堂.EDA技术及应用实践.清华大学出版社,2006 [6]候伯亨著.VHDL硬件描述语言与数字逻辑电路设计[M].西安:西安电子科技大学出版社,2009. [7]张昌凡著.可编程逻辑器件及VHDL设计技术[M].广州:华南理工大学出版社,2001. [8]曹昕燕、周凤臣等.EDA技术实验与课程设计.清华大学出版社,2006 源程序: 【1】 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity fenpin is port(clk_1k:in std_logic; clk:out std_logic); end; architecture rtl of fenpin is begin process(clk_1k) variable count:std_logic_vector(9 downto 0); begin if(clk_1k'event and clk_1k='1') then count:=count+1; end if; clk<=count(9); end process; end rtl; 【2】library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity code_set is port(set,enter1:in std_logic; datain:in std_logic_vector(3 downto 0); ram:out std_logic_vector(3 downto 0); led_r_temp1,led_g_temp1:out std_logic); end; architecture rtl of code_set is begin process(enter1,set) begin if(enter1'event and enter1='1') then if(set='0') then ram<=datain; led_r_temp1<='1';led_g_temp1<='0'; else led_r_temp1<='0';led_g_temp1<='0'; end if; end if; end process; end; 【3】library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity judge is port(clk_1k,set:in std_logic; datain:in std_logic_vector(3 downto 0); judge:out std_logic); end; architecture rtl of judge is begin process(set,clk_1k,datain) begin if(clk_1k'event and clk_1k='1') then if(set='0') then judge<='0'; elsif(set='1') then if((datain(0) or datain(1) or datain(2) or datain(3))='0') then judge<='0'; elsif((datain(0) or datain(1) or datain(2) or datain(3))='1') then judge<='1'; else judge<='0'; end if; end if; end if; end process; end; 【4】library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity judge_load is port(judge:in std_logic; judge_load:out std_logic); end; architecture rtl of judge_load is signal temp:std_logic:='0'; begin process(judge) begin if(judge'event and judge='1') then temp<='1'; end if; end process; judge_load<=temp; end; 【5】library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity time_5 is port(clk,judge_load,set:in std_logic; state:out std_logic); end; architecture rtl of time_5 is signal count_5:std_logic_vector(2 downto 0); signal state1:std_logic; begin process(clk) begin if(clk'event and clk='1') then if(judge_load='1') then if(count_5="101") then count_5<="101"; else count_5<=count_5+1; end if; end if; end if; end process; process(count_5) begin case count_5 is when "000"=>state1<='1'; when "001"=>state1<='1'; when "010"=>state1<='1'; when "011"=>state1<='1'; when "100"=>state1<='1'; when others=>state1<='0'; end case; end process; state<=state1 and set; end; 【6】library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity unlock is port(set,enter2,state:in std_logic; datain,ram:in std_logic_vector(3 downto 0); unlock:out std_logic); end; architecture rtl of unlock is begin process(enter2) begin if(enter2'event and enter2='1') then if(set='1') then if(state='1') then if(datain=ram) then unlock<='1'; else unlock<='0'; end if; end if; end if; end if; end process; end rtl; 【7】library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity unlock_led is port(state,unlock,clk_1k:in std_logic; led_r_temp2,led_g_temp2:out std_logic); end; architecture rtl of unlock_led is begin process(clk_1k,state) begin if(clk_1k'event and clk_1k='1') then if(state='1') then if(unlock='1') then led_r_temp2<='0';led_g_temp2<='1'; else led_r_temp2<='1';led_g_temp2<='0'; end if; end if; end if; end process; end; 【8】library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity unlock_sound is port(clk,unlock,clk_1k:in std_logic; speaker_temp1:out std_logic); end; architecture rtl of unlock_sound is signal count_2:std_logic_vector(1 downto 0); signal temp:std_logic; begin process(clk) begin if(clk'event and clk='1') then if(unlock='1') then if(count_2="10") then count_2<="10";temp<='0'; else count_2<=count_2+1;temp<='1'; end if; end if; end if; end process; speaker_temp1<=clk_1k and temp; end; 【9】library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity alert is port(state,clk,clk_1k,set,unlock:in std_logic; led_r_temp3,speaker_temp2:out std_logic); end; architecture rtl of alert is signal count_20:std_logic_vector(4 downto 0); signal temp:std_logic; begin process(clk) begin if(clk'event and clk='1') then if(set='0')then temp<='0'; elsif(set='1') then if(state='0') then if(count_20="10100") then count_20<="10100";temp<='0'; else count_20<=count_20+1;temp<='1'; end if; end if; end if; end if; end process; led_r_temp3<=temp and clk and (not unlock); speaker_temp2<=temp and clk_1k and (not unlock); end; 【10】library ieee; use ieee.std_logic_1164.all; entity led is port(led_r_temp1,led_r_temp2,led_g_temp1,led_g_temp2,set:in std_logic; led_r,led_g:out std_logic); end; architecture rtl of led is begin process(set) begin if(set='0') then led_r<=led_r_temp1; led_g<=led_g_temp1; else led_r<=led_r_temp2; led_g<=led_g_temp2; end if; end process; end; 【11】library ieee; use ieee.std_logic_1164.all; entity speaker is port(unlock,speaker_temp1,speaker_temp2,set:in std_logic; speaker:out std_logic); end; architecture rtl of speaker is begin process(unlock) begin if(set='0') then speaker<='0'; elsif(unlock='1') then speaker<=speaker_temp1; else speaker<=speaker_temp2; end if; end process; end;
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 环境建筑 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服