Prev Problem
Next Problem

30. PWM with 4-bit Resolution

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

The top one is for continuous PWM rather than just one pulse of a particular length, the second is the latter as this is technically what the system here would like.

 

 

Code for actual PWM

module pwm4_basic (input wire clk, 
                    input wire rst, 
                    input wire [3:0] duty, 
                    output reg pwm_out);

    reg [3:0] count = 0;

    always @(posedge clk) begin
        count = (rst | count == duty) ? 0 : count + 1;
        pwm_out <= |count;
    end

endmodule

Code for what they want

module pwm4_basic (input wire clk, 
                    input wire rst, 
                    input wire [3:0] duty, 
                    output reg pwm_out);

    reg [3:0] count = 0;

    always @(posedge clk) begin
        count <= rst ? 0 : count + 1;
        pwm_out <= rst ? 0 : count < duty;
    end

endmodule
Was this helpful?
Upvote
Downvote