触摸按键控制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 仿真结果

相关推荐
zlinear数据采集卡9 分钟前
数据采集卡从入门到精通(7):流水线型ADC——级级接力,高速与高精的平衡术
开发语言·arm开发·嵌入式硬件·fpga开发·c#
鬼手点金15 小时前
FPGA常用型号参考
fpga开发·verilog·vivado·xilinx·vhdl·altera·lattice
Kong_19942 天前
芯片开发学习笔记·二十九——大模型基础知识与 KV Cache 详解
fpga开发·芯片开发
m0_46644103詹湛2 天前
FPGA 定点数、小数与负数的加减乘除原理
fpga开发
zlinear数据采集卡2 天前
D223上位机C#开发实战:从帧解析到波形显示的完整实现
开发语言·arm开发·嵌入式硬件·fpga开发·开源·c#
zlinear数据采集卡2 天前
D223 ADC数据处理流水线:从原始采样值到工程单位的完整转换链
开发语言·arm开发·嵌入式硬件·fpga开发·开源·c#
FPGA小迷弟2 天前
Vivado中高速收发器GTX/GTH的配置与调试实战
fpga开发·fpga·硬件开发
minglie12 天前
简易寄存器接口SMMR---iic控制器
fpga开发
FPGA小迷弟2 天前
Vivado ILA与VIO调试工具实战指南:从入门到精通
fpga开发·fpga·硬件开发
FPGA小迷弟2 天前
FPGA状态机设计详解:从Moore到Mealy的完整指南
fpga开发·fpga