//*********************************************************
//文件名称: key.v
//说明: 手把手教你学习FPGA—按键篇 实验一 按键控制LED亮灭
// 按KEY1,数码管在0-9变化
//版本号: V0.0 2012.11.26
//*********************************************************
module key(sys_clk, rst_n, key, led_seg, led_bit);
input sys_clk, rst_n;
input [7:0] key;
output [7:0] led_seg;
output [7:0] led_bit;
//reg [7:0] led;
reg [3:0] count10;
reg [7:0] led_seg;
wire [7:0] led_bit;
wire key_scan;
reg key_samp;
reg key_samp_r;
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
key_samp <= 1'b1;
else
key_samp <= key[0];
end
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
key_samp_r <= 1'b1;
else
key_samp_r <= key_samp;
end
assign key_scan = key_samp_r & (~key_samp);
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
count10 <= 4'd0;//led <= 8'b1111_1111;
else if (key_scan)
begin
count10 <= count10 + 1;//led <= ~led;
if (count10 == 4'd9)
count10 <= 4'd0;
end
// else
// led <= led;
end
always @ (count10)
begin
case(count10)
4'd0 : led_seg = 8'hc0;
4'd1 : led_seg = 8'hf9;
4'd2 : led_seg = 8'ha4;
4'd3 : led_seg = 8'hb0;
4'd4 : led_seg = 8'h99;
4'd5 : led_seg = 8'h92;
4'd6 : led_seg = 8'h82;
4'd7 : led_seg = 8'hf8;
4'd8 : led_seg = 8'h80;
4'd9 : led_seg = 8'h90;
default: led_seg = 8'h86;
endcase
end
assign led_bit = 8'b1111_1110;
endmodule
//*********************************************************
//文件名称: key.v
//说明: 手把手教你学习FPGA—按键篇 实验二 按键消抖控制LED亮灭
// 按KEY1,数码管在0-9变化
//版本号: V0.0 2012.11.28
//*********************************************************
module key(sys_clk, rst_n, key, led_seg, led_bit);
input sys_clk, rst_n;
input [7:0] key;
//output [7:0] led;
output [7:0] led_seg;
output [7:0] led_bit;
//reg [7:0] led;
reg [19:0] delay_cnt;
wire key_scan;
wire key_low;
reg key_samp;
reg key_samp_r;
reg key_rst;
reg key_rst_r;
reg [3:0] count10;
reg [7:0] led_seg;
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
key_samp <= 1'b1;
else
key_samp <= key[0];
end
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
key_samp_r <= 1'b1;
else
key_samp_r <= key_samp;
end
assign key_scan = key_samp_r & (~key_samp);
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
delay_cnt <= 20'h0;
else if (key_scan)
delay_cnt <= 20'h0;
else
delay_cnt <= delay_cnt + 1'b1;
end
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
key_rst <= 1'b1;
else if (delay_cnt == 20'hfffff)
key_rst <= key[0];
end
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
key_rst_r <= 1'b1;
else
key_rst_r <= key_rst;
end
assign key_low = key_rst_r & (~key_rst);
/*always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
led <= 8'b1111_1111;
else if (key_low)
led <= ~led;
else
led <= led;
end*/
always @ (posedge sys_clk or negedge rst_n)
begin
if (!rst_n)
count10 <= 4'd0;
else if (key_low)
begin
count10 <= count10 + 1;
if (count10 == 4'd9)
count10 <= 4'd0;
end
end
always @ (count10)
begin
case(count10)
4'd0 : led_seg = 8'hc0;
4'd1 : led_seg = 8'hf9;
4'd2 : led_seg = 8'ha4;
4'd3 : led_seg = 8'hb0;
4'd4 : led_seg = 8'h99;
4'd5 : led_seg = 8'h92;
4'd6 : led_seg = 8'h82;
4'd7 : led_seg = 8'hf8;
4'd8 : led_seg = 8'h80;
4'd9 : led_seg = 8'h90;
default: led_seg = 8'h86;
endcase
end
assign led_bit = 8'b1111_1110;
endmodule