数字系统设计练习(三)—— 多文件多模块练习

一、实验硬软件环境:

  1. EDA软件:Vivado2019
  2. 实验开发板:Basys3 FPGA套件

二、实验内容:

  实现如下电路功能:当开关开启,灯泡按1s的间隔闪烁;开关关闭,则灯泡关闭。使用实验板上的按键模拟开关,led模拟灯泡,时钟使用实验板提供的clk。参考实验板的管脚文件编写约束文件。

  要求使用多文件、多模块描述,即一个模块一个Verilog源文件,顶层模块完成子模块间互连。子模块包括计数器模块,灯泡控制模块等,下图给出了一个模块划分的参考。

  注意,LED部分的电路图如左图所示,当FPGA输出为高电平时,相应的LED点亮;否则,LED熄灭。拨码开关的电路如右图所示,当开关打到下档时,表示FPGA的输入为低电平。

                         

三、具体实现

  1. 思路分析:开发板的晶振为100Mhz,则其周期为10ns。
    • led_top.v:顶层模块,负责将子模块互连;
    • counter.v:计时器,从1自加到100,000,000,计时1S;
    • led_gen:led小灯控制模块
  2. 设计源码:
    • counter.v:
       1 `timescale 1ns / 1ps
       2 //////////////////////////////////////////////////////////////////////////////////
       3 // Company: 
       4 // Engineer: 
       5 // 
       6 // Create Date: 2020/08/16 10:49:00
       7 // Design Name: 
       8 // Module Name: counter
       9 // Project Name: 
      10 // Target Devices: 
      11 // Tool Versions: 
      12 // Description: 
      13 // 
      14 // Dependencies: 
      15 // 
      16 // Revision:
      17 // Revision 0.01 - File Created
      18 // Additional Comments:
      19 // 
      20 //////////////////////////////////////////////////////////////////////////////////
      21 
      22 
      23 module counter(switch, clk,count);
      24     input switch, clk;
      25     output reg [28:0] count;
      26     
      27     always @(posedge clk)
      28     begin
      29         if(!switch)
      30             count <= 29'd0;
      31         else
      32             begin
      33                 if(count == 29'd100_000_000)
      34                     count <= 0;
      35                 else
      36                     count <= count + 1;
      37             end
      38     end
      39 endmodule
      View Code
    • led_gen.v:
       1 `timescale 1ns / 1ps
       2 //////////////////////////////////////////////////////////////////////////////////
       3 // Company: 
       4 // Engineer: 
       5 // 
       6 // Create Date: 2020/08/16 10:49:00
       7 // Design Name: 
       8 // Module Name: led_gen
       9 // Project Name: 
      10 // Target Devices: 
      11 // Tool Versions: 
      12 // Description: 
      13 // 
      14 // Dependencies: 
      15 // 
      16 // Revision:
      17 // Revision 0.01 - File Created
      18 // Additional Comments:
      19 // 
      20 //////////////////////////////////////////////////////////////////////////////////
      21 
      22 
      23 module led_gen(switch, clk, count, led);
      24     input switch, clk;
      25     input [28:0] count;
      26     output reg led;
      27     
      28     always @(posedge clk)
      29     begin
      30         if(!switch)
      31             led <= 0;
      32         else
      33             begin
      34                 if(count == 29'd100_000_000)
      35                     led <= ~led;
      36                 else
      37                     led <= led;
      38             end
      39     end
      40 endmodule
      View Code
    • led_top.v:
       1 `timescale 1ns / 1ps
       2 //////////////////////////////////////////////////////////////////////////////////
       3 // Company: 
       4 // Engineer: 
       5 // 
       6 // Create Date: 2020/08/16 10:49:00
       7 // Design Name: 
       8 // Module Name: led_top
       9 // Project Name: 
      10 // Target Devices: 
      11 // Tool Versions: 
      12 // Description: 
      13 // 
      14 // Dependencies: 
      15 // 
      16 // Revision:
      17 // Revision 0.01 - File Created
      18 // Additional Comments:
      19 // 
      20 //////////////////////////////////////////////////////////////////////////////////
      21 
      22 
      23 module led_top(switch, clk, led);
      24     input switch, clk;
      25     output led;
      26     
      27     wire [28:0] count;
      28     
      29     counter u_cnt(.switch(switch), .clk(clk), .count(count));
      30     led_gen u_led_gen(.switch(switch), .clk(clk), .count(count), .led(led));
      31     
      32 endmodule
      View Code
  3. 仿真代码:(为了便于仿真,将count的上限值调整为100,而非100,000,000。)
     1 `timescale 1ns / 1ps
     2 //////////////////////////////////////////////////////////////////////////////////
     3 // Company: 
     4 // Engineer: 
     5 // 
     6 // Create Date: 2020/08/16 11:30:02
     7 // Design Name: 
     8 // Module Name: led_top_tb
     9 // Project Name: 
    10 // Target Devices: 
    11 // Tool Versions: 
    12 // Description: 
    13 // 
    14 // Dependencies: 
    15 // 
    16 // Revision:
    17 // Revision 0.01 - File Created
    18 // Additional Comments:
    19 // 
    20 //////////////////////////////////////////////////////////////////////////////////
    21 
    22 
    23 module led_top_tb;
    24     reg switch;
    25     reg clk;
    26     wire led;
    27     
    28     led_top uut(.switch(switch), .clk(clk), .led(led));
    29     
    30     always #10 clk = ~clk;
    31     
    32     initial
    33     begin
    34         switch = 0;
    35         clk = 0;
    36         
    37         #100 switch = 1;
    38         
    39         #100 $stop;
    40     end
    41 
    42 endmodule
    View Code
  4. 波形图:当count为100(十六进制为64)时,led状态转变

  5. 约束文件:
    1 set_property PACKAGE_PIN V17 [get_ports switch]
    2 set_property IOSTANDARD LVCMOS33 [get_ports switch]
    3 set_property PACKAGE_PIN U16 [get_ports led]
    4 set_property IOSTANDARD LVCMOS33 [get_ports led]
    5 set_property PACKAGE_PIN W5 [get_ports clk]
    6 set_property IOSTANDARD LVCMOS33 [get_ports clk]
    View Code
原文地址:https://www.cnblogs.com/mantha/p/13633335.html