第一:项目需求
使用触摸按键控制LED灯亮灭,开发板上电后LED为点亮状态,手指触摸后LED熄灭,再次 触摸,LED点亮。
第二:源代码
module touch_led(
input clk,
input rst_n,
input touch,
output reg led
);
reg touch_en;
reg touch_d0;
reg touch_d1;
assign up=(~touch_d0)&(touch_d1);
// 确定上升沿的程序
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
touch_d0<=1'b0;
touch_d1<=1'b0;
end
else begin
touch_d0<=touch;
touch_d1<=touch_d0;
end
end
always@(posedge clk or negedge rst_n) begin
if(!rst_n)begin
touch_en<=1'b0;
end
else if(up) begin// 上升沿
touch_en<=1'b1;
end
else begin
touch_en<=1'b0;
end
end
always@(posedge clk or negedge rst_n)begin
if(!rst_n)
led<=1'b0;
else if(touch_en ) //按下去
led<=~led;
else
led<=led;
end
endmodule
第三:总结
1:做按键的时候,为什么需要两个标志量, 一是Key_flag, 另外一个是key_value。触摸按键不需要消除抖动的过程,因此只需要touch_en
2: 上升沿或者下降沿的检测应该需要,一个单独得模块。
3:如果按键采用检测上下沿的方式,按键的抖动过程会产生很多干扰信号。