vhdl 编写counter ,模拟钟表

来源:互联网 发布:斗鱼 诸葛网络 编辑:程序博客网 时间:2024/05/16 11:00

    好久没玩过FPGA了,回想起当时和张胤一起写的SOPC系统还真是怀念啊,自己编写的CPU,自己编写的外设驱动(软件+硬件),自己定义的汇编语言,自己编写的汇编器,基于这些自己的东西写自己的应用程序,那些好玩的游戏~ 怀念,有时间一定写篇文章纪念一下。

     选这个课,是想回忆下基本的VHDL知识,什么东西不用都会忘掉的。这是一个很简单的程序,有时分秒的功能,没有任何管脚指定,仅仅用modelsim仿真了一下。

      counter的代码:

 

  1. --------------------------------------------------------------- 
  2. -- 
  3. --      Author: Chaos Lee(ID:sy1106508) 
  4. --      Name: A simple counter. 
  5. --      Date: 2012/05/06 
  6. -- 
  7. --------------------------------------------------------------- 
  8. library ieee; 
  9. use ieee.std_logic_1164.all
  10. use ieee.std_logic_arith.all
  11. use ieee.std_logic_unsigned.all
  12. entity counter is 
  13.  port( 
  14.    clk: in std_logic; 
  15.    clr: in std_logic; 
  16.    sec: out std_logic_vector(5 downto 0); 
  17.    minout std_logic_vector(5 downto 0); 
  18.    hourout std_logic_vector(4 downto 0) 
  19.    ); 
  20. end entity; 
  21. architecture behave of counter is 
  22. begin 
  23.   process(clk,clr) 
  24.  variable sec_cnt: integer range 0 to 59; 
  25.  variable min_cnt: integer range 0 to 59; 
  26.  variable hour_cnt: integer range 0 to 23; 
  27.   begin 
  28.   if(clr = '0'then 
  29.    sec_cnt := 0; 
  30.    min_cnt := 0; 
  31.    hour_cnt := 0; 
  32.   else 
  33.    if(clk'event and clk = '1')then 
  34.     if(sec_cnt = 59) then 
  35.      sec_cnt := 0; 
  36.      if(min_cnt = 59) then 
  37.       min_cnt := 0; 
  38.       if(hour_cnt = 23) then 
  39.        hour_cnt := 0; 
  40.       else 
  41.        hour_cnt := hour_cnt + 1; 
  42.       end if; 
  43.      else  
  44.       min_cnt := min_cnt + 1; 
  45.      end if; 
  46.     else 
  47.      sec_cnt := sec_cnt+1; 
  48.     end if; 
  49.    end if; 
  50.   end if; 
  51.   sec <= conv_std_logic_vector(sec_cnt,6); 
  52.   min <= conv_std_logic_vector(min_cnt,6); 
  53.   hour <= conv_std_logic_vector(hour_cnt,5); 
  54.   end process; 
  55. end architecture; 

      modelsim的测试代码:

    

  1. ------------------------------------------------------------ 
  2. -- 
  3. --    Author: Chaos Lee (ID:sy1106508) 
  4. --    Name: counter_tb 
  5. --    Description: A testbench aiming to verify the counter simulating a clock 
  6. --    Date: 2012/05/06 
  7. -- 
  8. ------------------------------------------------------------- 
  9. library ieee; 
  10. use ieee.std_logic_1164.all
  11. use ieee.std_logic_arith.all
  12. use ieee.std_logic_unsigned.all
  13.  
  14. entity counter_tb is 
  15. end entity; 
  16.  
  17. architecture behave of counter_tb is 
  18. component counter is 
  19.  port( 
  20.    clk: in std_logic; 
  21.    clr: in std_logic; 
  22.    sec: out std_logic_vector(5 downto 0); 
  23.    minout std_logic_vector(5 downto 0); 
  24.    hourout std_logic_vector(4 downto 0) 
  25.    ); 
  26. end component; 
  27.  
  28. signal clk_tb:std_logic; 
  29. signal clr_tb:std_logic; 
  30. signal sec_tb:std_logic_vector(5 downto 0); 
  31. signal min_tb:std_logic_vector(5 downto 0); 
  32. signal hour_tb:std_logic_vector(4 downto 0); 
  33.  
  34. begin 
  35.   u1:counter port map ( 
  36.       clk => clk_tb ,  
  37.       clr => clr_tb , 
  38.       sec => sec_tb , 
  39.       min => min_tb , 
  40.       hour => hour_tb 
  41.       ); 
  42.   test_bench:process 
  43.   begin 
  44.     clr_tb <='0' , '1' after 20ns; 
  45.     clk_tb <= '1'
  46.     for i in 10000 downto 0 loop 
  47.       wait for 10 ns; 
  48.       clk_tb <= not clk_tb; 
  49.     end loop; 
  50.   wait; 
  51.   end process; 
  52. end architecture; 

本文出自 “相信并热爱着” 博客,请务必保留此出处http://hipercomer.blog.51cto.com/4415661/855155

原创粉丝点击