FPGA——Multiplexer to realize button control LED light on and off


foreword

  • Software: Quartus Prime Standard 18.0
  • Simulation software: modelsim 10.5
  • Code writing software: VSCode
  • Waveform Drawing Tool: Visio 2013
  • Hardware: Altera MAX10 for little feet

Expected effect:
We hope to select the input LED signal through a button. When we press key3, that is, when key3 is a low signal, the LED selects Key2 as the LED signal, otherwise, it uses the key1 signal for reference.


1. Multiplexer

A multiplexer is another name for a data selector. In multiple data transmission, a circuit that can select any one of them according to needs is called a data selector, also known as a multiplexer or a multiplex switch. It is a simple combinational logic circuit.

  • Combination logic:

Combinational logic is actually an important part of VerilogHDL design. From the essence of the circuit, the characteristic of the combinational logic circuit is that the output signal is only a function of the input signal at the current moment, and has nothing to do with the input state at other times, and there is no storage circuit and no feedback circuit.

2. Draw module block diagram and waveform diagram

At the beginning of the project, create a new project file, which contains four new folders, namely:
prj (project file), rtl (.v file), sim (simulation test file), doc (waveform drawing file)

  • add shape
    insert image description here
    insert image description here

  • Block Diagram
    insert image description here

  • Waveform

When sel is 0, the Out output signal is in_2 waveform, otherwise it is in_1 waveform.

insert image description here

3. Verilog HDL code and test code

Create a mus2_1.v file in the rtl folder, and the defined module name needs to be consistent with the file name. At this point, double-click to open VSCode for code writing.

module mus2_1(
    input wire  in_1,
    input wire in_2,
    input wire sel,//选通信号
    output reg out
);
always @(*)//组合逻辑进行阻塞赋值
    if(sel == 1'b1)
    out=in_1;
    else
    out =in_2;

endmodule

Create a new test file tb_mus2_1.v in the sim folder

`timescale 1ns/1ns //时间尺度
module tb_mus2_1();
reg in_1;
reg in_2;
reg sel;
wire out;
initial begin //初始化变量,上电后只执行一次
    in_1<=1'b0;
    in_2<=1'b0;
    sel <=1'b0;
end  
//设置随机波形图
always #10 in_1<={
    
    $random}%2;
always #10 in_2<={
    
    $random}%2;
always #10 sel<={
    
    $random}%2;


mus2_1   mus2_1_inst  ( //实例化模块
     .in_1(in_1),
     .in_2(in_2),
     .sel(sel),//选通信号
     .out(out)
);
endmodule

4. Create a project

  • Open quartus, a new project

insert image description here

  • click next
    insert image description here

  • Select the project path and choose to go to the prj folder we created earlier. Fill in the project name, here try to choose the same name as our .v file—mus2_1.
    insert image description here

  • Here we don't choose a template, just next.
    insert image description here

  • We will add the file later, next first.

insert image description here

  • Select the board type. Here I am using Altera max10. If you are using other boards, you need to download the device library corresponding to the quartus version from Intel’s official website. Even if it is also max 10, if the version of quartus is different, the device library Not the same. requires attention.
    insert image description here
  • Select simulation software and language
    insert image description here
  • Check whether the configuration is correct, and then click Finish to complete the project creation.
    insert image description here
  • Add source and test files

insert image description here

  • double click
    insert image description here

  • Select file to add
    insert image description here

  • Add test files to simulation

insert image description here
insert image description here
insert image description here
insert image description here

Under 1 in the above figure, select the test file, and fill in the test file name in 2.

5. Simulation

Click below to automatically open the simulation software

insert image description here

  • Simulation Waveform

insert image description here

Explanation: When sel is 1 at the position of the marking line, the signal whose output level should be in_1 is 0, and out is also 0 at this time. The simulation was successful! The specific steps of how to perform the simulation will not be introduced here.

6. Board verification

1. Assign pins

Assign according to the pin assignment diagram provided by the manufacturer

insert image description here

  • set pin
    insert image description here

  • Fill in the corresponding pins in turn
    insert image description here

Here key1 controls the in_1 signal, key2 controls the in_2 signal, and key3 acts as a strobe signal. Initially, none of the three keys is pressed, key3 is at high level, and the output signal is key1. When key1 is pressed at this time, the low level light at key1 is on. Conversely, press key3, the strobe signal selects key2 signal, press key2 at this time, the light is on. To sum up, press key1 to turn on the light, or press key2+key3 to turn on the light.

2. Burning

insert image description here

  • Select USB

insert image description here

If the USB-Blaster is not detected here, there are two situations. One is that your data cable does not support data transmission, and the other is that your driver has not been updated. Search for an update to identify it.

  • Click Add File, and look for the sof file under the outputfiles folder under the prj folder
    insert image description here
  • Click start to start burning, and the progress bar is on the right
    insert image description here

Seven, effect demonstration

keyofLED


8. Summary

This is the first time to operate the fpga board in kind, and it feels great. Generally speaking, the operation this time is relatively simple, but by drawing the module block diagram and waveform diagram by myself, I have a strong understanding of the following simulation and code understanding. s help! Through this experiment, it should be regarded as entering the pit, haha!

Guess you like

Origin blog.csdn.net/qq_52215423/article/details/128006883