「Verilog学习笔记」占空比50%的奇数分频

专栏前言

本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网

「Verilog学习笔记」占空比50%的奇数分频_第1张图片「Verilog学习笔记」占空比50%的奇数分频_第2张图片

根据题意7分频,实际上是第一次电平变化经历了4个上升沿+3个下降沿,第二次电平变化是4个下降沿+3个上升沿,所以用两个计数器就行了。分别对上升沿和下降沿进行计数,计数总共到7就可以让输出信号取反。

`timescale 1ns/1ns

module odo_div_or
   (
    input    wire  rst ,
    input    wire  clk_in,
    output   wire  clk_out7
    );

//*************code***********//
    reg [3:0] cnt1, cnt2 ; 
    reg [1:0] data ; 

    always @ (posedge clk_in or negedge rst) begin 
        if (~rst) cnt1 <= 'd0 ; 
        else cnt1 <= cnt1 + 1'd1 ; 
    end

    always @ (negedge clk_in or negedge rst) begin 
        if (~rst) cnt2 <= 'd0 ; 
        else cnt2 <= cnt2 + 1'd1 ; 
    end

    always @ (*) begin 
        if (~rst) data <= 'd0 ;
        else if (cnt1 + cnt2 == 3'd7) begin 
            cnt1 <= 'd0 ;
            cnt2 <= 'd0 ; 
            data <= ~data ; 
        end
        else data <= data ; 
    end

    assign clk_out7 = data ; 

//*************code***********//
endmodule

你可能感兴趣的:(Verilog学习笔记,学习,笔记,Verilog)