新手入门的简单小例子第三个,这几个例子主要给入门的新手建立时间或者说时序的概念,最近看了不少的资料基本上都是以点灯为例,估计是大家对嵌入式开发和FPGA的普遍共识,这两天逛B站发现了也不少比较好的视频教程,我就在这里二次提炼一下,做上几篇文章,为大家的学习垫上两块砖头,现在这一篇是第三篇,是对第二篇的另一种描述方法。
实验之前:
(1)默认对应的FPGA外部输入时钟为50MHz,这样对应的时钟周期就为20ns(当然其他的频率也可以,这里以50MHz时钟来举例);
(2)不限制开发板类型、IDE类型;
(3)实现目的:按照图示完成对应的功能即LED按照“点亮0.25秒→熄灭0.5秒→亮0.75秒→熄灭1秒”的规律,持续循环;
(4)对应的时序示意图如下:
设置基本时间单位:
设置基本时间计数循环:
对应的code:
module led_ctrl_3(
input wire clk,
input wire rst_n,
output reg led
);
parameter CNT_MAX = 26'd12_500_000 - 1;
//为什么要减1,大家可以思考一下从0到9是几个数,如果认为是9个的那么可以放弃学习FPGA了
//完成1秒的计数周期
reg [25:0] counter0;
always @(posedge clk or negedge rst_n)
if( rst_n == 1'b0 )
counter0 <= 26'b0;
else if( counter0 == CNT_MAX )
counter0 <= 26'b0;
else
counter0 <= counter0 + 1'b1;
reg [3:0] counter1;
always @(posedge clk or negedge rst_n)
if( rst_n == 1'b0 )
counter1 <= 4'b0;
else if( (counter0 == CNT_MAX)&&(counter1 == 4'd10) )
counter1 <= 4'b0;
else if( counter0 == CNT_MAX )
counter1 <= counter1 + 1'b1;
else
counter1 <= counter1;
always @(posedge clk or negedge rst_n)begin
if( rst_n == 1'b0 )begin
led <= 1'b1;
end
else begin
case( counter1 )
0 : led <= 1'b0;
1 : led <= 1'b1;
2 : led <= 1'b1;
3 : led <= 1'b0;
4 : led <= 1'b0;
5 : led <= 1'b0;
6 : led <= 1'b1;
7 : led <= 1'b1;
8 : led <= 1'b1;
9 : led <= 1'b1;
default : led <= led;
endcase
end
end
endmodule
仿真代码:
`timescale 1ns/1ns
module tb_led_ctrl_3();
//****************** Parameter and Internal Signal *******************//
//wire define
wire led;
//reg define
reg clk;
reg rst_n;
//***************************** Main Code ****************************//
initial begin
clk = 1'b1;
rst_n <= 1'b0;
#201
rst_n <= 1'b1;
#2_000_000;
$stop;
end
//参数重定义缩短仿真时间(大家的时间很宝贵,尽量不要浪费)
defparam led_ctrl_3_inst.CNT_MAX = 26'd1_250 - 1;
// creator clk
always #10 clk <= ~clk;
//*************************** Instantiation **************************//
led_ctrl_3 led_ctrl_3_inst
(
.clk ( clk ),
.rst_n ( rst_n ),
.led ( led )
);
endmodule
对应的仿真图:
更多回帖