按键边缘检测,控制LED亮灭

模块代码

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date:    13:40:18 09/25/2019 
// Design Name: 
// Module Name:    touch_led_top 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//
module touch_led_top(
	input 		clk,
	input 		rst_n,
	
	input 		touch_key,
	output  reg led
    );

reg touch_key_d0;
reg touch_key_d1;
wire touch_key_flag;

//边沿检测代码
assign touch_key_flag = (~touch_key_d1) & touch_key_d0;//检测上升沿
//assign touch_key_flag = touch_key_d1 & (~touch_key_d0);//检测下降沿

always@(posedge clk or negedge rst_n)begin
	if(!rst_n)begin
		touch_key_d0 <= 1'b0;
		touch_key_d1 <= 1'b0;
	end
	else begin
		touch_key_d0 <= touch_key;
		touch_key_d1 <= touch_key_d0;
	end		
end

//led赋值代码
always@(posedge clk or negedge rst_n)begin
	if(!rst_n)
		led <= 1'b1;
	else
		if(touch_key_flag)
			led <= ~led;
		else
			led <= led;
end

endmodule

仿真代码

`timescale 1ns / 1ps


// Company: 
// Engineer:
//
// Create Date:   10:07:01 09/26/2019
// Design Name:   touch_led_top
// Module Name:   C:/Verilog/touch_led/td_touch_led.v
// Project Name:  touch_led
// Target Device:  
// Tool versions:  
// Description: 
//
// Verilog Test Fixture created by ISE for module: touch_led_top
//
// Dependencies:
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 


module td_touch_led;

	// Inputs
	reg clk;
	reg rst_n;
	reg touch_key;

	// Outputs
	wire led;

	// Instantiate the Unit Under Test (UUT)
	touch_led_top uut (
		.clk(clk), 
		.rst_n(rst_n), 
		.touch_key(touch_key), 
		.led(led)
	);
	
	always #10 clk=~clk;//20nm时钟信号

	initial begin
		// Initialize Inputs
		clk = 0;
		rst_n = 0;
		touch_key = 0;

		// Wait 100 ns for global reset to finish
		#20;
		rst_n = 1;
		#100;
		touch_key = ~touch_key;
		#100;
		touch_key = ~touch_key;		
		#100;
		touch_key = ~touch_key;
		#100;
		touch_key = ~touch_key;	      
		// Add stimulus here

	end
      
endmodule

仿真图片
dage
dage

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值