由于今天连续多次无法发布该文章,心态真的是崩了,由于基础的PWM比较简单,此次先给大家展示个半成品,完整状态对应的PWM频率、占空比均可调节,对应的模块结构图如下:
对应的基本code如下:
module creat_PWM
(
input wire clk, //系统时钟为50MHz
input wire rst,
input wire key_flag1,
input wire key_flag2,
output reg PWM
);
parameter Frequency_CNT_MAX = 16'd49_999; //输出PWM为1KHz,1ms=5000*20ns
//PWM频率生成计数器模块
reg [15:0] couter;
always @(posedge clk or negedge rst)
if( rst == 1'b0 )
couter <= 0;
else if( couter == Frequency_CNT_MAX )
couter <= 0;
else
couter <= couter + 1'b1;
//占空比调节模块
reg [15:0] duty_counter;
always @(posedge clk or negedge rst)
if( rst == 1'b0 )
duty_counter <= 16'd24_999;
else if( key_flag1 == 1'b1 )
duty_counter <= duty_counter + 16'd49;
else if( key_flag2 == 1'b1 )
duty_counter <= duty_counter - 16'd49;
else
duty_counter <= duty_counter;
//生成PWM
always @(posedge clk or negedge rst)
if( rst == 1'b0 )
PWM <= 1'b0;
else if( duty_counter <= Frequency_CNT_MAX )
PWM <= 1'b1;
else
PWM <= 1'b0;
endmodule
由于是第一次在电子发烧友上发文章,体验感觉真的不太友好,希望能够把文章的自动保存功能给加上,否则没有备份真的让人不开心
更多回帖