- 触摸按键:若不进行处理,则按下时,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的波形绘制有些问题,该文章中的波形已经做了修改,各位可以仿真看下是谁出现了错误。
FPGA-触摸按键控制LED
于 2023-11-27 22:20:56 首次发布