触摸按键控制LED灯

目录

1.理论

2.代码

[2.1 touch_ctrl_led.v](#2.1 touch_ctrl_led.v)

[2.2 tb_touch_ctrl_led](#2.2 tb_touch_ctrl_led)


1.理论

以上的波形图的touch_flag是采用组合逻辑的方式产生的。

以上的touch_flag是采用时序逻辑产生的,时序逻辑会延迟一拍。

以上是上升沿和下降沿的组合逻辑和时序逻辑实现,逻辑或的写法刚好是逻辑与的两个寄存器的值反过来。

2.代码

2.1 touch_ctrl_led.v

复制代码
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; //因为没有延迟一拍所以是组合逻辑,wire形

//边沿检测的作用就是能够准确识别出单比特信号的上升沿或下降沿
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)
		begin
			touch_key_1<=1'b1;
			touch_key_2<=1'b1;
		end
	else if(sys_rst_n==1'b1)
		begin
			touch_key_1<=touch_key;
			touch_key_2<=touch_key_1;
		end
		
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

2.2 tb_touch_ctrl_led

复制代码
`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 #10 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

2.3 仿真结果

相关推荐
我爱C编程4 分钟前
【1.22】基于FPGA的Costas环开发课程学习总结
学习·fpga开发·costas环
FPGA_无线通信2 小时前
USB3.0 GPIF II接口
fpga开发
Terasic友晶科技3 小时前
5-DE10-Nano的HDMI方块移动案例——基于FPGA的I2C控制模块设计
单片机·嵌入式硬件·fpga开发·时钟分频·hdmi方块移动·i2c控制模块设计·配置寄存器
Joshua-a3 小时前
AGM FPGA AG10K 使用Supra新建Quartus工程开发教程
fpga开发
西安同步高经理3 小时前
脉冲信号发生器时序控制核心、延迟信号发生器、时间间隔发生器
fpga开发
FPGA_小田老师5 小时前
FPGA基础知识(二十一):xilinx FPGA中常用的原语
fpga开发·bufg·ibufds·obufds·iobuf·差分转单端·inout选择
minglie15 小时前
Tang-Nano-1K测试
fpga开发
千歌叹尽执夏5 小时前
FPGA:自定义AXI-FIFO主机接口(含versal读写工程)
fpga开发·ddr4·axi总线·versal·自定义fifo
可编程芯片开发19 小时前
基于FPGA的数字示波器设计和硬件调试,将正弦波形通过HDMI接口在屏幕上动态显示
fpga开发·hdmi·数字示波器·正弦波形
风_峰1 天前
AMD FPGA产品系列讲解
fpga开发