当前位置:   ASP > 正文

EDA课设(数字系统设计)--数字密码锁_eda课程设计代码

eda课程设计代码

目录

1,注意

2,可能遇到的问题

3,题目描述

4,实现前期准备

5,实现代码

6,引脚设置

7,部分验证


1,注意

该博客是根据自己的课设报告写的,所以大家不要抄袭,仅用作给大家提供实现思路以及一些经验,希望大家根据我写的东西,理解关键的代码,较为熟练的掌握VHDL语言的语法,规则以及流程,学会如何自己实现所有的功能;

2,可能遇到的问题

1,对于不会安装quartusII 9.0的同学可以参考前一篇博客:EDA课设(数字系统设计)--quartusII 9.0安装及altera usb-blaster驱动识别失败解决

2,如果大家不知道如何使用板载的led灯,8段数码管,clk信号,可以参考前一篇博客里面下载的资源,在目录:数字系统设计实验\实验,下的EDA-I便携式数字系统实验与设计平台说明书.pdf文件;

3,题目描述

完成一简易密码锁的设计,实现6位密码的设定与开锁。

1)使用6个按键进行密码输入,K0-K5,分别代表数字键0-5,用右边6个数码管显示;

2)密码初始值为555555;开锁方式:xxxxxx(x代表密码数字,位数等于6位);上电后,初始显示:"PP------";输入一个数字就在最右数码管显示,前面的数字均左移一个数码管。输入正确显示“--OPEN--”,输入错误显示“--EEEE--”。

3)设计一个重新输入按钮K6,在输入未全或者错误(没达到3次)时,恢复输入,按下后显示“PP------”

4)工作时钟1khz;连续3次输错密码则锁死,只有重启电路;连续2次错误点亮警报灯。

5) 用按键k7设置密码,设定方式:旧密码,输入两次,输入前显示为“OP------”,正确后提示输入新密码:“NP------”,连续输入2次。以上出错均显示“--EEEE--”,可按K7恢复设置,或者K6。

4,实现前期准备

根据题目的描述,我们可以将大体的设计分成两个主要部分,第一个部分是开锁,第二个部分是重置密码;

想明白自己要实现的功能之后,下面就是设计程序的流程了,因为VHDL内部绝大部分是并行执行的,但是我习惯了写顺序的代码,所以在编写程序时犯了许多的逻辑错误,比如对一些信号赋值时,本来想要的是一个一个改变,但是由于是并行执行,它总是全部一起赋值,导致数码管只能显示一样的内容,不能得到我想要的结果,后面我自己想了很久,也参考了一些别人的文章,经过自己的反复测试,发现状态的转换可以实现我想要的功能,所以我最后决定用类似状态转换的方式来实现我想要的效果,实现这个题目的要求并非几句话就可以说清楚的,过程是非常痛苦的,但是大家也不要因为一些困难就选择摆烂,应该要了解VHDL的结构,尽量理解书上的知识以及参考代码,达到能自己实现所有功能的目的,这应该也是课设的目的;

下面这个是我做这个题目的自己做思维导图,这个图是我实现密码锁所有之后才做的,算是后期的东西,放在这里主要是便于大家理解下面的代码,了解思路;

5,实现代码

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.std_logic_arith.all;
  4. use ieee.std_logic_unsigned.all;
  5. entity seg is
  6. port(
  7. clk : in std_logic;
  8. key0,key1,key2,key3,key4,key5,key6,key7 : in std_logic;
  9. led_warning : out std_logic_vector(7 downto 0);
  10. smg_led : out std_logic_vector(7 downto 0);--数码管8段led
  11. smg_code : out std_logic_vector(7 downto 0)--8个数码管
  12. );
  13. end entity seg;
  14. architecture display of seg is
  15. type smg_led_array is array (7 downto 0) of std_logic_vector(7 downto 0);
  16. signal smg_leds : smg_led_array;
  17. signal smg_index : integer range 0 to 7 :=0;
  18. type smg_code_array is array (0 to 9) of std_logic_vector(7 downto 0);
  19. signal smg_codes : smg_code_array; --存放字符
  20. signal password : std_logic_vector(17 downto 0);
  21. signal password_code:std_logic_vector(17 downto 0) :="101101101101101101";
  22. type smg_int_array is array (7 downto 0) of integer;
  23. signal smg_int : smg_int_array :=(7,7,6,6,6,6,6,6); --存放字符对应的数字
  24. signal counter,count : std_logic_vector(1 downto 0) := "00";
  25. signal i : std_logic_vector(2 downto 0) := "000";
  26. signal state : std_logic_vector(3 downto 0) := "0000";
  27. signal k0,k1,k2,k3,k4,k5,k6,k7 : std_logic;
  28. begin
  29. smg_codes(0)<="00111111";--0 --字符对应的数字数组
  30. smg_codes(1)<="00000110";--1
  31. smg_codes(2)<="01011011";--2
  32. smg_codes(3)<="01001111";--3
  33. smg_codes(4)<="01100110";--4
  34. smg_codes(5)<="01101101";--5
  35. smg_codes(6)<="01000000";--'-'
  36. smg_codes(7)<="01110011";--'P'
  37. smg_codes(8)<="01111001";--'E'
  38. smg_codes(9)<="00110111";--'N'
  39. --动态扫描数码管的进程
  40. smg_saomiao:process(clk)
  41. begin
  42. if clk'event and clk='1' then
  43. smg_led<=smg_leds(smg_index);--shu_ma_guan_shu_chu
  44. smg_code <= not(conv_std_logic_vector(2**smg_index,8));
  45. if smg_index=7 then smg_index<=0;
  46. else smg_index<=smg_index+1;
  47. end if;
  48. end if;
  49. end process;
  50. --数码管显示进程
  51. smg_show:process(clk)
  52. begin
  53. smg_leds(7) <= smg_codes(smg_int(7));
  54. smg_leds(6) <= smg_codes(smg_int(6));
  55. smg_leds(5) <= smg_codes(smg_int(5));
  56. smg_leds(4) <= smg_codes(smg_int(4));
  57. smg_leds(3) <= smg_codes(smg_int(3));
  58. smg_leds(2) <= smg_codes(smg_int(2));
  59. smg_leds(1) <= smg_codes(smg_int(1));
  60. smg_leds(0) <= smg_codes(smg_int(0));
  61. end process;
  62. --处理按键的进程
  63. handle_key:process(clk,state,key0,key1,key2,key3,key4,key5,key6,key7)
  64. variable int_temp : smg_int_array ;
  65. begin
  66. if clk'event and clk='1' then --统一时钟信号 k0<=key0;k1<=key1;k2<=key2;k3<=key3;k4<=key4;k5<=key5;k6<=key6;k7<=key7;
  67. --state0: --状态0:初始化
  68. if state="0000" then
  69. int_temp:=(7,7,6,6,6,6,6,6);
  70. state<="0001"; --进入状态1
  71. i<="000";
  72. password<="000000000000000000";
  73. end if;
  74. --state0 --状态0结束
  75. --state1: --状态1:确定密码
  76. if state="0001" then
  77. if i="110" then
  78. i<="000";
  79. if password=password_code then --密码正确
  80. led_warning<="00000000";
  81. counter<="00";
  82. state<="0010"; --进入状态2
  83. int_temp:=(6,6,0,7,8,9,6,6); --显示“--OPEN--”
  84. else --mi_ma_false;
  85. counter<=counter+"01";
  86. --password<="000000000000000000";
  87. if counter="01" then
  88. led_warning<="11111111"; --报警
  89. state<="0010"; --进入状态2
  90. int_temp:=(6,6,8,8,8,8,6,6); --显示“--EEEE--”
  91. elsif counter="10" then --密码错误三次
  92. state<="1000"; --进入死锁状态
  93. int_temp:=(8,8,8,8,8,8,8,8); --显示“EEEEEEEE”
  94. else
  95. led_warning<="00000000";
  96. state<="0010"; --进入状态2
  97. int_temp:=(6,6,8,8,8,8,6,6); --显示“--EEEE--”
  98. end if;
  99. end if;
  100. else
  101. --判断是哪个key
  102. if (k0='0')and(key0='1') then --key0
  103. i<=i+"001";
  104. password(17 downto 3)<=password(14 downto 0);
  105. password(2 downto 0)<="000";
  106. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),0);
  107. end if;
  108. if (k1='0')and(key1='1') then --key1
  109. i<=i+"001";
  110. password(17 downto 3)<=password(14 downto 0);
  111. password(2 downto 0)<="001";
  112. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),1);
  113. end if;
  114. if (k2='0')and(key2='1')then --key2
  115. i<=i+"001";
  116. password(17 downto 3)<=password(14 downto 0);
  117. password(2 downto 0)<="010";
  118. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),2);
  119. end if;
  120. if (k3='0')and(key3='1') then --key3
  121. i<=i+"001";
  122. password(17 downto 3)<=password(14 downto 0);
  123. password(2 downto 0)<="011";
  124. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),3);
  125. end if;
  126. if (k4='0')and(key4='1') then --key4
  127. i<=i+"001";
  128. password(17 downto 3)<=password(14 downto 0);
  129. password(2 downto 0)<="100";
  130. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),4);
  131. end if;
  132. if (k5='0')and(key5='1') then --key5
  133. i<=i+"001";
  134. password(17 downto 3)<=password(14 downto 0);
  135. password(2 downto 0)<="101";
  136. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),5);
  137. end if;
  138. if (k6='0')and(key6='1') then --key6
  139. state<="0000"; --回到状态0
  140. counter<="00"; --计数器清0
  141. end if;
  142. if (k7='0')and(key7='1') then
  143. state<="0011"; --进入状态3
  144. end if;
  145. end if;
  146. end if;
  147. --state1 --判断按键状态结束
  148. --state2: --恢复状态,按key6重新输入,按key7进入重置密码
  149. if state="0010" then
  150. password<="000000000000000000";
  151. if (k6='0')and(key6='1') then
  152. state<="0000"; --回到状态0
  153. end if;
  154. if (k7='0')and(key7='1') then
  155. state<="0011"; --进入状态3
  156. end if;
  157. end if;
  158. --state2 --状态2结束
  159. --state3: --状态3:重置密码中间态
  160. if state="0011" then
  161. int_temp:=(0,7,6,6,6,6,6,6); 显示“OP-----”
  162. state<="0100"; --进入状态4
  163. i<="000";
  164. password<="000000000000000000";
  165. end if;
  166. --state3 --状态3结束
  167. --state4: --状态4:确认旧密码
  168. if state="0100" then
  169. if i="110" then
  170. i<="000";
  171. count<=count+"01";
  172. if password=password_code then --mi_ma_true
  173. counter<=counter+"01";
  174. count<="00";
  175. led_warning<="00000000";
  176. if counter="01" then --连续两次输入正确旧密码
  177. counter<="00";
  178. int_temp:=(9,7,6,6,6,6,6,6); --显示“NP------”
  179. state<="0110"; --进入状态6
  180. else
  181. state<="0011"; --正确一次,回到状态3
  182. end if;
  183. else
  184. if count="01" then --连续两次错误
  185. state<="1000"; --进入死锁状态
  186. int_temp:=(8,8,8,8,8,8,8,8); --显示“EEEEEEEE”
  187. else
  188. led_warning<="11111111"; --报警
  189. state<="0101"; --输入错误旧密码,进入状态5
  190. int_temp:=(6,6,8,8,8,8,6,6); --显示“--EEEE--”
  191. i<="000";
  192. end if;
  193. end if;
  194. else
  195. --判断key
  196. if (k0='0')and(key0='1') then --key0
  197. i<=i+"001";
  198. password(17 downto 3)<=password(14 downto 0);
  199. password(2 downto 0)<="000";
  200. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),0);
  201. end if;
  202. if (k1='0')and(key1='1') then --key1
  203. i<=i+"001";
  204. password(17 downto 3)<=password(14 downto 0);
  205. password(2 downto 0)<="001";
  206. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),1);
  207. end if;
  208. if (k2='0')and(key2='1')then --key2
  209. i<=i+"001";
  210. password(17 downto 3)<=password(14 downto 0);
  211. password(2 downto 0)<="010";
  212. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),2);
  213. end if;
  214. if (k3='0')and(key3='1') then --key3
  215. i<=i+"001";
  216. password(17 downto 3)<=password(14 downto 0);
  217. password(2 downto 0)<="011"; int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),3);
  218. end if;
  219. if (k4='0')and(key4='1') then --key4
  220. i<=i+"001";
  221. password(17 downto 3)<=password(14 downto 0);
  222. password(2 downto 0)<="100";
  223. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),4);
  224. end if;
  225. if (k5='0')and(key5='1') then --key5
  226. i<=i+"001";
  227. password(17 downto 3)<=password(14 downto 0);
  228. password(2 downto 0)<="101"; int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),5);
  229. end if;
  230. end if;
  231. end if;
  232. --state4 --状态4结束
  233. --state5: --状态5:确认旧密码中间态,按key6或key7回到状态3
  234. if state="0101" then
  235. if (k6='0')and(key6='1') then
  236. state<="0011"; --回到状态3
  237. end if;
  238. if (k7='0')and(key7='1') then
  239. state<="0011"; --回到状态3
  240. end if;
  241. end if;
  242. --state5 --状态5结束
  243. --state6: --状态6:设置新密码中间态
  244. if state = "0110" then
  245. state<="0111"; --进入状态7
  246. i<="000";
  247. end if;
  248. --state6 --状态6结束
  249. --state7: --状态7:设置新密码
  250. if state="0111" then
  251. if i="110" then
  252. state<="0000"; --成功设置新密码,回到状态0
  253. int_temp:=(7,7,6,6,6,6,6,6); --显示“PP--------”
  254. else
  255. --判断key
  256. if (k0='0')and(key0='1') then --key0
  257. i<=i+"001";
  258. password_code(17 downto 3)<=password_code(14 downto 0);
  259. password_code(2 downto 0)<="000";
  260. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),0);
  261. end if;
  262. if (k1='0')and(key1='1') then --key1
  263. i<=i+"001";
  264. password_code(17 downto 3)<=password_code(14 downto 0);
  265. password_code(2 downto 0)<="001";
  266. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),1);
  267. end if;
  268. if (k2='0')and(key2='1')then --key2
  269. i<=i+"001";
  270. password_code(17 downto 3)<=password_code(14 downto 0);
  271. password_code(2 downto 0)<="010";
  272. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),2);
  273. end if;
  274. if (k3='0')and(key3='1') then --key3
  275. i<=i+"001";
  276. password_code(17 downto 3)<=password_code(14 downto 0);
  277. password_code(2 downto 0)<="011";
  278. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),3);
  279. end if;
  280. if (k4='0')and(key4='1') then --key4
  281. i<=i+"001";
  282. password_code(17 downto 3)<=password_code(14 downto 0);
  283. password_code(2 downto 0)<="100";
  284. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),4);
  285. end if;
  286. if (k5='0')and(key5='1') then --key5
  287. i<=i+"001";
  288. password_code(17 downto 3)<=password_code(14 downto 0);
  289. password_code(2 downto 0)<="101";
  290. int_temp:=(int_temp(6),int_temp(5),int_temp(4),int_temp(3),int_temp(2),int_temp(1),int_temp(0),5);
  291. end if;
  292. end if;
  293. end if;
  294. --state7 --状态7结束
  295. end if;
  296. --把中间结果赋值给数码管对应字符的数字数组
  297. smg_int<=int_temp;
  298. end process;
  299. end architecture display;

6,引脚设置

根据题目要求,需要用到的板载资源有时钟信号clk:pin128(1khz);8个数码管:pin135,pin133,pin132,pin131,pin130,pin121,pin120,pin119;数码管的8段led灯:pin144,pin143,pin142,pin141,pin140,pin138,pin137,pin136;8个按键:K7:pin43,K6:pin42,K5:pin41,K4:pin39,K3:pin38,K2:pin37,K1:pin36,K0:pin35;8个报警的led灯:pin95,pin92,pin91,pin90,pin89,pin88,pin87,pin86。 

7,部分验证

1,初始显示:

 2,打开锁:

3,重置密码:

 4,输入新密码:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/ASP/detail/36742
推荐阅读
相关标签
  

闽ICP备14008679号