基于VHDL的电子密码锁Word文档格式.docx
《基于VHDL的电子密码锁Word文档格式.docx》由会员分享,可在线阅读,更多相关《基于VHDL的电子密码锁Word文档格式.docx(16页珍藏版)》请在冰豆网上搜索。
(4)密码修改:
正确输入两次密码后,按下KEY4一次,将当前输入设为新的密码;
(5)清屏:
KEY5按下一次,外部输入置0;
(6)恢复出厂设置:
连续按下KEY5三次后恢复出厂密码“1234”;
三、程序设计
1由于程序稍显庞大,故采取分层次设计的方法,顶层采用画图法设计,底层采用VHDL语言进行设计。
顶层电路图如图1所示
图1
Xiao_dou模块为按键消抖模块,main为密码锁控制模块,yima为数码管显示译码模块。
Set_shuma端口为数码管段选端口,用于选择数码管。
Led端口为led灯端口,key1,key2,key3,key4,key5为五个按键接口,show_shuma端口为数码管位选选择端口。
2底层设计
a)由于按键为机械按键,故按下一次会产生多次脉冲,当产生第一次下降沿的时候延时一段时间后,再继续检测是否有下降沿产生。
主体程序如下:
b)数码管译码显示进程:
将0至9的4位矢量型数字转化为相应8位数码管显示数字
c)电子密码锁控制电路:
i.数码管位置选择进程:
当数码管位置选择时钟到来时,数码管显示位左移一位,利用人眼的视觉余辉效果,产生4位数码管同时点亮的效果。
ii.数码管位置选择时钟信号发生进程:
将系统时钟分频,产生500HZ占空比为1:
1的方波,用于控制数码管位置选择。
iii.整型密码转化为矢量型密码,位选选择进程:
利用CASE语句将不同位置的密码值显示在相应的数码管上。
iv.外部输入密码变更进程:
当KEY1按下时外部输入密码加1,并在数码管上显示出来,数字从1至9循环;
后半部分为当清屏信号到来时,外部输入密码置0。
v.按键位置选择进程:
KEY2按下时数码管位置选择端左移一位,后半部分为当清屏信号到来时,位置置0。
vi.密码设置,重置进程:
当密码连续正确两次后,按下KEY4键,将现在显示的密码置为新密码。
后半部分为恢复出厂设置密码。
vii.清屏,恢复出厂设置进程:
当KEY5按下时产生清屏信号,当KEY5连续按下三次后产生恢复出厂设置信号。
后半部分为清屏与恢复出厂设置信号的清除程序,用于清除产生的清屏与恢复出厂设置信号。
viii.密码比较进程:
比较当前输入密码与部密码的值,如相同则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.控制电路源程序
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
useieee.std_logic_arith.all;
entitymainis
port(key1,key2,key3,key4,key5:
instd_logic;
clk:
num_shuma:
outstd_logic_vector(3downto0);
set_shuma:
outstd_logic_vector(3downto0);
led:
outstd_logic:
='
0'
;
sel:
outstd_logic);
endentitymain;
architecturelockofmainis
signaltemp_1,temp_led,temp_shuma,reset_all,reset_mima
temp_reset,key1_clear,key2_clear,key3_clear,key4_clear:
std_logic;
signalbit_one_in:
integerrange0to9:
=1;
--insideofnumber
signalbit_two_in:
=2;
signalbit_three_in:
=3;
signalbit_four_in:
=4;
signalshow_shuma_location:
std_logic_vector(1downto0);
--locationofshuma
signalbit_one_out,bit_two_out,bit_three_out,bit_four_out:
integerrange0to9;
--outsideofnumber
signallocation:
--insidelocationofnumber
signalnumber_led:
integerrange0to5000000;
signalnumber_shuma:
integerrange0to50000;
signalnumber_error:
integerrange0to3;
signaltwice:
integerrange0to2;
signalclear_num:
begin
show_location_set:
process(temp_shuma)--thelocationofshuma_guan
begin
if(temp_shuma'
eventandtemp_shuma='
1'
)then
show_shuma_location<
=show_shuma_location+1;
endif;
endprocess;
show_shuma:
process(clk)--thenumberofshuma_guan
caseshow_shuma_locationis
when"
00"
=>
num_shuma<
=conv_std_logic_vector(bit_one_out,4);
set_shuma<
="
1110"
01"
=conv_std_logic_vector(bit_two_out,4);
1101"
10"
=conv_std_logic_vector(bit_three_out,4);
1011"
11"
=conv_std_logic_vector(bit_four_out,4);
0111"
endcase;
if(reset_all='
num_shuma<
0000"
--xianshizhi0
endif;
shuma_200:
process(clk)--usetodelaylocationset
if(clk'
eventandclk='
if(number_shuma=50000)then
number_shuma<
=0;
temp_shuma<
=nottemp_shuma;
elsenumber_shuma<
=number_shuma+1;
endprocessshuma_200;
add_outside:
process(clk,reset_all)--key1addnum
if(key1'
eventandkey1='
key1_clear<
caselocationis
when"
bit_one_out<
=bit_one_out+1;
if(bit_one_out=9)thenbit_one_out<
endif;
bit_two_out<
=bit_two_out+1;
if(bit_two_out=9)thenbit_two_out<
bit_three_out<
=bit_three_out+1;
if(bit_three_out=9)thenbit_three_out<
bit_four_out<
=bit_four_out+1;
if(bit_four_out=9)thenbit_four_out<
endcase;
if(clear_num=0)thenkey1_clear<
if(reset_all='
bit_one_out<
bit_two_out<
bit_three_out<
bit_four_out<
endprocessadd_outside;
location_set:
process(clk,reset_all)--key2location_set
if(key2'
eventandkey2='
--led<
key2_clear<
location<
=location+1;
if(clear_num=0)thenkey2_clear<
set_mima:
process(clk,clear_num)--key4setmima
if(twice=2)then
if(key4'
eventandkey4='
key4_clear<
bit_one_in<
=bit_one_out;
bit_two_in<
=bit_two_out;
bit_three_in<
=bit_three_out;
bit_four_in<
=bit_four_out;
if(clear_num=0)thenkey4_clear<
if(clear_num=3)then
bit_one_in<
bit_two_in<
bit_three_in<
bit_four_in<
reset:
process(clk,temp_reset,key1_clear,key2_clear,key3_clear,key4_clear)--key5clearandreset
if(key5'
eventandkey5='
reset_all<
if(clear_num=3)then
clear_num<
elseclear_num<
=clear_num+1;
if(location="
andbit_one_out=0andbit_two_out=0andbit_three_out=0andbit_four_out=0)then
reset_all<
if(temp_reset='
if(key1_clear='
orkey2_clear='
orkey3_clear='
orkey4_clear='
)then--ifotherkeyclearnum
clear_num<
compare:
process(clk,reset_all)--key3compare
if(key3'
eventandkey3='
key3_clear<
if(bit_one_in=bit_one_outandbit_two_in=bit_two_outand
bit_three_in=bit_three_outandbit_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;
led<
if(clear_num=0)thenkey3_clear<
andbit_one_out=0andbit_two_out=0andbit_three_out=0andbit_four_out=0)then--cleartongxin
temp_reset<
if(temp_1='
led<
=temp_led;
endprocesscompare;
one_second:
process(clk)
if(number_led=5000000)then
number_led<
temp_led<
=nottemp_led;
elsenumber_led<
=number_led+1;
endprocessone_second;
endarchitecturelock;
2.消抖源程序
entityxiao_douis
port(clk:
instd_logic;
din:
dout:
outstd_logic);
endentityxiao_dou;
architectureaofxiao_douis
signalnum:
integerrange0to1000000;
signaltemp_10,temp,temp1:
std_logic;
if(din='
if(num=1000000)thennum<
=num;
elsenum<
=num+1;
if(num=999999)thendout<
elsedout<
elsenum<
endarchitecturea;
3.译码源程序
entityyimais
port(yuanma:
instd_logic_vector(3downto0);
shuma:
outstd_logic_vector(7downto0));
endentityyima;
architectureaofyimais
process(yuanma)is
caseyuanmais
when"
shuma<
00000011"
0001"
10011111"
0010"
00100101"
0011"
00001101"
0100"
10011001"
0101"
01001001"
0110"
01000001"
00011111"
1000"
00000001"
1001"
00011001"
whenothers=>
endarchitecturea;