Verilog if statement always activating

  • Thread starter Thread starter tcosentino
  • Start date Start date
  • Tags Tags
    If statement
Click For Summary
SUMMARY

The discussion centers on a Verilog implementation of a combination lock using a state machine format. The user reports that an if statement intended to control the next state always activates the assignment to next_state = Inuse. The issue arises from the handling of the input signal kp, which may not be properly debounced, leading to unintended state transitions. The user is advised to ensure that the mechanical key switch input is debounced to prevent false triggering of the state logic.

PREREQUISITES
  • Understanding of Verilog syntax and constructs
  • Familiarity with state machines in digital design
  • Knowledge of input debouncing techniques for mechanical switches
  • Experience with combinational and sequential logic in Verilog
NEXT STEPS
  • Research Verilog input debouncing techniques for mechanical switches
  • Learn about state machine design patterns in Verilog
  • Explore the use of the always block in Verilog for combinational logic
  • Study the implications of signal assignments in Verilog, particularly with respect to non-blocking and blocking assignments
USEFUL FOR

Digital designers, Verilog programmers, and anyone developing state machine-based logic for hardware applications, particularly in security systems like combination locks.

tcosentino
Messages
1
Reaction score
0
So i am writing a Verilog program for a combination lock. I am using a state machine format to write it. I have commented out everything and just isolated one if statement as you can see. No matter what i put in the if statements the code within it (next_state = Inuse;) activates. The only way i can possibly get that code to not run is if i put in if(0).

I have been trying things and staring at it for a few hours but can not figure it out. Thank you for any help.

Here is my code:

module Lock_Logic(
clk,
clear, reset,
kp, v,
sec_3, sec_5,
lt_Reset, lt_Inuse, lt_Disable, lt_Open, prev_kp,
o_count //for output display
);

input clk;
input clear, reset;
input [3:0] kp;
input v;
input sec_3, sec_5; //high when 3sec or 5sec clock goes off respectively

output lt_Reset, lt_Inuse, lt_Disable, lt_Open; //if used in combo logic add reg
output [1:0] o_count;

//----Define state values as constants----------------
parameter Reset = 3'b001;
parameter Inuse = 3'b010;
parameter Disable = 3'b011;
parameter Open = 3'b100;

//----Internal Variables------------------------------
reg [2:0] state; // registered state value
reg [2:0] next_state; // combinational next state value
output reg [3:0] prev_kp;
reg [1:0] cnt;

//----Initial values----------------------------------
initial
begin
next_state = Reset;
prev_kp = 15;
end

//----------------------------------------------------
// Combinational logic to determine next state and outputs
//
always @ (state, clear, v)
begin
if(clear)
begin
// case (state)
// Reset: //reset case
// Inuse: //inuse case
// Disable: //disable case
// Open: //open case
// endcase
end
if(kp != prev_kp)
next_state = Inuse;
// begin
// case (state)
// Reset: //reset case
// begin
// next_state = Inuse;
// end
//// Inuse: //inuse case
//// Disable: //disable case
//// Open: //open case
// endcase
// end
prev_kp <= kp; //to check if the number pressed has changed
end

//----Sequential logic to register the state value----
always @ (posedge clk)
state <= next_state;

//----Output Logic------------------------------------
assign lt_Reset = (state == Reset) ? 1 : 0;
assign lt_Inuse = (state == Inuse) ? 1 : 0;
assign lt_Disable = (state == Disable) ? 1 : 0;
assign lt_Open = (state == Open) ? 1 : 0;
assign o_count = cnt;
endmodule
 
Engineering news on Phys.org
If kp is actually coming from a mechanical key switch, did you de-bounce it somewhere?
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 6 ·
Replies
6
Views
3K