本文对时钟脉冲进行计数,如果有需要按键计数器的,请移步《Verilog语言实现两个按键的简单加减计数器》
FPGA默认提供50Mhz的始终脉冲,我们需要对时钟脉冲进行分频以得到所需的频率,以下为分频代码:(为了仿真此处将分频计数器计数值调小)
module fenpin(
input clk,
input rst_n,
output reg clk_1s
);
reg [24:0] cnt; // 分频计数器
always @ (posedge clk or negedge rst_n) begin
if (!rst_n) begin
cnt <= 25'b0;
clk_1s <= 1'b0;
end
else if(cnt < 25'd2501) begin //n倍的偶数分频,只需要计数器在N/2-1 的时候将时钟反转即可
cnt <= cnt + 1'b1;
clk_1s <= clk_1s;
end
else begin
cnt <= 25'b0;
clk_1s <= ~clk_1s;
end
end
endmodule
以下为计数器代码:
module counter_counting(
input clk,
input rst,
output re