初级会员 
  
	- 积分
 - 149
 
        - 金钱
 - 149 
 
       - 注册时间
 - 2015-3-12
 
      - 在线时间
 - 170 小时
 
 
 
 | 
 
 
 楼主 |
发表于 2021-4-13 20:58:03
|
显示全部楼层
 
 
 
我在网上下载了一段代码,可以用,现在我贴上来 
`timescale 1ns / 1ps 
module pwm_gen( 
 
input rst_n, 
input sysclk, 
input en, 
 
input [15:0] period, 
input [15:0] h_time, 
 
output reg pwm 
 
); 
 
reg [31:0] cnt; 
//period=16'h7800; 
 
always @ (posedge sysclk or negedge rst_n) 
begin 
    if(!rst_n) 
        cnt <= 0; 
    else  
                begin 
                        if(cnt >= period - 1 ) 
                          cnt <= 0; 
                        else 
                          cnt <= cnt + 1; 
                end 
end 
 
always @ (posedge sysclk or negedge rst_n) 
begin 
    if(!rst_n) 
        pwm <= 0; 
    else    //rst_n = 1 
    begin 
        if(en == 0) 
            pwm <= 0; 
        else    //en = 1 
        begin 
            if(cnt <= h_time - 1) 
                pwm <= 1; 
            else 
                pwm <= 0; 
        end 
    end 
end 
 
endmodule |   
 
 
 
 |