Files
hardware/Verilog/test/test.v
2025-12-31 19:35:06 +08:00

22 lines
628 B
Verilog
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module pwm (
input wire clk, // 时钟输入
input wire rst, // 异步复位
input wire [7:0] duty, // 占空比0~255
output reg pwm_out // 输出信号
);
reg [7:0] counter = 0; // 8位计数器周期为 256
always @(posedge clk or posedge rst) begin
if (rst) begin
counter <= 0;
pwm_out <= 0;
end else begin
counter <= counter + 1;
// 比较占空比设置
if (counter < duty)
pwm_out <= 1;
else
pwm_out <= 0;
end
end
endmodule