基于FPGA使用Verilog编写驱动数码管单个显示例程

基于FPGA使用Verilog编写驱动数码管单个显示例程

硬件使用类似说明

  1. FPGA使用国产GW的YANG NANO 9K开发板
  2. 数码管使用四位共阳数码管在这里插入图片描述
    图中1-4数字代表数码管的段选,a-g为单个段的位选择,h表示数码管的小数点。
  3. 复位按键在开发板上所定义

共阳数码管显示1-F所对应的段选码表:

{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //共阳数码管  1- F  无小数点
{0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78, 0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};共阳数码管段选码表,有小数点
 

共阴数码管

{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴数码管段选表,无小数点:
{0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};共阴数码管段选表,有小数点:

HDL顶层测试程序

//四位数码管显示实验

module LEDS_Top (
        input  clk, //27Mhz  计数一个时间 = 1/27*10^6(s) = 37ns  1s计数个数 27,027,027
        input  rest,
        output reg [7:0] out_IO
    );

    parameter Delay_1s = 27_027_027;
    reg [24:0] count;
    reg [3:0] out_7IO;
	
    always @(posedge clk) begin
        if (rest == 0)
            count <= 0;
        else if(count != Delay_1s-1)
            count <=count+25'd1;
        else
            count<=0;
    end
//共阳数码管  {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
    always @(posedge clk) begin
        if (rest == 0)
            out_7IO <= 0;
        else if(count == Delay_1s-1)
            out_7IO <= out_7IO+4'd1;
        else if(out_7IO == 4'd10) begin
            out_7IO<=0;
            
        end

        else begin
            case (out_7IO)
                4'd0:
                    out_IO<= 8'hc0;
                4'd1:
                    out_IO<= 8'hF9;
                4'd2:
                    out_IO<= 8'hA4;
                4'd3:
                    out_IO<= 8'hB0;
                4'd4:
                    out_IO<= 8'h99;
                4'd5:
                    out_IO<= 8'h92;
                4'd6:
                    out_IO<= 8'h82;
                4'd7:
                    out_IO<= 8'hF8;
                4'd8:
                    out_IO<= 8'h80;
                4'd9:
                    out_IO<= 8'h90;
                default:
                    out_IO<= 8'h0;

            endcase
        end
    end
endmodule

源码说明:

    input  clk, //27Mhz  计数一个时间 = 1/27*10^6(s) = 37ns  1s计数个数 27,027,027
    input  rest,//复位按键
    output [7:0] out_IO//输出引脚

Testbench测试文件

`timescale 1ns/1ns

module LEDS_Top_tb ();

    reg clk;
    reg rest;

    wire [6:0] out_IO;

    initial begin
        clk =0;
        rest =0;
        #20;
        rest = 0;
        #100;
        rest = 1 ;

    end

    LEDS_Top #(
                .Delay_1s(500)
            )LEDS_Top_Init(
                .clk(clk), //27Mhz  计数一个时间 = 1/27*10^6(s) = 37ns  1s计数个数 27,027,027
                .rest(rest),
                .out_IO(out_IO)
                 //output [3:0] cs;
            );

    always #10 clk = ~clk;


//随机产生数   {$random}%2 0-1的数

endmodule

通过 Modelsim 查看测试波形是否正确

在这里插入图片描述

最后说明一下

此程序实现单为数码管每一秒计数器加一,到9自动再重0开始计数,以此往复循环

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个简单的Verilog代码示例,用于在数码管上动态显示时间。以下是代码示例: ``` module time_display( input clk, input reset, output reg [6:0] seg, output reg [3:0] an ); reg [25:0] count = 0; reg [3:0] digit = 0; always @(posedge clk) begin if (reset) begin count <= 0; digit <= 0; end else begin count <= count + 1; if (count == 50000000) begin // 每秒钟更新一次时间 count <= 0; digit <= digit + 1; if (digit == 4) begin // 当前时间为4位数,重置为0 digit <= 0; end end end end always @(posedge clk) begin case (digit) // 根据不同的位数,设置不同的数码管输出 0: begin seg <= 7'b1000000; // 数码管显示时钟的小时位 an <= 4'b1110; // 数码管选择第1位 end 1: begin seg <= 7'b0100000; // 数码管显示时钟的小时位 an <= 4'b1101; // 数码管选择第2位 end 2: begin seg <= 7'b0010000; // 数码管显示时钟的分钟位 an <= 4'b1011; // 数码管选择第3位 end 3: begin seg <= 7'b0001000; // 数码管显示时钟的分钟位 an <= 4'b0111; // 数码管选择第4位 end default: begin seg <= 7'b1111111; // 数码管显示任何数字 an <= 4'b1111; // 数码管不选择任何位 end endcase end endmodule ``` 该代码使用一个时钟信号(`clk`)和一个重置信号(`reset`)来更新数码管上的时间显示。每当时钟信号上升沿到来时,计数器(`count`)会递增。当计数器达到50000000时(即1秒钟),位数计数器(`digit`)会递增,从而更新数码管上的显示。 在数码管显示时间的方法是,在第一位数码管显示小时数的十位(`seg = 7'b1000000`),在第二位数码管显示小时数的个位(`seg = 7'b0100000`),在第三位数码管显示分钟数的十位(`seg = 7'b0010000`),在第四位数码管显示分钟数的个位(`seg = 7'b0001000`)。同时,位数计数器(`digit`)用于选择要显示数码管位数(`an`)。例如,当`digit`为0时,选择第一位数码管(`an = 4'b1110`),以此类推。 请注意,此代码仅用于演示目的,并不完整或可用于实际应用,您需要根据实际需求进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值