打印
[verilog]

一种浅显的脉宽控制的方案

[复制链接]
5874|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
邹逸谦|  楼主 | 2021-9-30 10:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
module bx (
    input        clk,
    input        rst_n,
    input        laser_tx_en,
    input [24:0] pwm_width,
    input [24:0] pwm_cycle,
    input [5:0]  laser_tx_num,  
    output     laser_on,     
    output     frame_sync,   
    output     laser_tx_done
);

    reg  [24 : 0]   pwm_cnt;      
    reg             laser_on_i;  
    reg  [5:0]  num_cnt;   

    always @(posedge clk or negedge rst_n) begin
        if (~rst_n) begin
            pwm_cnt    <= 25'd0;
        end
        else if (laser_tx_en)  begin
            if (pwm_cnt == pwm_cycle)  begin     
                pwm_cnt    <= 25'd0;
            end
            else
                pwm_cnt    <= pwm_cnt + 25'd1;
        end
        else
            pwm_cnt   <=  25'd0;
    end
   
    always @(posedge clk or negedge rst_n) begin
        if (~rst_n) begin
            laser_on_i   <=  1'd0;
        end
        else if (laser_tx_en) begin
            case (num_cnt > laser_tx_num)
                1'b1: laser_on_i   <=  1'd0;
                1'b0:if (pwm_cnt <= pwm_width) begin
                        laser_on_i   <=  1'd1;
                    end
                    else if (pwm_cnt >= pwm_width) begin
                        laser_on_i   <=  1'd0;
                    end
                    else
                        laser_on_i   <=  laser_on_i;
                default: laser_on_i  <=  1'd0;
            endcase
        end
    end
    always @(posedge clk or negedge rst_n) begin
        if (~rst_n) begin
            num_cnt    <=  6'd0;
        end
        else if (pwm_cnt == pwm_cycle)begin
            num_cnt    <=  num_cnt + 6'd1;     
        end
        else if (num_cnt > laser_tx_num) begin   
            num_cnt    <=  6'd0;
        end
    end

    assign   laser_on        =  laser_on_i;   
    assign   frame_sync      =  ((pwm_cnt == 1) & laser_on_i) ? 1'd1 : 1'd0;  
    assign   laser_tx_done   =  (num_cnt > laser_tx_num) ? 1'd1 : 1'd0;   
endmodule

控制脉冲与发射次数,达到一般的通用水准。

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

0

粉丝