Prev Problem
Next Problem

30. PWM with 4-bit Resolution

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

 

Code

/*Write your code here*/
module pwm4_basic(
    input clk, rst,
    input [3:0] duty,
    output pwm_out
);
    reg [3:0] cnt;
    reg pwm;
    assign pwm_out = pwm;
    always@(posedge clk) begin
        if (rst || cnt == 4'b1111) begin
            cnt <= 4'b0000;
            pwm <= 1'b0;
        end else if (cnt < duty) begin
            pwm <= 1'b1;
            cnt <= cnt + 4'b0001;
        end else begin
            cnt <= cnt + 4'b0001;
            pwm <= 1'b0;
        end
    end
endmodule

 

Was this helpful?
Upvote
Downvote