EGO1—通用按键

使用软件: Vivado
开发板: EGO1采用Xilinx Artix-7系列XC7A35T-1CSG324C FPGA

功能描述及分析

五个按键,S4—S0,按下按键,led灯亮,数码管显示一个数字,松开按键,led灯灭,数码管不变。

1. 五个通用按键,默认为低电平,按键按下时输出高电平。

EGO1—通用按键_第1张图片
按键管脚约束如下:
EGO1—通用按键_第2张图片

2.按键消抖

去 抖的设计是:声明3 个寄存器btn0 、btn1 、btn2 ,并将它们组合成移位寄存器,将移位方向定义 为btn0->btn1->btn2
EGO1—通用按键_第3张图片
将 按键输入送btn0 。每隔20ms 执行 一次移位 。
在每20ms 后 ,btn0 中存储的是当前的按键电平,btn1 中存储的是20ms 之前的按键电平,btn2 中存储的是40ms 之前的按键。

assign btn_out=(btn2&btn1&~btn0)|(btn2&btn1&btn0 |(~btn2&btn1&btn0);

注:btn,btn0,btn1,btn2都是5位的寄存器,因为有5个按键,可以一起处理。

代码实现

1. verilog 代码

顶层文件:

`timescale 1ns / 1ps
//
// Create Date: 2022/10/03 16:55:27
// Design Name: 
// Module Name: v1
// Revision 0.01 - File Created
// Additional Comments:
// 
//

//顶层文件
module v1(
    input clk,
    input[4:0] btn,//不能只在v_ajxd里面有,否则约束文件中的btn值不能传到v_ajxd
    output[4:0] led,
    output[7:0] seg,
    output[7:0] seg1,
    output[7:0] an
    );
    wire[4:0] btnout;
    reg[4:0] led=5'b00000;
    reg[15:0] showdat=0;
   
    v_smg a(
        .clk(clk),
        .show_data(showdat),
        .seg(seg),
        .seg1(seg1),
        .an(an)
    );
    divclk my_divclk(.clk(clk),.clk_ms(clk_ms),.btnclk(clk_20ms));//调用分频模块
    reg[3:0] i=0;
    always@(posedge clk_ms)
    begin
        led<=btnout[4:0];
        for(i=0;i<=4;i=i+1)
        if(btnout[i]==1)
            showdat<=i;
    end
    v_ajxd uut_ajxd(//按键消抖
        .clk(clk_ms),
        .btn_clk(clk_20ms),
        .btn(btn),
        .btn_out(btnout),
        .ledtest1(ledtest1),
        .ledtest2(ledtest2),
        .ledtest3(ledtest3)
    );

endmodule

按键消抖:

`timescale 1ns / 1ps
//
// Create Date: 2022/10/03 16:35:27
// Design Name: 
// Module Name: v_ajxd
// Revision 0.01 - File Created
// Additional Comments:
// 
//

//按键消抖模块
module v_ajxd(
    input clk,
    input btn_clk,
    input[4:0] btn,//5个按键
    output[4:0] btn_out,
    output ledtest1,
    output ledtest2,
    output ledtest3
    );

    reg[4:0] btn0=0;
    reg[4:0] btn1=0;
    reg[4:0] btn2=0;
    reg ledtest1;
    reg ledtest2;
    reg ledtest3=1'b1;
    assign btn_out=(btn2&btn1&~btn0)|(btn2&btn1&btn0)|(~btn2&btn1&btn0);
    always@(posedge btn_clk)
    begin
        btn0<=btn;
        btn1<=btn0;
        btn2<=btn1;
    end
endmodule

分频

`timescale 1ns / 1ps
//
// Create Date: 2022/10/03 16:10:47
// Design Name: 
// Module Name: divclk
// Revision 0.01 - File Created
// Additional Comments:
// 
//

//输出 1ms、20ms周期的时钟信号
module divclk(clk,clk_ms,btnclk);
input clk;
output clk_ms,btnclk;
    reg[31:0] cnt1 = 0;
    reg[31:0] btnclk_cnt = 0;
    reg clk_ms = 0;
    reg btnclk = 0;
    //产生1ms时钟
    always@ (posedge clk)//系统时钟分频 100M/1000=100000; 1000HZ
    begin
        if(cnt1==26'd50000)
        begin
            clk_ms=~clk_ms;
            cnt1=0;
        end
        else
            cnt1=cnt1+1'b1;
    end
    always@ (posedge clk)//20MS   100M/50 = 2000 000    50HZ
    begin
        if(btnclk_cnt==1000000)begin
        btnclk=~btnclk;
        btnclk_cnt=0;
        end
        else btnclk_cnt=btnclk_cnt+1'b1;
    end
endmodule

数码管

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2022/10/04 20:23:10
// Design Name: 
// Module Name: v_smg
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module v_smg(
input clk,
    input[19:0] show_data, //开关
    output[7:0] seg,//段选,高有效
    output[7:0] seg1,
    output[7:0] an //位选,低有效
);
    reg[18:0] divclk_cnt = 0;//分频计数器
    reg divclk = 0;//分频后的时钟
    reg[7:0] seg=0;//段码
    reg[7:0] seg1=0;
    reg[7:0] an=8'b00000001;//位码
    reg[3:0] disp_dat=0;//要显示的数据
    reg[2:0] disp_bit=0;//要显示的位
    parameter maxcnt = 50000;// 周期:50000*2/100M
    always@(posedge clk)
    begin
        if(divclk_cnt==maxcnt)
        begin
            divclk=~divclk;
            divclk_cnt=0;
        end
        else
        begin
            divclk_cnt=divclk_cnt+1'b1;
        end
    end
    always@(posedge divclk) begin
        if(disp_bit >= 4)
            disp_bit=0;
         else
            disp_bit=disp_bit+1'b1;
         case (disp_bit)
            3'b000 :
            begin
                disp_dat=show_data[3:0];
                an=8'b00000001;//显示第一个数码管,高电平有效
            end
            3'b001 :
            begin
                disp_dat=show_data[7:4];
                an=8'b00000010;//显示第二个数码管,低电平有效
            end
            3'b010 :
            begin
                disp_dat=show_data[11:8];
                an=8'b00000100;//显示第三个数码管,低电平有效
            end
            3'b011 :
            begin
                disp_dat=show_data[15:12];
                an=8'b00001000;//显示第四个数码管,低电平有效
            end
            3'b100 :
            begin
                disp_dat=show_data[19:16];
                an=8'b00010000;//显示第五个数码管,低电平有效
            end
            default:
            begin
                disp_dat=0;
                an=8'b00000000;
            end
        endcase
    end
    always@(disp_dat)
    begin
        if(an > 8'b00001000) begin
            case (disp_dat)
            //显示0-F
            4'h0 : seg = 8'hfc;
            4'h1 : seg = 8'h60;
            4'h2 : seg = 8'hda;
            4'h3 : seg = 8'hf2;
            4'h4 : seg = 8'h66;
            4'h5 : seg = 8'hb6;
            4'h6 : seg = 8'hbe;
            4'h7 : seg = 8'he0;
            4'h8 : seg = 8'hfe;
            4'h9 : seg = 8'hf6;
            4'ha : seg = 8'hee;
            4'hb : seg = 8'h3e;
            4'hc : seg = 8'h9c;
            4'hd : seg = 8'h7a;
            4'he : seg = 8'h9e;
            4'hf : seg = 8'h8e;
            endcase
        end
        else begin
            case (disp_dat)
            //显示0-F
            4'h0 : seg1 = 8'hfc;
            4'h1 : seg1 = 8'h60;
            4'h2 : seg1 = 8'hda;
            4'h3 : seg1 = 8'hf2;
            4'h4 : seg1 = 8'h66;
            4'h5 : seg1 = 8'hb6;
            4'h6 : seg1 = 8'hbe;
            4'h7 : seg1 = 8'he0;
            4'h8 : seg1 = 8'hfe;
            4'h9 : seg1 = 8'hf6;
            4'ha : seg1 = 8'hee;
            4'hb : seg1 = 8'h3e;
            4'hc : seg1 = 8'h9c;
            4'hd : seg1 = 8'h7a;
            4'he : seg1 = 8'h9e;
            4'hf : seg1 = 8'h8e;
            endcase
        end
    end
endmodule

约束文件

## clk
set_property PACKAGE_PIN P17 [get_ports clk]
set_property IOSTANDARD LVCMOS33 [get_ports clk]
## switch


##smg位码
set_property PACKAGE_PIN G2 [get_ports an[7]]
set_property IOSTANDARD LVCMOS33 [get_ports an[7]]
set_property PACKAGE_PIN C2 [get_ports an[6]]
set_property IOSTANDARD LVCMOS33 [get_ports an[6]]
set_property PACKAGE_PIN C1 [get_ports an[5]]
set_property IOSTANDARD LVCMOS33 [get_ports an[5]]
set_property PACKAGE_PIN H1 [get_ports an[4]]
set_property IOSTANDARD LVCMOS33 [get_ports an[4]]
set_property PACKAGE_PIN G1 [get_ports an[3]]
set_property IOSTANDARD LVCMOS33 [get_ports an[3]]
set_property PACKAGE_PIN F1 [get_ports an[2]]
set_property IOSTANDARD LVCMOS33 [get_ports an[2]]
set_property PACKAGE_PIN E1 [get_ports an[1]]
set_property IOSTANDARD LVCMOS33 [get_ports an[1]]
set_property PACKAGE_PIN G6 [get_ports an[0]]
set_property IOSTANDARD LVCMOS33 [get_ports an[0]]

## 段码
set_property PACKAGE_PIN B4 [get_ports {seg[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[7]}]
set_property PACKAGE_PIN A4 [get_ports {seg[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[6]}]
set_property PACKAGE_PIN A3 [get_ports {seg[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[5]}]
set_property PACKAGE_PIN B1 [get_ports {seg[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[4]}]
set_property PACKAGE_PIN A1 [get_ports {seg[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[3]}]
set_property PACKAGE_PIN B3 [get_ports {seg[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[2]}]
set_property PACKAGE_PIN B2 [get_ports {seg[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[1]}]
set_property PACKAGE_PIN D5 [get_ports {seg[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[0]}]

# 段码2
set_property PACKAGE_PIN D4 [get_ports {seg1[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[7]}]
set_property PACKAGE_PIN E3 [get_ports {seg1[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[6]}]
set_property PACKAGE_PIN D3 [get_ports {seg1[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[5]}]
set_property PACKAGE_PIN F4 [get_ports {seg1[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[4]}]
set_property PACKAGE_PIN F3 [get_ports {seg1[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[3]}]
set_property PACKAGE_PIN E2 [get_ports {seg1[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[2]}]
set_property PACKAGE_PIN D2 [get_ports {seg1[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[1]}]
set_property PACKAGE_PIN H2 [get_ports {seg1[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[0]}]

## led
set_property PACKAGE_PIN F6 [get_ports {led[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[4]}]
set_property PACKAGE_PIN G4 [get_ports {led[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[3]}]
set_property PACKAGE_PIN G3 [get_ports {led[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[2]}]
set_property PACKAGE_PIN J4 [get_ports {led[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[1]}]
set_property PACKAGE_PIN H4 [get_ports {led[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[0]}]

##按键
set_property PACKAGE_PIN V1 [get_ports {btn[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {btn[4]}]
set_property PACKAGE_PIN U4 [get_ports {btn[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {btn[3]}]
set_property PACKAGE_PIN R11 [get_ports {btn[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {btn[2]}]
set_property PACKAGE_PIN R17 [get_ports {btn[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {btn[1]}]
set_property PACKAGE_PIN R15 [get_ports {btn[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {btn[0]}]


## clk
set_property PACKAGE_PIN P17 [get_ports clk]
set_property IOSTANDARD LVCMOS33 [get_ports clk]

##smg位码
set_property PACKAGE_PIN G2 [get_ports an[7]]
set_property IOSTANDARD LVCMOS33 [get_ports an[7]]
set_property PACKAGE_PIN C2 [get_ports an[6]]
set_property IOSTANDARD LVCMOS33 [get_ports an[6]]
set_property PACKAGE_PIN C1 [get_ports an[5]]
set_property IOSTANDARD LVCMOS33 [get_ports an[5]]
set_property PACKAGE_PIN H1 [get_ports an[4]]
set_property IOSTANDARD LVCMOS33 [get_ports an[4]]
set_property PACKAGE_PIN G1 [get_ports an[3]]
set_property IOSTANDARD LVCMOS33 [get_ports an[3]]
set_property PACKAGE_PIN F1 [get_ports an[2]]
set_property IOSTANDARD LVCMOS33 [get_ports an[2]]
set_property PACKAGE_PIN E1 [get_ports an[1]]
set_property IOSTANDARD LVCMOS33 [get_ports an[1]]
set_property PACKAGE_PIN G6 [get_ports an[0]]
set_property IOSTANDARD LVCMOS33 [get_ports an[0]]

## 段码
set_property PACKAGE_PIN B4 [get_ports {seg[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[7]}]
set_property PACKAGE_PIN A4 [get_ports {seg[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[6]}]
set_property PACKAGE_PIN A3 [get_ports {seg[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[5]}]
set_property PACKAGE_PIN B1 [get_ports {seg[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[4]}]
set_property PACKAGE_PIN A1 [get_ports {seg[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[3]}]
set_property PACKAGE_PIN B3 [get_ports {seg[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[2]}]
set_property PACKAGE_PIN B2 [get_ports {seg[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[1]}]
set_property PACKAGE_PIN D5 [get_ports {seg[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg[0]}]

# 段码2
set_property PACKAGE_PIN D4 [get_ports {seg1[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[7]}]
set_property PACKAGE_PIN E3 [get_ports {seg1[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[6]}]
set_property PACKAGE_PIN D3 [get_ports {seg1[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[5]}]
set_property PACKAGE_PIN F4 [get_ports {seg1[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[4]}]
set_property PACKAGE_PIN F3 [get_ports {seg1[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[3]}]
set_property PACKAGE_PIN E2 [get_ports {seg1[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[2]}]
set_property PACKAGE_PIN D2 [get_ports {seg1[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[1]}]
set_property PACKAGE_PIN H2 [get_ports {seg1[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg1[0]}]

结果

按下最左边按键,led灯亮,数码管显示:4
EGO1—通用按键_第4张图片

你可能感兴趣的:(FPGA,fpga开发)