分频器code

理论学习

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

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

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

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

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

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

偶数分频

module    divider_six    
(
    input    wire    sys_clk         ,
    input    wire    sys_rst_n       ,
        
    output   reg     clk_out
);


    reg    [3:0]    cnt    ;

    always@(posedge sys_clk or negedge sys_rst_n )
    begin
        if( sys_rst_n == 1'b0 )
        begin
            cnt    <=    4'd0    ;
        end
        else if( cnt == 4'd2 )
        begin
            cnt    <=    4'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 == 4'b2 )
        begin
            clk_out    <=    ~clk_out    ;
        end
    end
    


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   ;
    
    //产生0 ~4 循环
    always@( posedge sys_clk or negedge sys_rst_n )
    begin
        if( sys_rst_n == 1'b0 )
        begin
            cnt    <=    3'b0    ;
        end
        else if( cnt == 3'd4 )
        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
            if( sys_rst_n == 1'b0 )
            begin
                clk1    <=    1'b1    ;
            end
            else    if( cnt == 3'd2 )
            begin
                clk1    <=    1'b0    ;
            end
            else    if( cnt == 3'd4 )
            begin
                clk1    <=    1'b1    ;
            end
        end
    end
    //产生两个高电平和三个低电平
    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    if( cnt == 3'd4 )
        begin
            clk2    <=    1'b1    ;
        end
    end

    assign    clk_out    =    clk1 & clk2    ;


endmodule

你可能感兴趣的:(verilog开发实战指南,fpga开发)