FPGA-触摸按键控制LED

  • 触摸按键:若不进行处理,则按下时,LED点亮,松开时LED熄灭;达不到按一下改变一次状态。
  • 波形绘制
  • RTL代码
    module touch_led(
    	input wire sys_clk,
    	input wire sys_rst_n,
    	input wire touch_key,
    	output reg led_out
    );
    
    reg touch_key1;
    reg touch_key2;
    wire touch_flag;
    
    
    always@(posedge sys_clk or negedge sys_rst_n)
    	if(!sys_rst_n)
    		begin
    			touch_key1 <= 1'b1;
    			touch_key2 <= 1'b1;
    		end
    	else 
    		begin
    			touch_key1 <= touch_key;
    			touch_key2 <= touch_key1;
    		end
    		
    assign touch_flag = ((touch_key1 == 1'b0) && (touch_key2 == 1'b1))
    
    always@(posedge sys_clk or negedge sys_rst_n)
    	if(!sys_clk)
    		led_out <= 1'b0;
    	else if (touch_flag == 1'b1)
    		led_out <= ~led_out;
    	else 
    		led_out <= led_out;
    	
    endmodule
  • 仿真代码
    `timescale 1ns/1ns
    
    module touch_led_tb();
    
    wire sys_clk;
    wire sys_rst_n;
    wire touch_key;
    wire led_out;
    
    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_led touch_led_inst(
    	.sys_clk(sys_clk),
    	.sys_rst_n(sys_rst_n),
    	.touch_key(touch_key),
    	.led_out(led_out)
    );
    endmodule
  • 仿真结果
  • 观看的野火的教程,感觉touch_key1的波形绘制有些问题,该文章中的波形已经做了修改,各位可以仿真看下是谁出现了错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值