电 子 密 码 锁.docx
《电 子 密 码 锁.docx》由会员分享,可在线阅读,更多相关《电 子 密 码 锁.docx(34页珍藏版)》请在冰点文库上搜索。
电子密码锁
《基于FPGA的数字系统设计》项目设计文档
项目名称:
电子密码锁
姓名:
院系:
应用技术学院
专业:
电子信息工程
学号:
指导教师:
完成时间:
2012年6月20日
基于FPGA的数字系统设计项目成绩评价表
设计题目
电子密码锁
设计要求
设计一个简单的数字电子密码锁,密码为4位。
功能:
1、密码输入:
按下一个数字键,其对应的数字就会显示在数码管上,同时将先前输入的所有数字向左移一位。
2、密码清除:
当按下清除键时,清除前面输入的所有值,并显示为“0000”。
3、密码修改:
将当前输入设为新的密码;
4、密码激活:
按下此键,将前面输入的四位数字设为密码,密码锁上锁。
5、电锁解除:
按下电锁解除键,系统会将输入与密码进行检查核对,如果正确,电锁开启,否则将打不开锁。
设计过程
附后
项目设计成绩评价
评价项目
指标
满分
评分
工作量、工作态度和出勤率
按期圆满的完成了规定的任务,难易程度和工作量符合教学要求,工作努力,遵守纪律,出勤率高,工作作风严谨,善于与他人合作。
30
课程设计质量
项目设计系统架构合理,设计过程简练正确,分析问题思路清晰,结构严谨,文理通顺,撰写规范,图表完备正确。
40
创新
工作中有创新意识,对前人工作有一些改进或有一定应用价值。
15
答辩
能正确回答指导教师所提出的问题。
15
综合成绩等级
指导教师:
年月日
项目设计文档主要
目录
1项目名称、内容与要求1
1.1设计内容1
1.2具体要求1
2系统整体架构(ArchitectureDescription)2
2.1设计思路2
2.2系统原理(包含:
框图等阐述)与设计说明等内容2
2.3创新点与原创性内容3
3系统设计(含VHDL或原理图输入设计)3
3.1HDL代码3
3.2系统整体电路图(或RTL级电路图)10
4.1VHDL的工作环境10
4.2程序的编译和波形的仿真:
11
5FPGA实现(FPGAImplementation)16
5.1引脚的设置16
5.2全程编译及配置文件下载17
5.3硬件验证18
6总结(Closing)18
参考书目(Reference)19
附录(Appendix)19
1项目名称、内容与要求
1.1设计内容
本实验中说要求设计的电子密码锁密码为4位,由三大部分组成,每一部分又包含了若干子电路,将各电路组合起来,就构成了一个整体。
1、矩阵键盘接口电路设计由于硬件电路没有提供该矩阵键盘,用开关代替该部分电路。
2、密码锁的控制电路设计
3、输出七段显示电路的设计
1.2具体要求
数码输入:
按下一个数字键,其对应的数字就会显示在数码管上,同时将先前输入的所有数字向左移一位。
数码清除:
当按下清除键时,清除前面输入的所有值,并显示为“0000”。
密码激活:
按下此键,将前面输入的四位数字设为密码,密码锁上锁。
密码更改:
将输入的值作为新的密码。
电锁解除:
按下电锁解除键,系统会将输入与密码进行检查核对,如果正确,电锁开启,否则将打不开锁。
2系统整体架构(ArchitectureDescription)
2.1设计思路
本设计所需的硬件主要有:
矩阵键盘、七段译码器、发光二极管(用来模拟电子锁,红灯亮为加锁,绿灯亮为解锁)、EPM7128LC84-6适配器。
注:
实际使用的为EDA试验箱,没有矩阵键盘,没有七段译码器,有发光二极管,仅有红色,芯片为EPM7128LC84-15。
2.2系统原理(包含:
框图等阐述)与设计说明等内容
程序包括六个逻辑元件,分别为:
数字按键输入、控制功能按键输入、时钟分频器、处理核心、输出处理、七段译码器。
框图如下:
2.3创新点与原创性内容
本实验设计简单,只需将矩阵键盘接口电路、密码锁的控制电路、输出七段显示电路三大部分合起来就是一个密码锁了。
3系统设计(含VHDL或原理图输入设计)
3.1HDL代码
数字按键输入逻辑-numinput
说明:
读取数字键0~9。
高电平表示按键未按下,低电平表示按键按下。
按照0~9顺序读取,只能输出1位数字。
输出4位二进制代码,“0000”~“1001”表示0~9,用“1010”表示无输入。
VHDL代码:
libraryieee;
useieee.std_logic_1164.all;
entitynuminputis
port(
numin:
INstd_logic_vector(9downto0);
numstate,clk:
INstd_logic;
numout:
OUTstd_logic_vector(3downto0));
endnuminput;
architectureoneofnuminputis
signalstate:
std_logic;
signalmem:
std_logic_vector(9downto0);
begin
process(clk)
begin
ifclk'eventandclk='1'then
ifstate/=numstatethen
ifmem/=numinthen
casenuminis
when"1111111110"=>numout<="0000";
when"1111111101"=>numout<="0001";
when"1111111011"=>numout<="0010";
when"1111110111"=>numout<="0011";
when"1111101111"=>numout<="0100";
when"1111011111"=>numout<="0101";
when"1110111111"=>numout<="0110";
when"1101111111"=>numout<="0111";
when"1011111111"=>numout<="1000";
when"0111111111"=>numout<="1001";
whenothers=>numout<="1010";
endcase;
state<=numstate;
elsenumout<="1010";
endif;
mem<=numin;
endif;
endif;
endprocess;
endone;
控制功能按键输入-coninput
说明:
读取控制功能按键-清除按键,修改密码,锁定,解锁。
高电平表示按键未按下,低电平表示按键按下。
按照“清除按键,修改密码,锁定,解锁”顺序读取,只能输出一位控制信号。
输出3位二进制代码,“001”~“100”表示“清除按键,修改密码,锁定解锁”,用“000”表示无输入。
VHDL代码:
libraryieee;
useieee.std_logic_1164.all;
entityconinputis
port(
conin:
INstd_logic_vector(3downto0);
constate,clk:
INstd_logic;
conout:
OUTstd_logic_vector(2downto0));
endconinput;
architectureoneofconinputis
signalstate:
std_logic;
signalmem:
std_logic_vector(3downto0);
begin
process(clk)
begin
ifclk'eventandclk='1'then
ifconstate/=statethen
state<=constate;
ifmem/=coninthen
caseconinis
when"1110"=>conout<="001";
when"1101"=>conout<="010";
when"1011"=>conout<="011";
when"0111"=>conout<="100";
whenothers=>conout<="000";
endcase;
mem<=conin;
elseconout<="000";
endif;
endif;
endif;
endprocess;
endone;
时钟分频器-fenpin
说明:
十分频器。
VHDL代码:
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_unsigned.all;
entityfenpinis
port(
clkin:
instd_logic;
clkout:
outstd_logic);
endfenpin;
architectureoneoffenpinis
signalfenp:
integerrange0to9;
signalclk:
std_logic;
begin
process(clkin)
begin
ifclkin'eventandclkin='1'then
fenp<=fenp+1;
iffenp=9then
ifclk='1'thenclk<='0';
elseclk<='1';
endif;
endif;
--numberclear
--changecode
--lockstart
--unlock
clkout<=clk;
endif;
endprocess;
endone;
处理核心-core
说明:
处理核心,根据输入(数字输入以及控制功能输入)改变存储器状态、数码管显示以及LED灯显示。
VHDL代码:
libraryieee;
useieee.std_logic_1164.all;
entitycoreis
port(
numin:
instd_logic_vector(3downto0);
conin:
instd_logic_vector(2downto0);
clk:
instd_logic;
dataa,datab,datac,datad:
outstd_logic_vector(3downto0);
ledr,ledg,numout,conout:
outstd_logic);
endentity;
architectureoneofcoreis
typelockstateis(unlock,locked);
signalnuma,numb,numc,numd,codea,codeb,codec,coded:
std_logic_vector(3downto0);
signalnumstate,constate:
std_logic;
signallocksta:
lockstate;
begin
process(clk,numin,conin)
begin
ifclk'eventandclk='1'then
ifnumin/="1000"then
numd<=numc;
numc<=numb;
numb<=numa;
numa<=numin;
endif;
ifconin/="000"then
ifconin="001"then
numa<="0000";
numb<="0000";
numc<="0000";
numd<="0000";
elsifconin="010"then
iflocksta/=lockedthen
codea<=numa;
codeb<=numb;
codec<=numc;
coded<=numd;
endif;
elsifconin="001"then
iflocksta/=lockedthen
numa<="0000";
numb<="0000";
numc<="0000";
numd<="0000";
locksta<=locked;
endif;
elsifconin="100"then
iflocksta=lockedthen
ifnuma=codeaandnumb=codebthen
ifnumc=codecandnumd=codedthen
locksta<=unlock;
endif;
endif;
endif;
endif;
endif;
iflocksta=lockedthen
ledr<='1';
ledg<='0';
else
ledr<='0';
ledg<='1';
endif;
dataa<=numa;
datab<=numb;
datac<=numc;
datad<=numd;
ifnumstate='1'thennumstate<='0';
elsenumstate<='1';
endif;
ifconstate='1'thenconstate<='0';
elseconstate<='1';
endif;
numout<=numstate;
conout<=constate;
endif;
endprocess;
endone;
输出处理-allout
说明:
根据core提供的数码管数据刷新显示数字,需要配合七段译码器。
VHDL代码:
libraryieee;
useieee.std_logic_1164.all;
entityalloutis
port(
dataa,datab,datac,datad:
instd_logic_vector(3downto0);
clk:
instd_logic;
dataout:
outstd_logic_vector(3downto0);
outsel:
outstd_logic_vector(1downto0));
endallout;
architectureoneofalloutis
signaltimer:
std_logic_vector(1downto0);
begin
process(clk)
begin
ifclk'eventandclk='1'then
iftimer="00"then
dataout<=dataa;
outsel<="00";
timer<="01";
elsiftimer="01"then
dataout<=datab;
outsel<="01";
timer<="10";
elsiftimer="10"then
dataout<=datac;
outsel<="10";
timer<="11";
else
dataout<=datad;
outsel<="11";
timer<="00";
endif;
endif;
endprocess;
endone;
七段译码器-dataout
说明:
把4位2进制数翻译成数码管代码。
VHDL代码:
libraryieee;
useieee.std_logic_1164.all;
entitydataoutis
port(datain:
INstd_logic_vector(3downto0);
dataout:
OUTstd_logic_vector(6downto0));
enddataout;
architectureoneofdataoutis
begin
process(datain)
begin
casedatainis
when"0000"=>dataout<="1111110";
when"0001"=>dataout<="0110000";
when"0010"=>dataout<="1101101";
when"0011"=>dataout<="1111001";
when"0100"=>dataout<="0110011";
when"0101"=>dataout<="1011011";
when"0110"=>dataout<="1011111";
when"0111"=>dataout<="1110000";
when"1000"=>dataout<="1111111";
when"1001"=>dataout<="1111011";
whenothers=>dataout<="0000000";
endcase;
endprocess;
endone;
3.2系统整体电路图(或RTL级电路图)
密码锁顶层文件原理图
4系统仿真(SimulationWaveform)
4.1VHDL的工作环境
在菜单栏中选择File→New→NewQuartusproject
芯片选择:
4.2程序的编译和波形的仿真:
芯片的选择一定要与自己的芯片一致。
创建完项目后,新建一个VHDL语言的文件,创建方法:
File→NEW→VHDLFile。
然后将自己的程序代码写入。
程序输入完之后就进行语法编译,语法编译是检查语法的正确性,若有错误程序自动指出。
在命令栏中选择startAnalysis&synthesis按钮,编译成功后如下:
接下来就是生成元件模块,方法为File→create/update→createsymbolFilesforcorrentFile
生成元件模块后就是连接元件原理图
然后设为顶层文件
最后全程编译。
接下来就是波形的仿真,先建一个波形文件,方法:
File→New→VectorWaveFormFile,然后把程序中的输入输出端导入到这个波形文件中。
导入完后,就设置输入端的初始值。
最后就是波形的编译。
编译完后,其结果如下:
5FPGA实现(FPGAImplementation)
5.1引脚的设置
方法是选择菜单栏中的Assignments→Pins
然后逐个的进行设置
5.2全程编译及配置文件下载
在菜单栏中点击StartComiplation进行全程编译
最后就是程序下载,在连接好下载线的前提下,点击菜单栏中的Programmer图标
点击Start,程序就会下载到FPGA中。
5.3硬件验证
程序下载到FPGA中,接好相应的接线脚之后。
按下“修改密码”键,开始输入密码(假设输入为1234),再按下“锁定”键锁定密码锁。
此时红灯亮、绿灯熄灭。
在输入一个错误的密码(假设输入为1212),然后按下“解锁”键,此时密码锁未解锁,依然是红灯亮、绿灯熄灭。
再输入正确的密码(1234),按下“解锁”键,此时红灯熄灭、绿灯亮,密码锁被解锁。
6总结(Closing)
课程设计刚开始,和搭档拿着选定的题目觉得很简单。
脑海中马上就出现了几个设计思路,但在设计的几天里才发现设计并不是那么简单。
毕竟课程设计不同于实验课,电路图都要自己设计,而且要着眼整个系统。
原来的思路一个个被排除掉。
静下心来,仔细分析题目,再加上指导老师的说明与提示,心中才有了谱。
将整个系统根据不同的功能化分成模块,再分别进行设计,逐个攻破,最后再将其整合。
在设计过程中,既有课堂上学习过的方法,又有在图书资料上查阅的方法,即学即用。
最后仿真调试阶段,哪怕一个小小的错误也会使结果出不来。
只好一条线一条线地查,。
结果终于出来了,又发现有的地方还应改进。
经过反复的修改仿真,最终得到了满意的效果。
通过这次课程设计,使我受益颇多。
既巩固了课堂上学到的理论知识,又掌握了常用EDA软件的使用方法。
在此基础上学习了数字系统设计的基本思想和方法,学会了科学地分析实际问题,通过查资料、分析资料及请教老师和同学等多种途径,独立解决问题。
同时,也培养了我认真严谨的工作作风。
参考书目(Reference)
[1]《高频电路原理与分析》(第四版)西安电子科技大学出版社,2006
[2]《高频电子线路》北京:
高等教育出版社,1998.12
[3]张健华.数字电子技术.北京:
机械工业出版社,1999
附录(Appendix)
电子密码锁程序清单:
libraryieee;
useieee.std_logic_1164.all;
entitynuminputis
port(
numin:
INstd_logic_vector(9downto0);
numstate,clk:
INstd_logic;
numout:
OUTstd_logic_vector(3downto0));
endnuminput;
architectureoneofnuminputis
signalstate:
std_logic;
signalmem:
std_logic_vector(9downto0);
begin
process(clk)
begin
ifclk'eventandclk='1'then
ifstate/=numstatethen
ifmem/=numinthen
casenuminis
when"1111111110"=>numout<="0000";
when"1111111101"=>numout<="0001";
when"1111111011"=>numout<="0010";
when"1111110111"=>numout<="0011";
when"1111101111"=>numout<="0100";
when"1111011111"=>numout<="0101";
when"1110111111"=>numout<="0110";
when"1101111111"=>numout<="0111";
when"1011111111"=>numout<="1000";
when"0111111111"=>numout<="1001";
whenothers=>numout<="1010";
endcase;
state<=numstate;
elsenumout<="1010";
endif;
mem<=numin;
endif;
endif;
endprocess;
endone;
libraryieee;
useieee.std_logic_1164.all;
entityconinputis
port(
conin:
INstd_logic_vec