Lab 2: Switch Inputs and Debouncing
Implementing debounce logic for switch inputs.

Implementing debounce logic for switch inputs

In this lab, students will learn about the concept of debounce logic and its significance in digital systems. They will study Schmitt trigger inputs, which introduce hysteresis to the input signal, improving noise immunity and acting as a switch debouncer. Students will understand how Schmitt triggers help stabilize switch inputs by ensuring that a stable output is produced even in the presence of noisy or bouncing signals.

Lab Objective:

To learn and implement button debouncing techniques in Verilog and to utilize the SignalTap II Logic Analyzer to monitor and verify the functionality and stability of the debounced signals in real-time.

Required Tools and Components

  • Intel Quartus Prime (with SignalTap II Logic Analyzer).
  • DE10-Lite FPGA Development Board.
  • USB cable for FPGA programming and debugging.
  • Computers with Quartus Prime installed.

Pin Assignment of Push-buttons

Signal NameFPGA Pin No.DescriptionI/O Standard
KEY0PIN_B8Push-button[0]3.3 V SCHMITT TRIGGER
KEY1PIN_A7Push-button[1]3.3 V SCHMITT TRIGGER

Lab Setup and Implementation Guide

Verilog HDL code

module debounce(
    input CLOCK_50,
    input [1:0] KEY,
    output [7:0] LED
);

    reg [1:0] KEY_reg;           // Previous state of KEY
    reg [1:0] KEY_stable;        // Stable state of KEY after debouncing
    reg [19:0] debounce_count0 = 0;  // Debounce counter for KEY[0]
    reg [19:0] debounce_count1 = 0;  // Debounce counter for KEY[1]
    reg [7:0] count = 0;         // Main counter for button presses

    // Parameters for debounce threshold
    parameter DEBOUNCE_LIMIT = 20'hFFFFF; // Adjust this threshold as needed for your environment

    always @(posedge CLOCK_50) begin
        // Debouncing for KEY[0]
        if (KEY[0] == KEY_reg[0]) begin
            debounce_count0 <= 0;
        end else begin
            if (debounce_count0 < DEBOUNCE_LIMIT)
                debounce_count0 <= debounce_count0 + 1;
            else begin
                KEY_stable[0] <= KEY[0];
                debounce_count0 <= 0;
            end
        end

        // Debouncing for KEY[1]
        if (KEY[1] == KEY_reg[1]) begin
            debounce_count1 <= 0;
        end else begin
            if (debounce_count1 < DEBOUNCE_LIMIT)
                debounce_count1 <= debounce_count1 + 1;
            else begin
                KEY_stable[1] <= KEY[1];
                debounce_count1 <= 0;
            end
        end

        KEY_reg <= KEY;  // Update the previous state of KEY

        // Increment main count on falling edge of debounced KEY[0]
        if (KEY_stable[0] == 0 && KEY_reg[0] == 1)
            count <= count + 1;

        // Reset count on falling edge of debounced KEY[1]
        if (KEY_stable[1] == 0 && KEY_reg[1] == 1)
            count <= 0;
    end

    assign LED = count;  // Display count on LEDs

endmodule

Pin Planner

Node NameDirectionLocationI/O Standard
CLOCK_50InputPIN_P113
KEY[1]InputPIN_A77
KEY[0]InputPIN_B48
LED[7]OutputPIN_D147
LED[6]OutputPIN_E147
LED[5]OutputPIN_C137
LED[4]OutputPIN_D137
LED[3]OutputPIN_B107
LED[2]OutputPIN_A107
LED[1]OutputPIN_A97
LED[0]OutputPIN_A87

Explanation

This Verilog module named debounce is designed to handle button inputs from a two-button setup (KEY[0] and KEY[1]), apply debouncing to these inputs, and display the result on an 8-bit LED display. Debouncing is essential for reliable digital input from mechanical buttons or switches, which often generate noisy signals when pressed or released.

Setting Up Signal Tap Logic Analyzer

  1. Go to Tools > SignalTap II Logic Analyzer.
  2. Add a new SignalTap II file to your project.
  3. Insert KEY, KEY_reg, KEY_stable, debounce_count0, and debounce_count1 into the SignalTap setup.
  4. Set trigger conditions to capture the falling edge of KEY[0] and KEY[1].

Signal Tap Setup

Output

Graph Result