当前位置:   article > 正文

VHDL实现正计时器_99秒计时vhdl程序

99秒计时vhdl程序

所用开发板:正点原子新起点V2

目录

功能介绍:

系统总框图:

分频器模块:

数码管显示模块:

计时器模块:

实验效果图:


功能介绍:

实现了一个带有小数位的正计时功能的计时器,按下reset键系统复位,key1开始计时,key2暂停计时,按下按键蜂鸣器会发出响声

系统总框图:

 

分频器模块:

  1. LIBRARY IEEE;
  2. USE IEEE.std_logic_1164.ALL;
  3. USE IEEE.std_logic_unsigned.all;
  4. ENTITY divider IS
  5. PORT(
  6. clk:IN std_logic;
  7. rst_n:IN std_logic;
  8. clk_div:OUT std_logic
  9. );
  10. END ENTITY;
  11. ARCHITECTURE behav OF divider IS
  12. signal count:integer range 1 to 25000000;
  13. signal clk_out:std_logic;
  14. BEGIN
  15. PROCESS(clk)
  16. BEGIN
  17. IF (rst_n='0') THEN clk_out<='0';
  18. ELSIF rising_edge(clk)
  19. THEN count<=count+1;
  20. if (count=2500000)
  21. THEN clk_out<=not clk_out;
  22. count<=1;
  23. end if;
  24. END IF;
  25. clk_div<=clk_out;
  26. END PROCESS;
  27. END behav;

数码管显示模块:

  1. library ieee;use ieee.std_logic_1164.all;
  2. use ieee.std_logic_unsigned.all;
  3. entity seg is
  4. port(
  5. clk: in std_logic;
  6. rst_n:in std_logic;
  7. ids_data: in std_logic_vector (11 downto 0);
  8. seg_duan: out std_logic_vector (7 downto 0);
  9. seg_wei: out std_logic_vector (5 downto 0)
  10. );
  11. end seg;
  12. architecture behav of seg is
  13. constant num0:std_logic_vector(7 downto 0) := x"c0";
  14. constant num1:std_logic_vector(7 downto 0) := x"f9";
  15. constant num2:std_logic_vector(7 downto 0) := x"a4";
  16. constant num3:std_logic_vector(7 downto 0):= x"b0";
  17. constant num4:std_logic_vector(7 downto 0) := x"99";
  18. constant num5:std_logic_vector(7 downto 0) := x"92";
  19. constant num6:std_logic_vector(7 downto 0):= x"82";
  20. constant num7:std_logic_vector(7 downto 0) := x"f8";
  21. constant num8:std_logic_vector(7 downto 0) := x"80";
  22. constant num9:std_logic_vector(7 downto 0) := x"90";
  23. constant num00:std_logic_vector(7 downto 0) := x"40";
  24. constant num11:std_logic_vector(7 downto 0) := x"79";
  25. constant num22:std_logic_vector(7 downto 0) := x"24";
  26. constant num33:std_logic_vector(7 downto 0) := x"30";
  27. constant num44:std_logic_vector(7 downto 0) := x"19";
  28. constant num55:std_logic_vector(7 downto 0) := x"12";
  29. constant num66:std_logic_vector(7 downto 0) := x"02";
  30. constant num77:std_logic_vector(7 downto 0) := x"78";
  31. constant num88:std_logic_vector(7 downto 0) := x"00";
  32. constant num99:std_logic_vector(7 downto 0) := x"10";
  33. constant numa:std_logic_vector(7 downto 0) := x"88";
  34. constant numb:std_logic_vector(7 downto 0) := x"83";
  35. constant numc:std_logic_vector(7 downto 0):= x"c6";
  36. constant numd:std_logic_vector(7 downto 0) := x"a1";
  37. constant nume:std_logic_vector(7 downto 0) := x"86";
  38. constant numf:std_logic_vector(7 downto 0) := x"8e";
  39. constant we0:std_logic_vector(5 downto 0) := "111110";
  40. constant we1:std_logic_vector(5 downto 0) := "111101";
  41. constant we2:std_logic_vector(5 downto 0) := "111011";
  42. constant we:std_logic_vector(5 downto 0) := "111111";
  43. signal a:integer range 0 to 100 :=0;
  44. signal b:integer range 0 to 100 :=0;
  45. signal c:integer range 0 to 100 :=0;
  46. begin
  47. process(rst_n,clk,ids_data)
  48. variable cnt_4 : std_logic_vector (9 downto 0);
  49. variable seg_num : std_logic_vector (3 downto 0);
  50. begin
  51. if (rst_n = '0') then cnt_4:="0000000000";
  52. elsif(rising_edge(clk)) then cnt_4:= cnt_4+1;
  53. end if;
  54. if (rst_n= '0') then seg_num:= "0000";
  55. elsif(rising_edge(clk)) then
  56. case (cnt_4 (9 downto 8)) is
  57. when "00" => seg_num:=ids_data (3 downto 0);
  58. when "01" => seg_num:=ids_data (7 downto 4);
  59. when "10" =>seg_num:=ids_data (11 downto 8) ;
  60. when others=>NULL;
  61. end case;
  62. end if;
  63. if (rst_n='0') then seg_wei<= "000000";
  64. elsif (rising_edge(clk)) then
  65. if(cnt_4(7 downto 0) <="11100011")then
  66. case cnt_4 (9 downto 8) is
  67. when "00"=>
  68. seg_wei<=we0;
  69. when "01"=>
  70. seg_wei<=we1;
  71. when "10"=>
  72. seg_wei<=we2;
  73. when others=>
  74. seg_wei<=we;
  75. end case;
  76. else seg_wei<=we;
  77. end if;
  78. end if;
  79. if (rst_n='0') then seg_duan<= "00000000";
  80. elsif (rising_edge(clk)) then
  81. if(cnt_4(9 downto 8)="01")then
  82. case seg_num is
  83. when "0000"=>seg_duan<=num00;
  84. when "0001"=>seg_duan<=num11;
  85. when "0010"=>seg_duan<=num22;
  86. when "0011"=>seg_duan<=num33;
  87. when "0100"=>seg_duan<=num44;
  88. when "0101"=>seg_duan<=num55;
  89. when "0110"=>seg_duan<=num66;
  90. when "0111"=>seg_duan<=num77;
  91. when "1000"=>seg_duan<=num88;
  92. when "1001"=>seg_duan<=num99;
  93. when others=>NULL;
  94. end case;
  95. else
  96. case seg_num is
  97. when "0000"=>seg_duan<=num0;
  98. when "0001"=>seg_duan<=num1;
  99. when "0010"=>seg_duan<=num2;
  100. when "0011"=>seg_duan<=num3;
  101. when "0100"=>seg_duan<=num4;
  102. when "0101"=>seg_duan<=num5;
  103. when "0110"=>seg_duan<=num6;
  104. when "0111"=>seg_duan<=num7;
  105. when "1000"=>seg_duan<=num8;
  106. when "1001"=>seg_duan<=num9;
  107. when others=>NULL;
  108. end case;
  109. end if;
  110. end if;
  111. end process;
  112. end behav;

计时器模块:

  1. LIBRARY IEEE;
  2. USE IEEE.std_logic_1164.ALL;
  3. USE IEEE.std_logic_unsigned.All;
  4. USE IEEE.std_logic_arith.ALL;
  5. ENTITY demo1 IS
  6. PORT(clk:IN std_logic;
  7. clk_div:IN std_logic;
  8. reset:IN std_logic; --复位按键
  9. key1:IN std_logic; --开始按键
  10. key2:IN std_logic; --停止按键
  11. sound:OUT std_logic;
  12. led_0:OUT std_logic;--等待状态指示灯
  13. led_1:OUT std_logic;--开始计时状态
  14. led_2:OUT std_logic;--暂停计时状态
  15. num:out std_Logic_vector(11 downto 0) );--输出
  16. END ENTITY;
  17. ARCHITECTURE behav OF demo1 IS
  18. TYPE state_type IS(s0,s1,s2);
  19. SIGNAL present_state,next_state:state_type;
  20. Signal waiting_time:integer range 0 to 999:=0 ; --抢答倒计时时间
  21. signal o1,o2,o3 :std_logic_vector(3 downto 0); --用来组成输出的数字
  22. SIGNAL q1,q2,q3:integer range 0 to 1200 :=0;
  23. SIGNAL tmp:integer range 0 to 1200 :=0;
  24. SIGNAL flag:std_logic_vector(1 DOWNTO 0):="00";
  25. BEGIN
  26. seq:PROCESS(clk_div,reset)
  27. BEGIN
  28. IF (reset='0')THEN
  29. present_state<=s0;
  30. ELSIF(rising_edge(clk_div))THEN
  31. present_state<=next_state;
  32. END IF;
  33. END PROCESS;
  34. ns:PROCESS(clk_div,present_state)
  35. BEGIN
  36. CASE present_state IS
  37. WHEN s0=> --等待状态
  38. IF(key1='0')THEN next_state<=s1;
  39. ELSE next_state<=s0;
  40. END IF;
  41. WHEN s1=> --计时状态
  42. IF(key2='0')THEN next_state<=s2;
  43. ELSE next_state<=s1;
  44. END IF;
  45. WHEN s2=> --暂停状态
  46. IF(key1='0')THEN next_state<=s1;
  47. ELSE next_state<=s2;
  48. END IF;
  49. END CASE;
  50. END PROCESS;
  51. PROCESS(clk_div, present_state,reset)
  52. BEGIN
  53. IF rising_edge(clk_div) THEN
  54. CASE present_state IS
  55. WHEN s0=>
  56. led_0<='1';
  57. led_1<='0';
  58. led_2<='0';
  59. waiting_time<=0;
  60. WHEN s1=>
  61. led_0<='0';
  62. led_1<='1';
  63. led_2<='0';
  64. waiting_time<=waiting_time+1;
  65. WHEN s2=>
  66. led_0<='0';
  67. led_1<='0';
  68. led_2<='1';
  69. END CASE;
  70. tmp<=waiting_time/10;
  71. q2<=tmp rem 10;
  72. q3<=waiting_time/100;
  73. q1<=waiting_time rem 10;
  74. o1<=conv_std_logic_vector(q1,4);
  75. o2<=conv_std_logic_vector(q2,4);
  76. o3<=conv_std_logic_vector(q3,4);
  77. num<=o3&o2&o1;
  78. END IF;
  79. END PROCESS;
  80. PROCESS(clk_div, key1, key2)
  81. BEGIN
  82. IF rising_edge(clk_div) THEN
  83. IF (key1='0') or (key2='0')THEN
  84. flag <= "10";
  85. sound <= '1';
  86. ELSIF reset='0' THEN sound<='0';flag<="00";
  87. ELSIF flag="00" THEN
  88. sound <= '0';
  89. ELSE flag <= flag - '1';
  90. END IF;
  91. END IF;
  92. END PROCESS;
  93. END behav;

实验效果图:

待机状态:

 

计时状态:

暂停计时状态:

恢复计时状态:

 完整工程代码:VHDL语言实现了一个带有小数位的正计时功能的计时器-嵌入式文档类资源-CSDN文库

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

闽ICP备14008679号