fpga中,一般外接的晶振是50Mhz,如果电路中一个模块需要25mhz时钟,那么进行一个2分频,这个是相当容易的,下面是一种方法,还有可以用一个二进制计数器实现。这里就不写代码了。easy.同样的原理 ,四分频也很容易。

process(clk)--clk输入时钟;
begin
  if(rst = '0') then  --rst复位信号;
     clkout <= '0';
  elsif(clk;event and clk = '1')then
     clkout <= not clk;
  end if;
end process;
但是如果实现一个三分频呢?? 是不是3分频器应该是每1.5的clock就0变1、1变0,但问题来了,哪来的1.5个clock?计数器并不能产生1.5!!正源触发与负源触发的间隔时间刚好是0.5个clock?所以我们产生两个clock,一个是posedge clk,一个是negedge clk,最后将两个clock做or,这样就可以产生出0.5个clock了。下面给出代码:::
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity clk_div_n is

    port(clk : in std_logic;
         rst : in std_logic; 
         clkout :out std_logic
         );
end clk_div_n;

architecture rtl of clk_div_n is
    
constant n   : integer range 0 to 10 := 6;  --这里的n可以是任意值,当然要大于1.
signal clk_p : std_logic;
signal clk_n : std_logic;

signal cnt_p : integer range 0 to n;
signal cnt_n : integer range 0 to n;

begin 
    process(clk_p, clk_n)
    begin
        if((n mod 2) = 0)then
            clkout <= clk_p;
        else
            clkout <= clk_p or clk_n;
        end if;
    end process;

     process(clk, rst)
     begin
        if(rst = '0') then
           cnt_p <= 0;
        elsif(clk'event and clk = '1') then
          if(cnt_p = n-1) then
              cnt_p <= 0;
          else
              cnt_p <= cnt_p + 1;
          end if;
        end if;
     end process;
        
     process(clk, rst)
     begin
          if(rst = '0') then
             clk_p <= '0';
          elsif(clk'event and clk = '1')then
            if (cnt_p < (n/2)) then
                clk_p <= '1';
            else
                clk_p <= '0';
            end if ;
          end if;
     end process; 
     
     
     process(clk, rst)
     begin
        if(rst = '0') then
           cnt_n <= 0;
        elsif(clk'event and clk = '0')then
          if(cnt_n = n-1) then
              cnt_n <= 0;
          else
              cnt_n <= cnt_n + 1;
          end if;
        end if;
     end process;
     
     process(clk, rst)
     begin
          if(rst = '0') then
             clk_n <= '0';
          elsif(clk'event and clk = '0')then
            if (cnt_n < (n/2)) then
                clk_n <= '1';
            else
                clk_n <= '0';
            end if ;
          end if;
     end process; 
end rtl;             
接下来我给出对应的testbench::有兴趣可以用make a simulation in modelsim 
LIBRARY ieee  ; 
USE ieee.std_logic_1164.all  ; 
USE ieee.std_logic_arith.all  ; 
USE ieee.std_logic_unsigned.all  ; 
ENTITY clk_div_n_tb  IS 
END clk_div_n_tb; 
 
ARCHITECTURE clk_div_tb_arch OF clk_div_n_tb IS
  SIGNAL clkout   :  std_logic  ; 
  SIGNAL rst   :  std_logic := '0' ; 
  SIGNAL clk   :  std_logic := '1' ; 
  COMPONENT clk_div_n  
    PORT ( 
      clk  : in std_logic ;
      rst  : in std_logic ; 
      clkout  : out std_logic
       ); 
  END COMPONENT ; 
BEGIN
  process
    begin
    wait for 50ns;
       clk <= not clk;
  end process;
  rst <= '1' after 200ns;
    test:clk_div_n
    PORT MAP ( 
      clk    => clk,
      rst    => rst,
      clkout => clkout) ; 
END clk_div_tb_arch; 

最新文章

  1. AngularJS SPA Template For Visual Studio
  2. hnu10104
  3. Mysql报错:1172 - Result consisted of more than one row
  4. password安全之动态盐
  5. C# 正则表达式(一)
  6. oracle计算年龄
  7. LFS 中文版手册发布:如何打造自己的 Linux 发行版
  8. C#界面设计疑问2:panel摆放问题
  9. Bower安装
  10. memcache与mysql的连接
  11. 12集合(3)-----Map
  12. WPF应用
  13. Dubbo 生态添新兵,Dubbo Admin 发布 v0.1
  14. skb的两个函数pskb_copy和skb_copy
  15. iPhone 配置使用工具
  16. 线程安全计算 AtomicLong
  17. Windows 内核漏洞学习—空指针解引用
  18. SLAP(Speaker-Listener Label Propagation Algorithm)社区发现算法
  19. python 返回系统名称,系统平台,系统版本
  20. 基于easyUI实现经典系统主界面

热门文章

  1. docker centos:latest 使用 sshd
  2. three.js是什么,能干嘛,和webgl什么关系
  3. JS Ajax异步请求发送列表数据后面多了[]
  4. hdu 5137 去掉一个点 使得最短路最大(2014广州现场赛 K题)
  5. Elastic-Job开发指南
  6. shell在linux里摇摇晃晃
  7. Django实战(21):使用内置的Amin管理用户
  8. 8-2 Party Games uva1610 (贪心)
  9. CentOS源码安装搭建LNMP全过程(包括nginx,mysql,php,svn)
  10. win10 远程桌面远程电脑时每次要输入密码及身份验证错误,要求的函数不受支持问题解决