开发板环境:vivado2017.4
开发板:Zedboard 芯片型号:xc7z020clg484-1
本章主要使用用verilog编写一个按键检测程序,按一次按键LED亮一次,依次点亮八个LED灯
按键工程
按键检测代码
`timescale 1ns / 1ps
//
// Company:
// Engineer:
//
// Create Date: 2022/02/26 12:48:48
// Design Name:
// Module Name: key_test
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
module key_test(
input clock,//100M
input reset,//SW0
input key_in,//BTNC
output [7:0] led//LED0~LED7
);
reg key_in_r1;
reg key_in_r2;
wire key_in_flag;
reg key_out;
reg key_out_r1;
reg key_out_r2;
reg [19:0] cnt;
reg [7:0] led_reg;
wire key_out_flag;
always @ (posedge clock, negedge reset)
if(!reset)
key_in_r1 <= 1'b1;
else
key_in_r1 <= key_in;
always @ (posedge clock, negedge reset)
if(!reset)
key_in_r2 <= 1'b1;
else
key_in_r2 <= key_in_r1;
assign key_in_flag = (!key_in_r2 & key_in_r1);
always @ (posedge clock, negedge reset)
if(!reset)
cnt <= 20'h0;
else if(key_in_flag)
cnt <= 20'h0;
else
cnt <= cnt + 1'b1;
always @ (posedge clock, negedge reset)
if(!reset)
key_out <= 1'b1;
else if(cnt == 20'hfffff)
key_out <= key_in;
always @ (posedge clock, negedge reset)
if(!reset)
key_out_r1 <= 1'b1;
else
key_out_r1 <= key_out;
always @ (posedge clock, negedge reset)
if(!reset)
key_out_r2 <= 1'b1;
else
key_out_r2 <= key_out_r1;
assign key_out_flag = (!key_out_r2 && key_out_r1);
//点亮LED
always@(posedge clock or negedge reset)begin
if(!reset)
led_reg <= 8'b00000001;
else if(key_out_flag)begin
if(led_reg == 8'b10000000)
led_reg <= 8'b00000001;
else
led_reg <= led_reg<<1;
end
end
assign led = led_reg;
endmodule
按键检测约束文件
set_property PACKAGE_PIN T22 [get_ports {led[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[0]}]
set_property PACKAGE_PIN T21 [get_ports {led[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[1]}]
set_property PACKAGE_PIN U22 [get_ports {led[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[2]}]
set_property PACKAGE_PIN U21 [get_ports {led[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[3]}]
set_property PACKAGE_PIN V22 [get_ports {led[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[4]}]
set_property PACKAGE_PIN W22 [get_ports {led[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[5]}]
set_property PACKAGE_PIN U19 [get_ports {led[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[6]}]
set_property PACKAGE_PIN U14 [get_ports {led[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {led[7]}]
set_property PACKAGE_PIN Y9 [get_ports clock]
set_property IOSTANDARD LVCMOS33 [get_ports clock]
set_property PACKAGE_PIN F22 [get_ports reset]
set_property IOSTANDARD LVCMOS33 [get_ports reset]
set_property PACKAGE_PIN P16 [get_ports key_in]
set_property IOSTANDARD LVCMOS33 [get_ports key_in]
使用的按键
将reset打到下面不然会一直复位,然后按key按键,注意key按键默认没有按的时候是低电平,按键按下就会输入高电平,所以和大多少的按键相反,写程序注意按键上升沿检测按键
开发板上电后第一个led灯亮
按一下按键,第二个灯亮
再按一下按键,第三个灯亮,每次按键都是依次点亮led,如果按了八次就会重新从第一个灯开始亮起