天天看點

Verilog 上升沿檢測信号及觸摸按鍵控制led亮滅

功能:實作觸摸按鍵來控制led 燈的亮滅。

核心代碼:經典的上升沿檢測信号脈沖的獲得

//上升沿檢測信号脈沖
 assign touch_en=(~touch_reg1)&& touch_reg0;
 always @(posedge sys_clk or negedge sys_rst_n)begin
	if(!sys_rst_n)begin
		touch_reg0<=1'b0;
		touch_reg1<=1'b0;
		end
	else begin
	    touch_reg0<=touch;
		touch_reg1<=touch_reg0;
		end
end
           

全部代碼:

//author :bronceyang
//time:  2020.04.16
// version: 1.0
//功能描述:觸摸按鍵上升沿檢測,控制led亮滅

module touch_key(
	input sys_clk,
	input sys_rst_n,
	input touch,
	
	output reg led


);

//reg define 
reg touch_reg0;
reg touch_reg1;

//wire define
wire touch_en;

//上升沿檢測信号脈沖
 assign touch_en=(~touch_reg1)&& touch_reg0;
 always @(posedge sys_clk or negedge sys_rst_n)begin
	if(!sys_rst_n)begin
		touch_reg0<=1'b0;
		touch_reg1<=1'b0;
		end
	else begin
	   touch_reg0<=touch;
		touch_reg1<=touch_reg0;
		end
end

//檢測結果控制led亮滅
always @(posedge sys_clk or negedge sys_rst_n)begin
	if(!sys_rst_n)
		led<=1'b1;
	else if(touch_en)
		led<=~led;
end

	
	endmodule