基于FPGA的上升沿检测模块

以触摸按键驱动led为例

RTL代码:

module touch_led(
	input sys_clk,
	input sys_rst,
	input touch_key,
	output reg led
);

reg touch_key_d0;
reg touch_en;

// edge detect
always @(posedge sys_clk or negedge sys_rst) begin
	if (!sys_rst)
		touch_key_d0 <= 1'd0;
	else begin
		touch_key_d0 <= touch_key;   // delay one clk 
		touch_en <= (~touch_key_d0) & touch_key;
		end
end

// led control
always @(posedge sys_clk or negedge sys_rst) begin
	if (!sys_rst)
		led <= 1'd1;
	else begin
		if (touch_en)
			led <= ~ led;
		end
end
endmodule

Testbench:

`timescale 1ns/1ns

module touch_led_tb();

reg sys_clk;
reg sys_rst;
reg touch_key;
reg touch_key_d0;
reg touch_en;

wire led;

parameter T = 10;

always #T sys_clk = ~ sys_clk;

initial begin
	sys_rst <= 1'b0;
	sys_clk <= 1'b0;
	touch_key <= 1'b0;
	# 20 sys_rst <= 1'b1;
	# 10 touch_key <= 1'b1;
	# 30 touch_key <= 1'b0;
	# 110 touch_key <= 1'b1;
	# 30 touch_key <= 1'b0;
end

touch_led u1(
	sys_clk,
	sys_rst,
	touch_key,
	led
);

endmodule

仿真时序分析:

检测到触摸按键的上升沿后两个时钟周期,led将被激活

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值