分频器code

理论学习

数字电路中时钟占有非常重要的地位。时间的计算都依靠时钟信号作为基本单元。一般而言,一块板子只有一个晶振,即只有一种频率的时钟,但是数字系统中,经常需要对基准时钟进行不同倍数的分频,进而得到各模块所需的频率。

若想得到比系统时钟更慢的时钟,可以将基准时钟进行分频。

若想得到比系统时钟更快的时钟,可以将基准时钟进行倍频。

不管是分频还是倍频,都通过PLL实现或者用verilog描述实现。

我们用verilog实现的一般是分频电路,即分频器。分频电路是数字系统设计中常用的基本电路之一。

分频器分为偶数分频和奇数分频。

偶数分频demo1

复制代码
module    divider_sex
(
    input    wire    sys_clk      ,
    input    wire    sys_rst_n    ,

    output   reg     clk_out
);


    reg    [1:0]    cnt    ;

    always@( posedge sys_clk or negedge sys_rst_n )
    begin
        if( sys_rst_n == 1'b0 )
        begin
            cnt    <=    2'd0    ;
        end
        else if( cnt == 2'd2 )
        begin
            cnt    <=    2'd0    ;
        end
        else
        begin
            cnt    <=    cnt    + 1'b1    ;
        end
    end

    always@(posedge sys_clk or negedge sys_rst_n )
    begin
        if(  sys_rst_n == 1'b0 )
        begin
            clk_out    <=    1'b0    ;
        end 
        else if( cnt == 2'd2 )
        begin
            clk_out    <=    ~clk_out    ;
        end
    end


endmodule

偶数分频demo2

复制代码
module    divider_six
(
    input    wire    sys_clk      ,
    input    wire    sys_rst_n    ,

    outpur   reg     clk_out
);

    reg    [2:0]    cnt    ;
    
    always@( posedge sys_clk or negedge sys_rst_n )
    begin
        if( sys_rst_n == 1'b0 )
        begin
            cnt    <=    3'd0    ;
        end
        else    if( cnt == 3'd5 )
        begin
            cnt    <=    3'd0    ;
        end
        else
        begin
            cnt    <=    cnt    + 1'b1    ;
        end
    end

    always@( posedge sys_clk or negedge sys_rst_n )
    begin
        if( sys_rst_n == 1'b0 )
        begin
            clk_out    <=    1'b0    ;
        end
        else if( cnt == 3'd4 )
        begin
            clk_out    <=    1'b1    ;
        end
        else    
        begin
            clk_out    <=    1'b0    ;    
        end
    end


endmodule

偶数分频对应的tb

复制代码
`timescale    1ns/1ns


module    tb_divider_six();


    reg    sys_clk      ;
    reg    sys_rst_n    ;
    wire   clk_out      ;
 
    initial
    begin
        sys_clk    =    1'b1    ;
        sys_rst_n    <=    1'b0    ;
        #20    ;
        sys_rst_n    <=    1'b1    ;
    end

    always #10     sys_clk     <=    ~sys_clk    ;

    divider_six    divider_six_inst
    (
        .sys_clk      (sys_clk)      ,
        .sys_rst_n    (sys_rst_n)    ,

        .clk_out      (clk_out)
    );

endmodule

奇数分频

复制代码
module    divider_five
(
    input    wire    sys_clk      ,
    input    wire    sys_rst_n    ,

    output   wire    clk_out
);

    reg    [2:0]    cnt     ;
    reg             clk1    ;
    reg             clk2    ;


    always@( posedge sys_clk or negedge sys_rst_n )
    begin
        if( sys_rst_n == 1'b0 )
        begin
            cnt    <=    3'd0    ;
        end
        else if( cnt == 3'd4 )
        begin
            cnt    <=    3'd0    ;
        end
        else
        begin
            cnt    <=    cnt    + 1'b1    ;
        end
    end
    
    //clk1
    always@( posedge sys_clk or negedge sys_rst_n )        
    begin
        if( sys_rst_n == 1'b0 )
        begin
            clk1    <=    1'b1    ;
        end
        else if( cnt == 3'd2 )
        begin
            clk1    <=    1'b0    ;
        end
        else
        begin
            clk1    <=    1'b1    ;
        end
    end


    //clk2
    always@( negedge sys_clk or negedge sys_rst_n )
    begin
        if( sys_rst_n == 1'b0 )
        begin
            clk2    <=    1'b1    ;
        end
        else if( cnt    ==    3'd2 )
        begin
            clk2    <=    1'b0    ;
        end
        else
        begin
            clk2    <=    1'b1    ;
        end
    end


    assign    clk_out    =    clk1 & clk2    ;

endmodule

奇数分频的tb

复制代码
`timescale 1ns/1ns


module    tb_divider_five();


    reg    sys_clk      ; 
    reg    sys_rst_n    ;
    wire   clk_out      ;


    initial
    begin
        sys_clk    =    1'b1    ;
        sys_rst_n    <=    1'b0    ;
        #20    ;
        sys_rst_n    <=    1'b1    ;  
    end

    always #10    sys_clk    <=    ~sys_clk    ;

    divider_five    divider_five_inst
    (
        .sys_clk      (sys_clk)      ,
        .sys_rst_n    (sys_rst_n)    ,

        .clk_out      (clk_out)
    );


endmodule
相关推荐
ZPC82107 天前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC82107 天前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
tiantianuser8 天前
RDMA设计53:构建RoCE v2 高速数据传输系统板级测试平台2
fpga开发·rdma·高速传输·cmac·roce v2
博览鸿蒙8 天前
FPGA 和 IC,哪个前景更好?怎么选?
fpga开发
FPGA_小田老师8 天前
xilinx原语:ISERDESE2原语详解(串并转换器)
fpga开发·iserdese2·原语·串并转换
tiantianuser8 天前
RDMA设计50: 如何验证网络嗅探功能?
网络·fpga开发·rdma·高速传输·cmac·roce v2
Lzy金壳bing8 天前
基于Vivado平台对Xilinx-7K325t FPGA芯片进行程序在线更新升级
fpga开发·vivado·xilinx
unicrom_深圳市由你创科技8 天前
医疗设备专用图像处理板卡定制
图像处理·人工智能·fpga开发
tiantianuser8 天前
RDMA设计52:构建RoCE v2 高速数据传输系统板级测试平台
fpga开发·rdma·高速传输·cmac·roce v2
luoganttcc8 天前
Taalas 将人工智能模型蚀刻到晶体管上,以提升推理能力
人工智能·fpga开发