FPGA学习笔记2.2——用Verilog实现七段管的工作逻辑

目录

设计思路:

原理图:

Task下的调用:

功能模块代码: 

测试模块代码:

运行图: 

Module下的调用:

功能模块代码:

测试模块代码:

运行图: 

设计思路:

一个七段管使用7bit信号控制7段LED灯的亮灭。若每段LED灯赋值为0,灯亮;赋值为1时灯灭。则Hex=7‘b1111001,其中hex[6]—hex[5]…..hex[0]分别对应下图1中七段管中的编号是6543210的信号。那么该信号hex[6]=1  hex[5]=1  hex[4]=1  hex[3]=1  hex[2]=0  hex[1]=0   hex[0]=1。

原理图:

Task下的调用:

功能模块代码: 

module BCDto7(in,out1,out2,out3);
    input [7:0] in;      // 8路输入,高电平1为有效电平
    output reg[6:0] out1; //个位的数码管
    output reg[6:0] out2; // 十位的数码管
    output reg[6:0] out3; // 百位的数码管
    wire[3:0] bai;  
    wire[3:0] shi;  
    wire[3:0] ge;   
    
    assign ge=in%10;                     // 个位赋值
    assign shi=(in/10)%10;                // 十位赋值
    assign bai=in/100;                // 百位赋值
    
    always@(in)
        begin
            if(in<10)        
                begin
                    show(ge,out1);     // 第一个七段管 显示个位
                    out2=7'b1111111;        // 第二个七段管不显示
                    out3=7'b1111111;        // 第三个七段管不显示
                end
            else if(in<100)      
                begin
                    show(ge,out1);     // 第一个七段管 显示个位
                    show(shi,out2);     // 第二个七段管 显示十位
                    out3=7'b1111111;        // 第三个七段管不显示
                end
            else                
                begin
                    show(ge,out1);     // 第一个七段管 显示个位
                    show(shi,out2);     // 第二个七段管 显示十位
                    show(bai,out3); // 第三个七段管 显示百位
                end
        end
    
    task show;
        input integer decc;                 // 输入,十进制数
        output reg[6:0] outt;               // 输出,7位二进制数值
		  case(decc)
        4'd0: outt=7'b1000000;   // 七段管显示0
        4'd1: outt=7'b1111001;   // 七段管显示1
        4'd2: outt=7'b0100100;   // 七段管显示2
        4'd3: outt=7'b0110000;   // 七段管显示3
        4'd4: outt=7'b0011001;   // 七段管显示4
        4'd5: outt=7'b0010010;   // 七段管显示5
        4'd6: outt=7'b0000010;   // 七段管显示6
        4'd7: outt=7'b1111000;   // 七段管显示7
        4'd8: outt=7'b0000000;   // 七段管显示8
        4'd9: outt=7'b0011000;   // 七段管显示9
		  default:outt=7'b1111111; // 七段管不显示
		  endcase
    endtask
    
endmodule

测试模块代码:

// Copyright (C) 2017  Intel Corporation. All rights reserved.
// Your use of Intel Corporation's design tools, logic functions 
// and other software and tools, and its AMPP partner logic 
// functions, and any output files from any of the foregoing 
// (including device programming or simulation files), and any 
// associated documentation or information are expressly subject 
// to the terms and conditions of the Intel Program License 
// Subscription Agreement, the Intel Quartus Prime License Agreement,
// the Intel FPGA IP License Agreement, or other applicable license
// agreement, including, without limitation, that your use is for
// the sole purpose of programming logic devices manufactured by
// Intel and sold by Intel or its authorized distributors.  Please
// refer to the applicable agreement for further details.

// *****************************************************************************
// This file contains a Verilog test bench template that is freely editable to  
// suit user's needs .Comments are provided in each section to help the user    
// fill out necessary details.                                                  
// *****************************************************************************
// Generated on "03/31/2022 11:37:13"
                                                                                
// Verilog Test Bench template for design : BCDto7
// 
// Simulation tool : ModelSim-Altera (Verilog)
// 

`timescale 1 ps/ 1 ps
module BCDto7_vlg_tst();
// constants                                           
// general purpose registers
reg eachvec;
// test vector input registers
reg [7:0] in;
// wires                                               
wire [6:0]  out1;
wire [6:0]  out2;
wire [6:0]  out3;
wire[3:0] bai;  
wire[3:0] shi;  
wire[3:0] ge;   

// assign statements (if any)                          
BCDto7 i1 (
// port map - connection between master ports and signals/registers   
	.in(in),
	.out1(out1),
	.out2(out2),
	.out3(out3)
);
initial                                                
begin                                                  
// code that executes only once                        
// insert code here --> begin                          
                                                       
// --> end                                             
   
$display("Running testbench"); 
for(in=8'b00000000;in<=8'b11111111;in=in+1)
begin
#1;
end           

                
end    
initial
begin
$monitor($realtime,,,"input:%b; output:%b %b %b",in,out3,out2,out1);  
#256 $stop;   
end                                              
always                                                 
// optional sensitivity list                           
// @(event1 or event2 or .... eventn)                  
begin                                                  
// code executes for every event on sensitivity list   
// insert code here --> begin                          
                                                       
@eachvec;                                              
// --> end                                             
end                                                    
endmodule

运行图: 

循环测试所有情况

Module下的调用:

功能模块代码:

module bcdyyf(in8,out71,out72,out73);
input[7:0] in8; 
output [6:0] out71,out72,out73;
reg [3:0] a1,a2,a3;
always@(*)
begin
	a1=in8/100;            //百位数
	a2=(in8-(100*a1))/10;  //十位数
	a3=in8%10;             //个位数
end
bcd accbcd1(a1,out71);    //模块调用注意位置即可(不注意容易出问题,不能放在always前,也不可放在begin end内部)
bcd accbcd2(a2,out72);
bcd accbcd3(a3,out73);
endmodule

module bcd(tt,out7);
   input [3:0] tt;
	output reg [6:0] out7;
always@(*)
begin
   case(tt)                 //低电平有效
	4'b0000:out7=7'b1000000;
	4'b0001:out7=7'b1111001;
	4'b0010:out7=7'b0100100;
	4'b0011:out7=7'b0110000;
	4'b0100:out7=7'b0011001;
	4'b0101:out7=7'b0010010;
	4'b0110:out7=7'b0000010;
	4'b0111:out7=7'b1111000;
	4'b1000:out7=7'b0000000;
	4'b1001:out7=7'b0010000;
default:out7=7'b0001001;   //其他情况显示H
	endcase
end
endmodule

测试模块代码:

`timescale 1 ps/ 1 ps
module bcdyyf_vlg_tst();
reg eachvec;
reg [7:0] in8;                                           
wire [6:0]  out71;
wire [6:0]  out72;
wire [6:0]  out73;                     
bcdyyf i1 ( 
	.in8(in8),
	.out71(out71),
	.out72(out72),
	.out73(out73)
);
initial                                                
begin                                                                                        
$display("Running testbench");    
for(in8=8'b00000000;in8<=8'b11111111;in8=in8+1)
begin
#1;
end                           
end  
initial
begin
$monitor($realtime,,,"input:%b; output:%b %b %b",in8,out71,out72,out72);  
#256 $stop;   
end                                                                                                           
endmodule

运行图: 

本次实验主要是Module的调用注意他的位置,极其容易出差错(就是因为一开始不会module模块调用,就走了task的调用)。


版权声明:本文为yyfloveqcw原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。