22 lines
628 B
Verilog
22 lines
628 B
Verilog
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 |