【FPGA】DE2-115实现LED流水灯与VScode的安装与使用
目录
一、通过Verilog编程完成LED流水灯
1.1新建工程
1.2添加Verilog文件
1.3引脚分配
1.4烧录
二、VScode下载与插件安装
2.1Vscode的安装
2.2Vscode插件安装
三、VScode层次化设计流水灯
3.1在VScode中进行代码的编写
3.2在QUartus中进行代码编译
一、通过Verilog编程完成LED流水灯
1.1新建工程
打开QUartus并新建一个工程,选择对应的存放位置,并填写工程名和顶层实体名。
一直NExt到如图界面
选择对应的芯片型号
然后finish
1.2添加Verilog文件
新建两个Verilog HDL File文件,分别编写top.v文件和led_test.v文件(模块名称需和文件名称一致,否则编译时可能出现报错)
led_test.v
module led_test(
input sys_clk,
input sys_rst_n,
output reg [7:0] led
);
// 计数器参数,假设sys_clk为50MHz
localparam TIME_1S = 50_000_000; // 50MHz计数到1秒需要的计数
reg [25:0] cnt;
wire add_cnt;
wire end_cnt;
reg [2:0] cnt1;
always @(posedge sys_clk or negedge sys_rst_n) begin
if (!sys_rst_n) begin
cnt <= 26'b0;
end else if (add_cnt) begin
if (end_cnt) begin
cnt <= 26'b0;
end else begin
cnt <= cnt + 1'b1;
end
end else begin
cnt <= cnt;
end
end
// 异步复位
always @(posedge sys_clk or negedge sys_rst_n) begin
if (!sys_rst_n) begin
cnt1 <= 3'b0;
end else if (add_cnt) begin
if (end_cnt) begin
cnt1 <= 3'b0;
end else begin
cnt1 <= cnt1 + 1'b1;
end
end
end
always @(posedge sys_clk or negedge sys_rst_n) begin
if (!sys_rst_n) begin
led <= 8'b0;
end else begin
case (cnt1)
3'b000 : led <= 8'b00000001;
3'b001 : led <= 8'b00000010;
3'b010 : led <= 8'b00000100;
3'b011 : led <= 8'b00001000;
3'b100 : led <= 8'b00010000;
3'b101 : led <= 8'b00100000;
3'b110 : led <= 8'b01000000;
3'b111 : led <= 8'b10000000;
default: led <= led;
endcase
end
end
assign add_cnt = 1'b1;
assign end_cnt = add_cnt && cnt == (TIME_1S - 1);
endmodule
top.v文件
module top (
input wire sys_clk, // 时钟信号
input wire sys_rst_n, // 异步复位信号,低电平有效
output wire [7:0] led // LED输出信号
);
// 实例化LED流水灯模块
led_test u_led_test (
.sys_clk(sys_clk),
.sys_rst_n(sys_rst_n),
.led(led)
);
endmodule
然后我们设置top.v文件为顶层文件
1.3引脚分配
添加下述引脚
1.4烧录
点击Program
点击Hardware Setup
选择USB
点击添加文件,添加.sof文件
然后点击Star开始烧录
演示:
二、VScode下载与插件安装
2.1Vscode的安装
虽然可以使用Quartus Prime自带的编辑器编写Verilog代码,但VScode及其插件提供了更强大的代码编辑、导航、调试和项目管理功能,可以显著提高开发效率和代码质量。其可以帮我们识别Verilog代码中关键字、数据类型、注释等,并用不同颜色显示,使代码更任意阅读和理解。其次,它还提供了代码补全功能,帮助我们提高编码效率,同时还提供了基本的代码检查功能,帮助检查语法错误和编码问题。其插件还可以帮助我们快速跳转到代码中的函数定义、变量声明等位置,方便代码的浏览和编辑。
可以通过下面网址进行下载:Download Visual Studio Code - Mac, Linux, Windows
根据系统选择对应的版本进行安装。
2.2Vscode插件安装
下载完成后进行扩展包的安装,搜索Verilog-HDL/SystemVerilog插件并安装
我们还可以添加中文扩展包,便于我们的操作和使用
接下来进行进入设置,然后在搜索框中搜索Verilog,下滑找到框3,框4。在Verilog>Linting:Linter中选择modesim;在Verilog>Linting>Modesim:Work中输入自己电脑中存放modesim项目文件夹中的work文件夹的路径。
三、VScode层次化设计流水灯
3.1在VScode中进行代码的编写
在VScode中新建三个代码文件,分别时top顶层模块LedBlink.v、分频模块Fenpin.v和显示模块Display.v
LedBlink.v
module LedBlink(
input wire sys_clk, // 系统时钟信号
input wire sys_rst_n, // 系统复位信号,低电平有效
output wire [7:0] led // LED输出信号
);
wire clk_div; // 分频后的时钟信号
// 实例化分频模块
FenPin fen_pin_inst(
.clk(sys_clk),
.rst_n(sys_rst_n),
.clk_out(clk_div)
);
// 实例化显示模块
Display display_inst(
.clk(clk_div),
.rst_n(sys_rst_n),
.led(led)
);
endmodule
Fenpin.v
module FenPin(
input wire clk, // 输入时钟信号
input wire rst_n, // 异步复位信号,低电平有效
output reg clk_out // 分频后的时钟信号
);
localparam TIME_1S = 50_000_000; // 50MHz计数到1秒需要的计数
reg [25:0] counter = 26'd0;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
counter <= 26'd0;
clk_out <= 1'b0;
end else begin
if (counter == (TIME_1S - 1)) begin
counter <= 26'd0;
clk_out <= ~clk_out; // 翻转输出时钟
end else begin
counter <= counter + 1'b1;
end
end
end
endmodule
Display.v
module Display(
input wire clk, // 输入时钟信号
input wire rst_n, // 异步复位信号,低电平有效
output reg [7:0] led // LED输出信号
);
reg [2:0] counter = 3'd0;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
counter <= 3'd0;
led <= 8'b0;
end else begin
case (counter)
3'd0 : led <= 8'b00000001;
3'd1 : led <= 8'b00000010;
3'd2 : led <= 8'b00000100;
3'd3 : led <= 8'b00001000;
3'd4 : led <= 8'b00010000;
3'd5 : led <= 8'b00100000;
3'd6 : led <= 8'b01000000;
3'd7 : led <= 8'b10000000;
default: led <= 8'b0;
endcase
counter <= counter + 1'b1;
end
end
endmodule
3.2在QUartus中进行代码编译
我们按照前面的步骤进行工程的新建
当到了文件添加步骤,我们对应添加刚刚在VScode中编写的文件
然后我们将ledBlink.v文件设置为top文件
然后我们进行编译运行
感想:通过本次实验,进一步了解了关于FPGA相关的基础的led流水灯的编译与运行。同时通过VScode进行编码,让我进一步了解其可以帮我们检测代码、自动补全代码等便捷的功能。