分频器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
相关推荐
GateWorld2 小时前
FPGA内部模块详解之三 FPGA的“记忆细胞”——嵌入式块内存(Block RAM)
fpga开发·block ram
良许Linux3 小时前
ASIC的设计和制造
单片机·嵌入式硬件·fpga开发·程序员·嵌入式·制造
minglie13 小时前
Amaranth HDL
python·fpga开发
s09071366 小时前
保姆级教程一:ZYNQ-7030开发板安装/烧录Linux系统详细指南(小白必看)
linux·fpga开发·系统安装·zynq
lf2824814317 小时前
03 xilinx除法IP核的使用
fpga开发
智能物联网开发8 小时前
机器人电子皮肤系统开发:36通道柔性触觉阵列 + FPGA高速采集
fpga开发·计算机外设·嵌入式·fpga数据采集
沐欣工作室_lvyiyi9 小时前
基于FPGA的智能音箱设计(论文+源码)
fpga开发·毕业设计·智能音箱
我爱C编程10 小时前
【硬件片内测试】基于FPGA的4FSK扩频通信链路测试,包含帧同步,定时点,扩频伪码同步,信道,误码统计
fpga开发·帧同步·定时点·扩频通信·扩频伪码同步·4fsk
GateWorld10 小时前
Lattice FPGA开发全攻略--十余种输出文件格式及其区别
fpga开发·lattice·fpga开发工具
芯门1 天前
基于 Xilinx K7 FPGA 的全套万兆 10G GigE Vision 商业级传输方案
计算机视觉·fpga开发·万兆gige