3.1 呼吸灯的实现
breath_led
`timescale 1ns / 1ps
module breath_led(
input clk,
input rst_n,
output led
);
reg [15:0] period_cnt;//变化的频率设为1kHz,周期是1ms
reg [15:0] duty_cycle;//占空比
reg inc_dec_flag;//0为递增,1为递减
assign led = (period_cnt >= duty_cycle)?1'b1 : 1'b0;
always @(posedge clk or negedge rst_n)begin
if(!rst_n)
period_cnt <= 16'd0;
else if(period_cnt == 16'd50000)
period_cnt <= 16'd0;
else
period_cnt <= period_cnt + 1'b1;
end
always @(posedge clk or negedge rst_n)begin
if(!rst_n)begin
duty_cycle <= 16'd0;
inc_dec_flag <= 1'b0;
end
else begin
if(period_cnt == 15'd50000)begin
if(inc_dec_flag == 1'b0)begin
if(duty_cycle == 16'd50000)
inc_dec_flag <= 1'b1;
else
duty_cycle <= duty_cycle + 16'd25;
end
else begin
if(duty_cycle == 16'd0)
inc_dec_flag <= 1'b0;
else
duty_cycle <= duty_cycle - 16'd25;
end
end
end
end
endmodule