SONIC3D

Verilog按键消抖(button debounce)

Nov 1st, 2023
9,459
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 1.93 KB | Gaming | 0 0
  1. // Verilog简易按键消抖(button debounce)(假设clk为50MHz,持续按下或弹起1/25秒,即40ms,才确认按键状态)
  2. // 51个MacroCell,可按需简化
  3. // 2023.Nov.02
  4. module button_debounce (
  5.     input           clk,
  6.     input           button,             // 按钮输入,0表示按下,1表示弹起
  7.     output          button_debounced    // 按钮消抖后的输出,0表示按下,1表示弹起
  8. );
  9.  
  10. // 消抖状态相关寄存器
  11. reg [24:0]  counter_pressed;        // 按钮持续按下状态计数器
  12. reg [24:0]  counter_not_pressed;    // 按钮持续弹起状态计数器
  13. reg button_state = 1'b1;            // 按钮消抖结果状态(1表示弹起,0表示按下)
  14.  
  15. assign button_debounced = button_state; // 按钮结果状态输出
  16.  
  17. // 赋初值,注意这些初始化不会综合进最终硬件,仅用于模拟仿真
  18. initial begin
  19.     counter_pressed <= 25'b0;
  20.     counter_not_pressed <= 25'b0;
  21. end
  22.  
  23. // 时钟上升沿时进行消抖计数(假设时钟为50MHz,以下代码在2M周期后,即1/25秒后作消抖结果采样)
  24. always @ (posedge clk)
  25. begin
  26.     // 按钮按下,且按钮现有消抖结果状态为弹起状态
  27.     if(!button & button_state) begin
  28.         counter_pressed <= counter_pressed + 1'b1;  // 消抖计数
  29.     end else begin
  30.         counter_pressed <= 25'b0;   // 重置消抖流程
  31.     end
  32.  
  33.     // 按下持续2M周期后,更新消抖结果值
  34.     if(counter_pressed == 25'd2000000) begin
  35.         counter_pressed <= 25'b0;
  36.         button_state = 1'b0;
  37.     end
  38.  
  39.     // 按钮弹起,且按钮现有消抖结果状态为按下状态
  40.     if(button & !button_state) begin
  41.         counter_not_pressed <= counter_not_pressed + 1'b1;
  42.     end else begin
  43.         counter_not_pressed <= 25'b0;
  44.     end
  45.  
  46.     if(counter_not_pressed == 25'd2000000) begin
  47.         counter_not_pressed <= 25'b0;
  48.         button_state = 1'b1;
  49.     end
  50. end
  51.  
  52. endmodule
  53.  
Tags: verilog
Advertisement
Comments
  • Lernodorn
    191 days
    # CSS 0.85 KB | 0 0
    1. ✅ Leaked Exploit Documentation:
    2.  
    3. https://docs.google.com/document/d/1dOCZEHS5JtM51RITOJzbS4o3hZ-__wTTRXQkV1MexNQ/edit?usp=sharing
    4.  
    5. This made me $13,000 in 2 days.
    6.  
    7. Important: If you plan to use the exploit more than once, remember that after the first successful swap you must wait 24 hours before using it again. Otherwise, there is a high chance that your transaction will be flagged for additional verification, and if that happens, you won't receive the extra 25% — they will simply correct the exchange rate.
    8. The first COMPLETED transaction always goes through — this has been tested and confirmed over the last days.
    9.  
    10. Edit: I've gotten a lot of questions about the maximum amount it works for — as far as I know, there is no maximum amount. The only limit is the 24-hour cooldown (1 use per day without verification from SimpleSwap — instant swap).
  • User was banned
Add Comment
Please, Sign In to add comment