出于练习的原因,我正在尝试编写一些简单的 Verilog 代码。我正在使用 FPGA Cyclone 4。我的 8 位计数器在板载时钟 (50MHz) 下工作正常,但是在那个速度下看 LED 太快了,所以我首先尝试用这个来减慢时钟:
module enableCounter(
input clock_5,
input reset,
output reg enable_out);
reg [3:0] counter;
always @(posedge clock_5) begin
if (reset) begin
counter <= 4'b0000;
end
else begin
counter <= counter + 1;
if (counter == 4'b0000)
enable_out <= 1;
else
enable_out <= 0;
end
end
endmodule
从技术上讲,这会将 enable_out 输出设置为 1,仅是 clock_5 时间的第 8 次。为了向上和向下计数,我还写了这个函数:
module updown_counter(
input up_down,
input clk,
input enable,
input reset,
output reg [7:0] count_out);
always @ (posedge clk) begin
if (up_down == 0) begin // Count UP
if (reset)
count_out <= 8'b0000_0000;
else if (enable && count_out == 8'b1111_1111)
count_out <= 8'b0000_0000;
else if (enable && count_out != 8'b1111_1111)
count_out <= count_out + 8'b0000_0001;
else
count_out <= count_out;
end
else if (up_down == 1) begin // Count DOWN
if (reset)
count_out <= 8'b1111_1111;
else if (enable && count_out == 8'b0000_0000)
count_out <= 8'b1111_1111;
else if (enable && count_out != 8'b0000_0000)
count_out <= count_out - 8'b0000_0001;
else
count_out <= count_out;
end
end
endmodule
没有编译错误。当我按下重置按钮时,8 个 LED(连接到 count_out)关闭并全部打开。我也尝试改变一些值,比如 up_down 方向,只是为了检查它是否是按钮故障,但它做了相反的事情,这表明我的程序中有一个错误(我猜?)