module pwm_test(input clk , //时钟输入,可在外部设置不同时钟input rst_n , //低电平复位input [7:0] f , //频率控制,最大255input [7:0] d , //占空比控制字,上限100output pwm_out //pwm输出);reg [17:0] count ; //计数always @(posedge clk or negedge rst_n) beginif(~rst_n) begincount <= 0 ;endelse if(count >= 17'd100_000) //计数到100k清零count <= 0 ;elsecount <= count + f; //每次累加频率值endassign pwm_out = (count < d*1000)? 1:0 ;//pwm输出endmodule思路就是倍频累加,剩下的拨码开关程序自己想一下吧。例如输入时钟100m,频率设为20的时候,计数100k,每次加20,输出频率就是100m/(100k/20)=20k,占空比你一看就明白至于输入时钟,用tools->megawizard plug-in manager->i/o->altpll模块设置pll分频,倍频即可