前言
呼吸灯也就是PWM调光,这个相比于网上其他大部分代码内容少一点,但是功能还是可以实现的。代码是在别人的基础上修改的。我自己也是迷迷糊糊的,参考链接:https://www.cnblogs.com/hechengfei/p/4106538.html
第一版
module led(LED,CLK);
output reg LED = 1;
input CLK;//50Mhz
// 时钟参数化
parameter CLK_50M = 31'd50_000_000;
// 时间参数化
parameter UP_TIME = 1;
parameter T1 = CLK_50M / 100 * UP_TIME; // T1=UP_TIME*(1s/100)=UP_TIME*10us,时间改变占空比,时间内 有100个 不同占空比的脉冲。T1表示每个占空比的周期,也就是cunt2的最大值。
parameter high_TZL = T1 / 100; // 高电平时间有1000个等级,也就是cut1的最大值。
reg [30:0] cut1=high_TZL;//变亮时间计数变量,T1 = 1/1000/100s
reg [30:0] cut0=0;//子周期内的高电平计数变量,增量是1/50 000 000s,最大值是T1
reg flag=1;//1变亮,0变暗
always @(posedge CLK)
begin
if(cut0<cut1) begin LED<=1; cut0<=cut0+1; end
else if(cut0< T1) begin LED<=0;cut0<=cut0+1; end
else
begin
cut0<=0;
if(flag==1) begin cut1<=cut1+high_TZL;
if(cut1>=T1-high_TZL) begin cut1<=cut1; flag=~flag; end end
else begin cut1<=cut1-high_TZL;
if(cut1<high_TZL) begin cut1<=high_TZL; flag=~flag; end end
end
end
endmodule