`timescale 1ns / 1ns
module pwm_light# ( parameter REF_CLK = 64'd50_000_000 ) ( input I_sysclk_p, input I_rstn, output O_pwm );
localparam T20US_SET = REF_CLK/20000 - 1; //set 20us localparam T20MS_SET = 100 - 1; //set 20ms localparam PWM_SET = 100 - 1; //Set PWM duty cycle adjustment times
reg [23:0] t20us_cnt; reg [9 :0] t20ms_cnt; reg [9 :0] pwm_cnt; reg PWM_S;
wire t20us_done; wire t20ms_done; //设置20us计数,当计数条件达成时拉高 assign t20us_done = (t20us_cnt == T20US_SET); ///设置20ms计数,当计数条件达成时拉高 assign t20ms_done = t20us_done & (t20ms_cnt == T20MS_SET) ;
//1us base timer always @(posedge I_sysclk_p or negedge I_rstn)begin if(I_rstn == 1'b0) t20us_cnt <= 0; else if(t20us_cnt < T20US_SET) //t20us_cnt寄存器用来计数,目标值是T20US_SET t20us_cnt <= t20us_cnt + 1'b1; //未达到目标值+1 else t20us_cnt <= 24'd0; //达到目标值清零 end
//1ms pwm compare timer,based on 1us base timer always @(posedge I_sysclk_p or negedge I_rstn)begin //系统时钟的上升沿以及复位的下降沿触发 if(I_rstn == 1'b0) //复位清零 t20ms_cnt <= 0; else if(t20us_done) //t20us_done计数完成作为后续判断的大前提 t20ms_cnt <= (t20ms_cnt < T20MS_SET) ? (t20ms_cnt + 1'b1) : 10'd0; //判断语句,当t20ms_cnt < T20MS_SET满足时 else //t20ms_cnt计数器+1,否则就清零 t20ms_cnt <= t20ms_cnt; end
//PWM counter for generating duty cycle parameters as well as PWM adjustment count always @(posedge I_sysclk_p or negedge I_rstn)begin //系统时钟的上升沿以及复位的下降沿触发 if(I_rstn == 1'b0) //复位清零 pwm_cnt <= 0; else if(t20ms_done) //t20ms_done计数完成作为后续判断的大前提 pwm_cnt <= (pwm_cnt < PWM_SET) ? (pwm_cnt + 1'b1) : 10'd0; //判断语句,当pwm_cnt < PWM_SET满足时 else //pwm_cntt计数器+1,否则就清零 pwm_cnt <= pwm_cnt; end
//switch breathing state always @(posedge I_sysclk_p or negedge I_rstn)begin //系统时钟的上升沿以及复位的下降沿触发 if(I_rstn == 1'b0) PWM_S <= 0; //复位清零 else if(t20ms_done && (pwm_cnt == PWM_SET)) //pwm_cnt == PWM_SET达成时,对PWM_S的状态进行翻转 PWM_S <= ~PWM_S; //反应到LED灯上是LED灯的亮灭变换转换 else PWM_S <= PWM_S; end //添加ila IP ,Chipscope观察信号 ila_0 ila_debug ( .clk(I_sysclk_p), // input wire clk .probe0({t20us_done,O_pwm}) // input wire [1:0] probe0 ); //PWM wave generating assign O_pwm = PWM_S ? (pwm_cnt >= t20ms_cnt) : (pwm_cnt < t20ms_cnt); //判断语句,通过PWM_S的变换,使O_pwm输出的 endmodule
|