和机械按键结果一样,按下电平发生变化,松开恢复原状
module touch_ctrl_led
(
input wire sys_clk,
input wire sys_rst_n,
input wire touch_key,
output reg led
);
reg touch_key_1 ;
reg touch_key_2 ;
wire touch_flag ;
//边沿检测
always @(posedge sys_clk or negedge sys_rst_n)
begin
if (sys_rst_n == 1'b0)
begin
touch_key_1 <= 1'b1;
touch_key_2 <= 1'b1;
end
else
begin
touch_key_1 <= touch_key;
touch_key_2 <= touch_key_1; //此时touch_key_2比touch_key_1慢一拍 这样更容易产生标志信号
end
end
assign touch_flag = ((touch_key_1 == 1'b0)&&(touch_key_2 == 1'b1));
always@(posedge sys_clk or negedge sys_rst_n)
if(sys_rst_n == 1'b0)
led <= 1'b1;
else if(touch_flag == 1'b1)
led <= ~led;
else
led <= led;
endmodule
`timescale 1ns/1ns
module tb_touch_ctrl_led();
reg sys_clk;
reg sys_rst_n;
reg touch_key;
wire led;
initial
begin
sys_clk <= 1'b1;
sys_rst_n <= 1'b0;
touch_key <= 1'b1;
#20
sys_rst_n <= 1'b1;
#200
touch_key <= 1'b0;
#2000
touch_key <= 1'b1;
#1000
touch_key <= 1'b0;
#3000
touch_key <= 1'b1;
end
always #20 sys_clk = ~sys_clk;
touch_ctrl_led touch_ctrl_led_inst
(
.sys_clk (sys_clk),
.sys_rst_n (sys_rst_n),
.touch_key (touch_key),
.led (led)
);
endmodule