当前位置:   article > 正文

基于vivado+Verilog FPGA开发 — 使用状态机实现按键消抖_vivado频率计怎么实现消抖

vivado频率计怎么实现消抖

代码规范:Verilog 代码规范_verilog代码编写规范-CSDN博客

开发流程:FPGA基础知识----第二章 FPGA 开发流程_fpga 一个项目的整个流程-CSDN博客 

源码下载:GitHub - Redamancy785/FPGA-Learning-Record: 项目博客:https://blog.csdn.net/weixin_51460407

一、功能定义

二、设计输入 

  1. module key_filter(
  2. clk_i,
  3. reset_n_i,
  4. key_i,
  5. press_key_flag_o,
  6. release_key_flag_o
  7. );
  8. input clk_i,reset_n_i,key_i;
  9. output press_key_flag_o,release_key_flag_o;
  10. reg press_key_flag_o,release_key_flag_o;
  11. reg [1:0] state;
  12. wire time_20ms_reached;
  13. localparam IDLE = 0;
  14. localparam PRESS_FILTER = 1;
  15. localparam WAIT = 2;
  16. localparam RELEASE_FILTER = 3;
  17. // 输入 key_i 同步处理
  18. reg r_key_i,sync_d_0_key_i,sync_d_1_key_i;
  19. wire nedge_key,pedge_key;
  20. always@(posedge clk_i)
  21. sync_d_0_key_i <= key_i;
  22. always@(posedge clk_i)
  23. sync_d_1_key_i <= sync_d_0_key_i;
  24. always@(posedge clk_i)
  25. r_key_i <= sync_d_1_key_i;
  26. assign nedge_key = r_key_i & (~sync_d_1_key_i);
  27. assign pedge_key = ~r_key_i & sync_d_1_key_i;
  28. // 状态机
  29. always@(posedge clk_i or negedge reset_n_i)
  30. if(!reset_n_i) begin
  31. release_key_flag_o <= 0;
  32. press_key_flag_o <= 0;
  33. state <= IDLE;
  34. end
  35. else begin
  36. case(state)
  37. IDLE :
  38. begin
  39. release_key_flag_o <= 0;
  40. if(nedge_key)
  41. state <= PRESS_FILTER;
  42. else
  43. state <= IDLE;
  44. end
  45. PRESS_FILTER :
  46. if(time_20ms_reached) begin
  47. state <= WAIT;
  48. press_key_flag_o <= 1;
  49. end
  50. else if(pedge_key)
  51. state <= IDLE;
  52. else
  53. state <= PRESS_FILTER;
  54. WAIT :
  55. begin
  56. press_key_flag_o <= 0;
  57. if(pedge_key)
  58. state <= RELEASE_FILTER;
  59. else
  60. state <= WAIT;
  61. end
  62. RELEASE_FILTER :
  63. if(time_20ms_reached) begin
  64. state <= IDLE;
  65. release_key_flag_o <= 1;
  66. end
  67. else if(nedge_key)
  68. state <= WAIT;
  69. else
  70. state <= RELEASE_FILTER;
  71. endcase
  72. end
  73. // time_20ms_reached
  74. parameter MCNT = 1000_000;
  75. reg [29:0] counter;
  76. always@(posedge clk_i or negedge reset_n_i)
  77. if(!reset_n_i)
  78. counter <= 0;
  79. else if(state == RELEASE_FILTER || state == PRESS_FILTER)
  80. counter <= counter + 1;
  81. else
  82. counter <= 0;
  83. assign time_20ms_reached = ( counter >= (MCNT - 1) );
  84. endmodule

三、功能仿真 

  1. `timescale 1ns / 1ns
  2. module key_filter_tb();
  3. reg u_clk_i,u_reset_n_i,u_key_i;
  4. wire u_press_key_flag_o,u_release_key_flag_o;
  5. key_filter U_key_filter_0(
  6. .clk_i(u_clk_i),
  7. .reset_n_i(u_reset_n_i),
  8. .key_i(u_key_i),
  9. .press_key_flag_o(u_press_key_flag_o),
  10. .release_key_flag_o(u_release_key_flag_o)
  11. );
  12. initial u_clk_i = 1;
  13. always #10 u_clk_i = ~u_clk_i;
  14. initial begin
  15. u_reset_n_i = 0;
  16. u_key_i = 1;
  17. #201;
  18. u_reset_n_i = 1;
  19. #10_000_000;
  20. u_key_i = 0; #10_000_000;
  21. u_key_i = 1; #10_000_000;
  22. u_key_i = 0; #30_000_000;
  23. u_key_i = 1; #10_000_000;
  24. u_key_i = 0; #10_000_000;
  25. u_key_i = 1; #30_000_000;
  26. $stop;
  27. end
  28. endmodule

四、综合优化

五、布局布线

六、时序仿真

七、板级调试 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号