一般采用计数分频的办法,如何计算分频的计数值 N :
如果是50Mhz分频1Khz,那么表达式为:
1/50Mhz × N = 1/1Khz
N = 50Mhz / 1Khz = 50000
如果是50Mhz分频2hz,那么表达式为:
1/50Mhz × N = 1/2hz
N = 50Mhz / 2hz = 25000000
1、1Khz
reg [31:0] cnt;
reg clk_1;
always@(posedge clk_50M or negedge rst_n)
begin
if(!rst_n)
begin
cnt <= 32'd0;
clk_1 <= 1'b0;
end
else
begin
if(cnt == 32'd50_000-1) // N - 1
clk_1 <= ~clk_1; // 因为是从0开始计数的
else
cnt <= cnt + 1;
end
end
2、2hz
reg [31:0] cnt;
reg clk_1;
always@(posedge clk_50M or negedge rst_n)
begin
if(!rst_n)
begin
cnt <= 32'd0;
clk_1 <= 1'b0;
end
else
begin
if(cnt == 32'd25_000_000-1)
clk_1 <= ~clk_1;
else
cnt <= cnt + 1;
end
end