找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2333|回复: 0
收起左侧

基于FPGA的多功能电子钟设计与实现

[复制链接]
ID:357868 发表于 2018-6-24 16:46 | 显示全部楼层 |阅读模式
功能要求与技术指标
1.电子时钟。要求用24时制显示。分屏显示“时、分”和“分、秒”,即4个数码管不能同时显示“时、分、秒”,但可以只显示“时、分”,或只显示“分、秒”,通过按键来切换这两种显示方式。用数码管的小数点“.”代替时、分、秒的分隔符“:”。可设置时间。设置时间时,当前设置的“时”/“分”,相应的数码管应闪烁。
2.秒表(计时器)。秒表精度为0.01秒,计时范围0~99.99秒,用4个数码管显示,两个显示秒,两个显示百分秒,有暂停/继续、重置(清零)按钮。
3.定时器。可以实现0~9999秒定时。设置一定时值,当计时到达设定值时输出LED闪烁。有设置、暂停/继续、清零定时按钮。

  1. 1.管脚分配
  2. #Scnu_pins.tcl
  3. set_global_assignment -name RESERVE_ALL_UNUSED_PINS "AS INPUT TRI-STATED"
  4. set_global_assignment -name ENABLE_INIT_DONE_OUTPUT OFF
  5. set_location_assignment PIN_17 -to clk                                             

  6. set_location_assignment PIN_71 -to led

  7. set_location_assignment PIN_65 -to seg7com\[0\]
  8. set_location_assignment PIN_67 -to seg7com\[1\]
  9. set_location_assignment PIN_69 -to seg7com\[2\]
  10. set_location_assignment PIN_70 -to seg7com\[3\]

  11. set_location_assignment PIN_53 -to seg7data\[0\]  
  12. set_location_assignment PIN_55 -to seg7data\[1\]
  13. set_location_assignment PIN_57 -to seg7data\[2\]
  14. set_location_assignment PIN_58 -to seg7data\[3\]
  15. set_location_assignment PIN_59 -to seg7data\[4\]
  16. set_location_assignment PIN_60 -to seg7data\[5\]
  17. set_location_assignment PIN_63 -to seg7data\[6\]
  18. set_location_assignment PIN_64 -to seg7data\[7\]


  19. set_location_assignment PIN_88 -to key1
  20. set_location_assignment PIN_89 -to key2
  21. set_location_assignment PIN_90 -to key3
  22. set_location_assignment PIN_91 -to key4
  23. set_location_assignment PIN_72 -to key5
  24. --set_location_assignment PIN_64 -to seg7dp








  25. 2.顶层top.vhd
  26. library ieee;
  27. use ieee.std_logic_1164.all;
  28. use ieee.std_logic_unsigned.all;

  29. entity top is
  30. port(key1,key2,key3,key4,key5,clk:in std_logic;
  31.         led:out std_logic;
  32.         seg7data:out std_logic_vector(7 downto 0);
  33.         seg7com:out std_logic_vector(3 downto 0));
  34. end entity top;

  35. architecture example of top is
  36. --调用声明
  37. component fandou is
  38. port(clk,fin:in std_logic;
  39.         fout:out std_logic);
  40. end component fandou;

  41. component div_hz is
  42. port(clk:in std_logic;
  43.         clk_out:out std_logic);
  44. end component div_hz;

  45. component div_10hz is
  46. port(clk:in std_logic;
  47.         clk_out:out std_logic);
  48. end component div_10hz;

  49. component div_100hz is
  50. port(clk:in std_logic;
  51.         clk_out:out std_logic);
  52. end component div_100hz;

  53. component div_khz is
  54. port(clk:in std_logic;
  55.         clk_out:out std_logic);
  56. end component div_khz;

  57. component clock is
  58. port(clk,set,plus,clr:in std_logic;
  59.         set_data1,set_data2,set_data3,set_data4:in std_logic_vector(3 downto 0);
  60.         clock_data1,clock_data2,clock_data3,clock_data4,clock_data5,clock_data6:out std_logic_vector(3 downto 0));
  61. end component clock;

  62. component watch is
  63. port(clk,plus,clr:in std_logic;
  64.         watch_data1,watch_data2,watch_data3,watch_data4:out std_logic_vector(3 downto 0));
  65. end component watch;

  66. component timer is
  67. port(clk,mov,plus,clr:in std_logic;
  68.         set_data1,set_data2,set_data3,set_data4:in std_logic_vector(3 downto 0);
  69.         timer_data1,timer_data2,timer_data3,timer_data4:out std_logic_vector(3 downto 0));
  70. end component timer;

  71. component setting is
  72. port(set,mov,plus:in std_logic;
  73.         set_data1,set_data2,set_data3,set_data4:out std_logic_vector(3 downto 0));
  74. end component setting;

  75. component shining is
  76. port(clk,set,mov:in std_logic;
  77.         com:in std_logic_vector(3 downto 0);
  78.         seg7com:out std_logic_vector(3 downto 0));
  79. end component shining;

  80. component displayer is
  81. port(clk:in std_logic;
  82.         data1,data2,data3,data4:in std_logic_vector(3 downto 0);
  83.         seg7com:out std_logic_vector(3 downto 0);
  84.         seg7data:out std_logic_vector(7 downto 0));
  85. end component displayer;
  86. --定义信号量
  87. signal setn,mov,plus,clr,moden:std_logic;
  88. signal clk_hz,clk_10hz,clk_100hz,clk_khz:std_logic;
  89. signal set:std_logic;
  90. signal mode:std_logic_vector(3 downto 0):="0000";
  91. signal data1,data2,data3,data4:std_logic_vector(3 downto 0):="0000";
  92. signal set_data1,set_data2,set_data3,set_data4:std_logic_vector(3 downto 0):="0000";
  93. signal clock_data1,clock_data2,clock_data3,clock_data4,clock_data5,clock_data6:std_logic_vector(3 downto 0);
  94. signal watch_data1,watch_data2,watch_data3,watch_data4:std_logic_vector(3 downto 0);
  95. signal timer_data1,timer_data2,timer_data3,timer_data4:std_logic_vector(3 downto 0);
  96. signal com:std_logic_vector(3 downto 0);

  97. begin

  98. process(moden)is—mod按键置数
  99. begin
  100.         if(moden'event and moden='1')then
  101.                 if(mode="0011")then
  102.                         mode<="0000";
  103.                 else
  104.                         mode<=mode+'1';
  105.                 end if;
  106.         end if;
  107. end process;

  108. process(setn)is—set按键
  109. begin
  110.         if(setn'event and setn='1')then
  111.                 set<=not set;
  112.         end if;
  113. end process;

  114. process(set,mode)is—选通不同模块的data进行显示
  115. begin
  116.         if(set='1')then
  117.                 data1<=set_data1;
  118.                 data2<=set_data2;
  119.                 data3<=set_data3;
  120.                 data4<=set_data4;
  121.         elsif(set='0')then
  122.                 if(mode="0000")then
  123.                         data1<=clock_data3;
  124.                         data2<=clock_data4;
  125.                         data3<=clock_data5;
  126.                         data4<=clock_data6;
  127.                 elsif(mode="0001")then
  128.                         data1<=clock_data1;
  129.                         data2<=clock_data2;
  130.                         data3<=clock_data3;
  131.                         data4<=clock_data4;
  132.                 elsif(mode="0010")then
  133.                         data1<=watch_data1;
  134.                         data2<=watch_data2;
  135.                         data3<=watch_data3;
  136.                         data4<=watch_data4;
  137.                 elsif(mode="0011")then
  138.                         data1<=timer_data1;
  139.                         data2<=timer_data2;
  140.                         data3<=timer_data3;
  141.                         data4<=timer_data4;
  142.                 end if;
  143.         end if;
  144. end process;
  145. --按键1消抖
  146. fandou1:fandou port map(
  147.                 clk=>clk,
  148.                 fin=>key1,
  149.                 fout=>setn);
  150. --按键2消抖
  151. fandou2:fandou port map(
  152.                 clk=>clk,
  153.                 fin=>key2,
  154.                 fout=>mov);
  155. --按键3消抖               
  156. fandou3:fandou port map(
  157.                 clk=>clk,
  158.                 fin=>key3,
  159.                 fout=>plus);
  160. --按键4消抖               
  161. fandou4:fandou port map(
  162.                 clk=>clk,
  163.                 fin=>key4,
  164.                 fout=>clr);
  165. --按键5消抖               
  166. fandou5:fandou port map(
  167.                 clk=>clk,
  168.                 fin=>key5,
  169.                 fout=>moden);
  170. --秒分频
  171. name_div_hz:div_hz port map(
  172.                 clk=>clk,
  173.                 clk_out=>clk_hz);
  174. --100毫秒分频
  175. name_div_10hz:div_10hz port map(
  176.                 clk=>clk,
  177.                 clk_out=>clk_10hz);
  178. --10毫秒分频
  179. name_div_100hz:div_100hz port map(
  180.                 clk=>clk,
  181.                 clk_out=>clk_100hz);
  182. --毫秒分频
  183. name_div_khz:div_khz port map(
  184.                 clk=>clk,
  185.                 clk_out=>clk_khz);
  186. --时钟模块
  187. name_clock:clock port map(
  188.                 clk=>clk_hz,
  189.                 set=>set,
  190.                 plus=>not plus,
  191.                 clr=>not clr,
  192.                 set_data1=>set_data1,
  193.                 set_data2=>set_data2,
  194.                 set_data3=>set_data3,
  195.                 set_data4=>set_data4,
  196.                 clock_data1=>clock_data1,
  197.                 clock_data2=>clock_data2,
  198.                 clock_data3=>clock_data3,
  199.                 clock_data4=>clock_data4,
  200.                 clock_data5=>clock_data5,
  201.                 clock_data6=>clock_data6);
  202. --秒表模块
  203. name_watch:watch port map(
  204.                 clk=>clk_100hz,
  205.                 plus=>not plus,
  206.                 clr=>not clr,
  207.                 watch_data1=>watch_data1,
  208.                 watch_data2=>watch_data2,
  209.                 watch_data3=>watch_data3,
  210.                 watch_data4=>watch_data4);
  211. --定时器模块
  212. name_timer:timer port map(
  213.                 clk=>clk_hz,
  214.                 mov=>mov,
  215.                 plus=>not plus,
  216.                 clr=>not clr,
  217.                 set_data1=>set_data1,
  218.                 set_data2=>set_data2,
  219.                 set_data3=>set_data3,
  220.                 set_data4=>set_data4,
  221.                 timer_data1=>timer_data1,
  222.                 timer_data2=>timer_data2,
  223.                 timer_data3=>timer_data3,
  224.                 timer_data4=>timer_data4);
  225. --置数模块               
  226. name_set:setting port map(
  227.                 set=>not set,
  228.                 mov=>not mov,
  229.                 plus=>not plus,
  230.                 set_data1=>set_data1,
  231.                 set_data2=>set_data2,
  232.                 set_data3=>set_data3,
  233.                 set_data4=>set_data4);
  234. --闪烁模块               
  235. name_shine:shining port map(
  236.                 clk=>clk_10hz,
  237.                 set=>not set,
  238.                 mov=>not mov,
  239.                 com=>com,
  240.                 seg7com=>seg7com);
  241. --显示模块
  242. display:displayer port map(
  243.                 clk=>clk_khz,
  244.                 data1=>data1,
  245.                 data2=>data2,
  246.                 data3=>data3,
  247.                 data4=>data4,
  248.                 seg7com=>com,
  249.                 seg7data=>seg7data);
  250.                
  251. end architecture example;








  252. 3.消抖fandou.vhd
  253. library ieee;
  254. use ieee.std_logic_1164.all;
  255. use ieee.std_logic_unsigned.all;

  256. entity fandou IS
  257. port(clk,fin:in std_logic;
  258.         fout:out std_logic);
  259. end entity fandou;

  260. architecture example of fandou is
  261. begin
  262.         process(fin,clk)
  263.                 variable count:integer range 0 to 50000;
  264.         begin
  265.                 if(fin='0')then
  266.                         if(clk'event and clk='1')then
  267.                                 if(count<50000)then
  268.                                         count:=count+1;
  269.                                 else
  270.                                         count:=count;
  271.                                 end if;
  272.                                 if(count<50000)then
  273.                                         fout<='1';
  274.                                 else
  275.                                         fout<='0';
  276.                                 end if;
  277.                         end if;
  278.                 else
  279.                         count:=0;
  280.                         fout<='1';
  281.                 end if;
  282.         end process;
  283. end architecture example;



  284. 4.100毫秒分频div_10hz.vhd
  285. library ieee;
  286. use ieee.std_logic_1164.all;
  287. use ieee.std_logic_unsigned.all;

  288. entity div_10hz is
  289. port(clk:in std_logic;
  290.         clk_out:out std_logic);
  291. end entity div_10hz;

  292. architecture example of div_10hz is
  293. begin
  294.         process(clk)is
  295.                 variable counter:integer range 0 to 5000000;
  296.         begin
  297.                 if(clk'event and clk='1')then
  298.                         if (counter=5000000)then
  299.                                 counter:=0;
  300.                                 clk_out<='1';
  301.                         else
  302.                                 counter:=counter+1;
  303.                                 clk_out<='0';
  304.                         end if;
  305.                 end if;
  306.         end process;
  307. end architecture example;


  308. 5.10毫秒分频div_100hz.vhd
  309. library ieee;
  310. use ieee.std_logic_1164.all;
  311. use ieee.std_logic_unsigned.all;

  312. entity div_100hz is
  313. port(clk:in std_logic;
  314.         clk_out:out std_logic);
  315. end entity div_100hz;

  316. architecture example of div_100hz is
  317. begin
  318.         process(clk)is
  319.                 variable counter:integer range 0 to 500000;
  320.         begin
  321.                 if(clk'event and clk='1')then
  322.                         if (counter=500000)then
  323.                                 counter:=0;
  324.                                 clk_out<='1';
  325.                         else
  326.                                 counter:=counter+1;
  327.                                 clk_out<='0';
  328.                         end if;
  329.                 end if;
  330.         end process;
  331. end architecture example;


  332. 5.秒分频div_hz.vhd
  333. library ieee;
  334. use ieee.std_logic_1164.all;
  335. use ieee.std_logic_unsigned.all;

  336. entity div_hz is
  337. port(clk:in std_logic;
  338.         clk_out:out std_logic);
  339. end entity div_hz;

  340. architecture example of div_hz is
  341. begin
  342.         process(clk)is
  343.                 variable counter:integer range 0 to 50000000;
  344.         begin
  345.                 if(clk'event and clk='1')then
  346.                         if (counter=50000000)then
  347.                                 counter:=0;
  348.                                 clk_out<='1';
  349.                         else
  350.                                 counter:=counter+1;
  351.                                 clk_out<='0';
  352.                         end if;
  353.                 end if;
  354.         end process;
  355. end architecture example;


  356. 6.毫秒分频div_khz.vhd
  357. library ieee;
  358. use ieee.std_logic_1164.all;
  359. use ieee.std_logic_unsigned.all;

  360. entity div_khz is
  361. port(clk:in std_logic;
  362.         clk_out:out std_logic);
  363. end entity div_khz;

  364. architecture example of div_khz is
  365. begin
  366.         process(clk)is
  367.                 variable counter:integer range 0 to 50000;
  368.         begin
  369.                 if(clk'event and clk='1')then
  370.                         if (counter=50000)then
  371.                                 counter:=0;
  372.                                 clk_out<='1';
  373.                         else
  374.                                 counter:=counter+1;
  375.                                 clk_out<='0';
  376.                         end if;
  377.                 end if;
  378.         end process;
  379. end architecture example;


  380. 7.时钟clock.vhd
  381. library ieee;
  382. use ieee.std_logic_1164.all;
  383. use ieee.std_logic_unsigned.all;

  384. entity clock is
  385. port(clk,set,plus,clr:in std_logic;
  386.         set_data1,set_data2,set_data3,set_data4:in std_logic_vector(3 downto 0);
  387.         clock_data1,clock_data2,clock_data3,clock_data4,clock_data5,clock_data6:out std_logic_vector(3 downto 0));
  388. end entity clock;

  389. architecture example of clock is

  390. component count60 is
  391. port(clk,clr,wr,cin:in std_logic;
  392.         co:out std_logic;
  393.         data_in1:in std_logic_vector(3 downto 0);
  394.         data_in2:in std_logic_vector(3 downto 0);
  395.         data_out1:out std_logic_vector(3 downto 0);
  396.         data_out2:out std_logic_vector(3 downto 0));
  397. end component count60;

  398. component count24 is
  399. port(clk,clr,wr,cin:in std_logic;
  400.         co:out std_logic;
  401.         data_in1:in std_logic_vector(3 downto 0);
  402.         data_in2:in std_logic_vector(3 downto 0);
  403.         data_out1:out std_logic_vector(3 downto 0);
  404.         data_out2:out std_logic_vector(3 downto 0));
  405. end component count24;
  406. --定义信号量
  407. signal data1,data2,data3,data4,data5,data6:std_logic_vector(3 downto 0):="0000";
  408. signal co1,co2,co3:std_logic;

  409. begin
  410. --输出赋值
  411. clock_data1<=data1;
  412. clock_data2<=data2;
  413. clock_data3<=data3;
  414. clock_data4<=data4;
  415. clock_data5<=data5;
  416. clock_data6<=data6;
  417. --秒计数
  418. count1:count60 port map(
  419.                 clk=>clk,
  420.                 clr=>clr,
  421.                 wr=>'0',
  422.                 cin=>'1',
  423.                 co=>co1,
  424.                 data_in1=>"0000",
  425.                 data_in2=>"0000",
  426.                 data_out1=>data1,
  427.                 data_out2=>data2);
  428.                
  429. --分计数               
  430. count2:count60 port map(
  431.                 clk=>clk,
  432.                 clr=>clr,
  433.                 wr=>plus,
  434.                 cin=>co1,
  435.                 co=>co2,
  436.                 data_in1=>set_data1,
  437.                 data_in2=>set_data2,
  438.                 data_out1=>data3,
  439.                 data_out2=>data4);
  440. --时计数
  441. count3:count24 port map(
  442.                 clk=>clk,
  443.                 clr=>clr,
  444.                 wr=>plus,
  445.                 cin=>co2,
  446.                 co=>co3,
  447.                 data_in1=>set_data3,
  448.                 data_in2=>set_data4,
  449.                 data_out1=>data5,
  450.                 data_out2=>data6);
  451.                
  452. end architecture example;


  453. 8.秒表watch.vhd
  454. library ieee;
  455. use ieee.std_logic_1164.all;
  456. use ieee.std_logic_unsigned.all;

  457. entity watch is
  458. port(clk,plus,clr:in std_logic;
  459.         watch_data1,watch_data2,watch_data3,watch_data4:out std_logic_vector(3 downto 0));
  460. end entity watch;

  461. architecture example of watch is
  462. --component申明
  463. component count100 is
  464. port(clk,clr,en,wr,cin:in std_logic;
  465.         co:out std_logic;
  466.         data_in1:in std_logic_vector(3 downto 0);
  467.         data_in2:in std_logic_vector(3 downto 0);
  468.         data_out1:out std_logic_vector(3 downto 0);
  469.         data_out2:out std_logic_vector(3 downto 0));
  470. end component count100;
  471. --定义信号量
  472. signal data1,data2,data3,data4:std_logic_vector(3 downto 0):="0000";
  473. signal co0,co1,co2,co3,co4:std_logic;
  474. signal plusn:std_logic:='1';

  475. begin
  476. --输出赋值
  477. watch_data1<=data1;
  478. watch_data2<=data2;
  479. watch_data3<=data3;
  480. watch_data4<=data4;
  481. --判断plus按下
  482. process(plus)is
  483. begin
  484.         if(plus'event and plus='1')then
  485.                 plusn<=not plusn;
  486.         end if;
  487. end process;
  488. --个位、十位计数
  489. count1:count100 port map(
  490.                 clk=>clk,
  491.                 clr=>clr,
  492.                 en=>plusn,
  493.                 wr=>'0',
  494.                 cin=>'1',
  495.                 co=>co1,
  496.                 data_in1=>"0000",
  497.                 data_in2=>"0000",
  498.                 data_out1=>data1,
  499.                 data_out2=>data2);
  500. --百位、千位计数               
  501. count2:count100 port map(
  502.                 clk=>clk,
  503.                 clr=>clr,
  504.                 en=>plusn,
  505.                 wr=>'0',
  506.                 cin=>co1,
  507.                 co=>co2,
  508.                 data_in1=>"0000",
  509.                 data_in2=>"0000",
  510.                 data_out1=>data3,
  511.                 data_out2=>data4);

  512. end architecture example;


  513. 9.定时器timer.vhd
  514. library ieee;
  515. use ieee.std_logic_1164.all;
  516. use ieee.std_logic_unsigned.all;

  517. entity timer is
  518. port(clk,mov,plus,clr:in std_logic;
  519.         set_data1,set_data2,set_data3,set_data4:in std_logic_vector(3 downto 0);
  520.         timer_data1,timer_data2,timer_data3,timer_data4:out std_logic_vector(3 downto 0));
  521. end entity timer;

  522. architecture example of timer is
  523. --component申明
  524. component dcount100 is
  525. port(clk,clr,en,wr,cin:in std_logic;
  526.         co:out std_logic;
  527.         data_in1:in std_logic_vector(3 downto 0);
  528.         data_in2:in std_logic_vector(3 downto 0);
  529.         data_out1:out std_logic_vector(3 downto 0);
  530.         data_out2:out std_logic_vector(3 downto 0));
  531. end component dcount100;
  532. --定义信号量
  533. signal data1,data2,data3,data4:std_logic_vector(3 downto 0):="0000";
  534. signal co1,co2,co3,co4:std_logic;
  535. signal plusn,movn:std_logic:='1';
  536. signal ant,antn:std_logic;

  537. begin
  538. --输出赋值
  539. timer_data1<=data1;
  540. timer_data2<=data2;
  541. timer_data3<=data3;
  542. timer_data4<=data4;
  543. --判断按键mov按下
  544. process(mov)is
  545. begin
  546.         if(mov'event and mov='1')then
  547.                 movn<=not movn;
  548.         end if;
  549. end process;
  550. --判断倒计数为0暂停
  551. process(clk)is
  552. begin
  553.         if(data1="0000" and data2="0000" and data3="0000" and data4="0000")then
  554.                 antn<='1';
  555.         end if;
  556. end process;

  557. process(antn)is
  558. begin
  559.         if(antn'event and antn='1')then
  560.                 ant<=not ant;
  561.         end if;
  562. end process;
  563. --个位、十位倒计数
  564. count1:dcount100 port map(
  565.                 clk=>clk,
  566.                 clr=>clr,
  567.                 en=>movn or ant,
  568.                 wr=>plus,
  569.                 cin=>'1',
  570.                 co=>co1,
  571.                 data_in1=>set_data1,
  572.                 data_in2=>set_data2,
  573.                 data_out1=>data1,
  574.                 data_out2=>data2);
  575. --百位、千位倒计数
  576. count2:dcount100 port map(
  577.                 clk=>clk,
  578.                 clr=>clr,
  579.                 en=>movn or ant,
  580.                 wr=>plus,
  581.                 cin=>co1,
  582.                 co=>co2,
  583.                 data_in1=>set_data3,
  584.                 data_in2=>set_data4,
  585.                 data_out1=>data3,
  586.                 data_out2=>data4);

  587. end architecture example;



  588. 10.100进制计数器count100.vhd
  589. library ieee;
  590. use ieee.std_logic_1164.all;
  591. use ieee.std_logic_unsigned.all;

  592. entity count100 is
  593. port(clk,clr,en,wr,cin:in std_logic;
  594.         co:out std_logic;
  595.         data_in1:in std_logic_vector(3 downto 0);
  596.         data_in2:in std_logic_vector(3 downto 0);
  597.         data_out1:out std_logic_vector(3 downto 0);
  598.         data_out2:out std_logic_vector(3 downto 0));
  599. end entity count100;

  600. architecture example of count100 is
  601. --信号量定义
  602. signal data1:std_logic_vector(3 downto 0);
  603. signal data2:std_logic_vector(3 downto 0);

  604. begin
  605.         data_out1<=data1;
  606.         data_out2<=data2;
  607. --个位计数
  608. process(clk,wr)is
  609. begin
  610.         if(clr='1')then
  611.                 data1<="0000";
  612.         elsif(wr='1')then
  613.                 data1<=data_in1;
  614.         elsif(clk'event and clk='1')then
  615.                 if(en='0')then
  616.                         if(cin='1')then
  617.                                 if(data1=9)then
  618.                                         data1<="0000";
  619.                                 else
  620.                                         data1<=data1+1;
  621.                                 end if;
  622.                         end if;
  623.                 end if;
  624.         end if;
  625. end process;
  626. --十位计数
  627. process(clk,wr)is
  628. begin
  629.         if(clr='1')then
  630.                 data2<="0000";
  631.         elsif(wr='1')then
  632.                 data2<=data_in2;
  633.         elsif(clk'event and clk='1')then
  634.                 if(en='0')then
  635.                         if(cin='1' and data1=9)then
  636.                                 if(data2=9)then
  637.                                         data2<="0000";
  638.                                 else
  639.                                         data2<=data2+1;
  640.                                 end if;
  641.                         end if;
  642.                 end if;
  643.         end if;
  644. end process;
  645. --进位判断
  646. process(data1,data2,cin)is
  647. begin
  648.         if(cin='1'and data1=9 and data2=9)then
  649.                 co<='1';
  650.         else
  651.                 co<='0';
  652.         end if;
  653. end process;
  654. end architecture example;



  655. 11.100进制倒计数器dcount100.vhd
  656. library ieee;
  657. use ieee.std_logic_1164.all;
  658. use ieee.std_logic_unsigned.all;

  659. entity dcount100 is
  660. port(clk,clr,en,wr,cin:in std_logic;
  661.         co:out std_logic;
  662.         data_in1:in std_logic_vector(3 downto 0);
  663.         data_in2:in std_logic_vector(3 downto 0);
  664.         data_out1:out std_logic_vector(3 downto 0);
  665.         data_out2:out std_logic_vector(3 downto 0));
  666. end entity dcount100;

  667. architecture example of dcount100 is
  668. --信号量定义
  669. signal data1:std_logic_vector(3 downto 0);
  670. signal data2:std_logic_vector(3 downto 0);

  671. begin
  672.         data_out1<=data1;
  673.         data_out2<=data2;
  674. --个位计数
  675. process(clk,wr,en)is
  676. begin
  677.         if(clr='1')then
  678.                 data1<="0000";
  679.         elsif(wr='1')then
  680.                 data1<=data_in1;
  681.         elsif(clk'event and clk='1')then
  682.                 if(en='0')then
  683.                         if(cin='1')then
  684.                                 if(data1=0)then
  685.                                         data1<="1001";
  686.                                 else
  687.                                         data1<=data1-1;
  688.                                 end if;
  689.                         end if;
  690.                 end if;
  691.         end if;
  692. end process;
  693. --十位计数
  694. process(clk,wr)is
  695. begin
  696.         if(clr='1')then
  697.                 data2<="0000";
  698.         elsif(wr='1')then
  699.                 data2<=data_in2;
  700.         elsif(clk'event and clk='1')then
  701.                 if(en='0')then
  702.                         if(cin='1' and data1=0)then
  703.                                 if(data2=0)then
  704.                                         data2<="1001";
  705.                                 else
  706.                                         data2<=data2-1;
  707.                                 end if;
  708.                         end if;
  709.                 end if;
  710.         end if;
  711. end process;
  712. --进位判断
  713. process(data1,data2,cin)is
  714. begin
  715.         if(cin='1'and data1=0 and data2=0)then
  716.                 co<='1';
  717.         else
  718.                 co<='0';
  719.         end if;
  720. end process;
  721. end architecture example;

  722. 12.60进制计数器count60.vhd
  723. library ieee;
  724. use ieee.std_logic_1164.all;
  725. use ieee.std_logic_unsigned.all;

  726. entity count60 is
  727. port(clk,clr,wr,cin:in std_logic;
  728.         co:out std_logic;
  729.         data_in1:in std_logic_vector(3 downto 0);
  730.         data_in2:in std_logic_vector(3 downto 0);
  731.         data_out1:out std_logic_vector(3 downto 0);
  732.         data_out2:out std_logic_vector(3 downto 0));
  733. end entity count60;

  734. architecture example of count60 is

  735. signal data1:std_logic_vector(3 downto 0);
  736. signal data2:std_logic_vector(3 downto 0);

  737. begin
  738.         data_out1<=data1;
  739.         data_out2<=data2;
  740. process(clk,wr)is
  741. begin
  742.         if(clr='1')then
  743.                 data1<="0000";
  744.         elsif(wr='1')then
  745.                 data1<=data_in1;
  746.         elsif(clk'event and clk='1')then
  747.                 if(cin='1')then
  748.                         if(data1=9)then
  749.                                 data1<="0000";
  750.                         else
  751.                                 data1<=data1+1;
  752.                         end if;
  753.                 end if;
  754.         end if;
  755. end process;

  756. process(clk,wr)is
  757. begin
  758.         if(clr='1')then
  759.                 data2<="0000";
  760.         elsif(wr='1')then
  761.                 data2<=data_in2;
  762.         elsif(clk'event and clk='1')then
  763.                 if(cin='1' and data1=9)then
  764.                         if(data2=5)then
  765.                                 data2<="0000";
  766.                         else
  767.                                 data2<=data2+1;
  768.                         end if;
  769.                 end if;
  770.         end if;
  771. end process;

  772. process(data1,data2,cin)is
  773. begin
  774.         if(cin='1'and data1=9 and data2=5)then
  775.                 co<='1';
  776.         else
  777.                 co<='0';
  778.         end if;
  779. end process;
  780. end architecture example;


  781. 13.24进制计数器count24.vhd
  782. library ieee;
  783. use ieee.std_logic_1164.all;
  784. use ieee.std_logic_unsigned.all;

  785. entity count24 is
  786. port(clk,clr,wr,cin:in std_logic;
  787.         co:out std_logic;
  788.         data_in1:in std_logic_vector(3 downto 0);
  789.         data_in2:in std_logic_vector(3 downto 0);
  790.         data_out1:out std_logic_vector(3 downto 0);
  791.         data_out2:out std_logic_vector(3 downto 0));
  792. end entity count24;

  793. architecture example of count24 is

  794. signal data1:std_logic_vector(3 downto 0);
  795. signal data2:std_logic_vector(3 downto 0);

  796. begin
  797.         data_out1<=data1;
  798.         data_out2<=data2;
  799. process(clk,wr)is
  800. begin
  801.         if(clr='1')then
  802.                 data1<="0000";
  803.         elsif(wr='1')then
  804.                 data1<=data_in1;
  805.         elsif(clk'event and clk='1')then
  806.                 if(cin='1')then
  807.                         if(data1=9)then
  808.                                 data1<="0000";
  809.                         else
  810.                                 data1<=data1+1;
  811.                         end if;
  812.                 end if;
  813.                 if(cin='1'and data1=3 and data2=2)then
  814.                         data1<="0000";
  815.                 end if;
  816.         end if;
  817. end process;

  818. process(clk,wr,cin)is
  819. begin
  820.         if(clr='1')then
  821.                 data2<="0000";
  822.         elsif(wr='1')then
  823.                 data2<=data_in2;
  824.         elsif(clk'event and clk='1')then
  825.                 if(cin='1' and data1=9)then
  826.                         if(data2=2)then
  827.                                 data2<="0000";
  828.                         else
  829.                                 data2<=data2+1;
  830.                         end if;
  831.                 end if;
  832.                 if(cin='1'and data1=3 and data2=2)then
  833.                         data2<="0000";
  834.                 end if;
  835.         end if;
  836. end process;

  837. process(data1,data2,cin)is
  838. begin
  839.         if(cin='1'and data1=3 and data2=2)then
  840.                 co<='1';
  841.         else
  842.                 co<='0';
  843.         end if;
  844. end process;
  845. end architecture example;



  846. 14.置数模块 setting.vhd
  847. library ieee;
  848. use ieee.std_logic_1164.all;
  849. use ieee.std_logic_unsigned.all;

  850. entity setting is
  851. port(set,mov,plus:in std_logic;
  852.         set_data1,set_data2,set_data3,set_data4:out std_logic_vector(3 downto 0));
  853. end entity setting;

  854. architecture example of setting is

  855. signal data1,data2,data3,data4:std_logic_vector(3 downto 0):="0000";
  856. signal count:std_logic_vector(3 downto 0):="0000";

  857. begin

  858. set_data1<=data1;
  859. set_data2<=data2;
  860. set_data3<=data3;
  861. set_data4<=data4;
  862. --选通一路输出加1
  863. process(set,plus,count)is
  864. begin
  865.         if(set='0')then
  866.                 if(plus'event and plus='1')then
  867.                         case count is
  868.                                 when "0000"=>data1<=data1+'1';
  869.                                 when "0001"=>data2<=data2+'1';
  870.                                 when "0010"=>data3<=data3+'1';
  871.                                 when "0011"=>data4<=data4+'1';
  872.                                 when others=>NULL;
  873.                         end case;
  874.                 end if;
  875.         end if;
  876. end process;

  877. process(mov,count)is
  878. begin
  879.         if(mov'event and mov='1')then
  880.                 if(count="0011")then
  881.                         count<="0000";
  882.                 else
  883.                         count<=count+'1';
  884.                 end if;
  885.         end if;
  886. end process;

  887. end architecture example;


  888. 15.闪烁模块 shining.vhd
  889. library ieee;
  890. use ieee.std_logic_1164.all;
  891. use ieee.std_logic_unsigned.all;

  892. entity shining is
  893. port(clk,set,mov:in std_logic;--秒脉冲,设置键,移位键
  894.         com:in std_logic_vector(3 downto 0);
  895.         seg7com:out std_logic_vector(3 downto 0));
  896. end entity shining;

  897. architecture example of shining is

  898. signal count:std_logic_vector(3 downto 0):="0000";
  899. signal com2:std_logic_vector(3 downto 0);
  900. signal squ:std_logic;

  901. begin

  902. process(mov)is
  903.         variable dot: integer range 0 to 3;
  904. begin
  905.         if(mov'event and mov='1')then--按下移位键
  906.                 if(dot=3)then
  907.                         dot:=0;
  908.                 else
  909.                         dot:=dot+1;
  910.                 end if;
  911.                 case dot is
  912.                         when 0=>count<="1000";
  913.                         when 1=>count<="0100";
  914.                         when 2=>count<="0010";
  915.                         when 3=>count<="0001";
  916.                         when others=>null;
  917.                 end case;
  918.         end if;
  919. end process;

  920. process(clk)is
  921. begin
  922.         if(clk'event and clk='1')then
  923.                 squ<=not squ;
  924.         end if;
  925. end process;
  926. --按下set闪烁
  927. process(set,com,squ)is
  928. begin
  929.         if(set='1')then
  930.                 com2<="0000";
  931.         elsif(set='0')then
  932.                 com2<=count;
  933.         end if;
  934. --或运算
  935.         if(squ='1')then
  936.                 seg7com<=com or com2;
  937.         else       
  938.                 seg7com<=com;
  939.         end if;
  940. end process;

  941. end architecture example;


  942. 16.显示模块 displayer.vhd
  943. library ieee;
  944. use ieee.std_logic_1164.all;
  945. use ieee.std_logic_unsigned.all;

  946. entity displayer is
  947. port(clk:in std_logic;
  948.         data1,data2,data3,data4:in std_logic_vector(3 downto 0);
  949.         seg7com:out std_logic_vector(3 downto 0);
  950.         seg7data:out std_logic_vector(7 downto 0));
  951. end entity displayer;

  952. architecture example of displayer is
  953. component seg7led is
  954. port(bcd_in:in std_logic_vector(3 downto 0);
  955.         dot:in std_logic;
  956.         data_out:out std_logic_vector(7 downto 0));
  957. end component seg7led;

  958. signal data:std_logic_vector(3 downto 0);
  959. signal dot,setn:std_logic;

  960. begin
  961. led:seg7led port map(
  962.                 bcd_in=>data,
  963.                 dot=>dot,
  964.                 data_out=>seg7data);
  965. --选通四路输出扫频
  966. process(clk)is
  967.         variable count: integer range 0 to 3;
  968. begin
  969.         if(clk'event and clk='0')then
  970.                 if(count=3)then
  971.                         count:=0;
  972.                 else
  973.                         count:=count+1;
  974.                 end if;
  975.                 case count is
  976.                         when 0=>seg7com<="0111";data<=data1;dot<='1';
  977.                         when 1=>seg7com<="1011";data<=data2;dot<='1';
  978.                         when 2=>seg7com<="1101";data<=data3;dot<='0';
  979.                         when 3=>seg7com<="1110";data<=data4;dot<='1';
  980.                         when others=>null;
  981.                 end case;
  982.         end if;
  983. end process;

  984. end architecture example;



  985. 17.4-7译码器 seg7led.vhd
  986. LIBRARY IEEE;
  987. USE IEEE.STD_LOGIC_1164.ALL;

  988. ENTITY seg7led IS
  989.         PORT(bcd_in:IN STD_LOGIC_VECTOR(3 downto 0);
  990.                 dot:in std_logic;
  991.                 data_out:OUT STD_LOGIC_VECTOR(7 downto 0));
  992. END seg7led;

  993. ARCHITECTURE example OF seg7led IS
  994. signal data:std_LOGIC_VECTOR(6 downto 0);
  995. BEGIN
  996. data_out(0)<=data(0);
  997. data_out(1)<=data(1);
  998. data_out(2)<=data(2);
  999. data_out(3)<=data(3);
  1000. data_out(4)<=data(4);
  1001. data_out(5)<=data(5);
  1002. data_out(6)<=data(6);
  1003. data_out(7)<=dot;
  1004.         process(bcd_in)
  1005.         begin
  1006.                         case bcd_in is
  1007.                         when "0000" => data <= "1000000";                       
  1008.                         when "0001" => data <= "1111001";
  1009.                         when "0010" => data <= "0100100";
  1010.                         when "0011" => data <= "0110000";
  1011.                         when "0100" => data <= "0011001";
  1012.                         when "0101" => data <= "0010010";
  1013.                         when "0110" => data <= "0000010";
  1014.                         when "0111" => data <= "1111000";
  1015.                         when "1000" => data <= "0000000";
  1016.                         when "1001" => data <= "0010000";
  1017.                         when others => NULL;
  1018.                 end case;
  1019.         end process;
  1020.        
  1021.         END example;
  1022.                        
复制代码

全部资料51hei下载地址:
VHDL程序.docx (21.81 KB, 下载次数: 22)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表