add debouncing buttons before start/stop

This commit is contained in:
2020-10-26 22:33:44 -05:00
parent c6cc9bc99f
commit 124d1fff63
2 changed files with 134 additions and 4 deletions

View File

@@ -12,6 +12,21 @@ module tdc #(parameter COUNTER_WIDTH=16)(
reg [COUNTER_WIDTH-1:0] counter;
assign o_data = counter;
reg db_start, db_stop;
debounce db1 (
// Outputs
.db (db_start),
// Inputs
.clk (i_clk),
.reset (i_reset),
.sw (i_start));
debounce db2 (
// Outputs
.db (db_stop),
// Inputs
.clk (i_clk),
.reset (i_reset),
.sw (i_stop));
// states
localparam state_idle = 2'b00;
localparam state_started = 2'b01;
@@ -20,7 +35,7 @@ localparam state_stopped = 2'b11;
reg [1:0] current_state, next_state;
// ensure that state changes each clock
always @(posedge i_clk) begin
always @(posedge i_clk, posedge i_reset) begin
if (i_reset) begin
current_state <= state_idle;
end else begin
@@ -33,19 +48,19 @@ end
always @(*) begin
case (current_state)
state_idle: begin
if (i_start && (~i_stop))
if (db_start && (~db_stop))
next_state <= state_started;
else
next_state <= state_idle;
end
state_started: begin
if (~i_start && (~i_stop))
if (~db_start && (~db_stop))
next_state <= state_running;
else
next_state <= state_started;
end
state_running: begin
if (~i_start && (i_stop))
if (~db_start && (db_stop))
next_state <= state_stopped;
else
next_state <= state_running;