来源:博客园
2023-06-03 16:05:23
(资料图片)
触摸按键可分为四大类:电阻式,电容式,红外感应式和表面声波式
没有触摸按键的时候是高电平,触摸按键的时候是低电平,低电平的维持时间是和触摸时间是一致的,松开之后又会回到高电平采用touch_key信号控制按键,按键点亮需要一直按在按键上,如何实现按一次点亮,再按一次熄灭?采用touch_key的下降沿控制输出信号进行取反,触摸按键没有按下的时候,输出信号保持原来的值,检测到下降沿的时候进行取反,等到下一个下降沿的时候再次进行取反,就可以实现如何采集下降沿,使用寄存器?
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; always @ (posedge sys_clk or negedge sys_rst_n) if(!sys_rst_n) begin touch_key_1 <= 1"b1; touch_key_2 <= 1"b0; end else begin touch_key_1 <= touch_key; touch_key_2 <= touch_key_1; end assign touch_flag = (touch_key_1 && touch_key_2) ? 1"b1 : 1"b0; // 输出信号 always @ (posedge sys_clk or negedge sys_rst_n) if(!sys_rst_n) led <= 1"b1; else if(touch_flag) led <= ~led; else led <= led; endmodule// 时序逻辑采样,上升沿always @ (posedge sys_clk or negedge sys_rst_n) if(!sys_rst_n) podge <= 1"b0; else if((touch_key_1 == 1"b1) && (touch_key_2 == 1"b0)) podge <= 1"b1; else podge <= 1"b0; always @ (posedge sys_clk or negedge sys_rst_n) if(!sys_rst_n) podge <= 1"b0; else if((touch_key_1 == 1"b0) || (touch_key_2 == 1"b1)) podge <= 1"b0; else podge <= 1"b1;// 时序逻辑采样,下升沿always @ (posedge sys_clk or negedge sys_rst_n) if(!sys_rst_n) nedge <= 1"b0; else if((touch_key_1 == 1"b0) && (touch_key_2 == 1"b1)) nedge <= 1"b1; else nedge <= 1"b0; always @ (posedge sys_clk or negedge sys_rst_n) if(!sys_rst_n) nedge <= 1"b0; else if((touch_key_1 == 1"b1) || (touch_key_2 == 1"b0)) nedge <= 1"b0; else nedge <= 1"b1;`timescale 1ns/1nsmodule 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 关键词: