Verilog if statement always activating

In summary: If so, you need to set a "next_state" value to "Disable" in the internal state variables, and also set a "prev_kp" value to 14 in the external state variables.
  • #1
tcosentino
1
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
  • #2
If kp is actually coming from a mechanical key switch, did you de-bounce it somewhere?
 

1. What is the purpose of an always block in Verilog?

An always block in Verilog is used to describe behavior that will always be active, regardless of any changes in the inputs or signals. It is commonly used to model sequential logic in circuits.

2. How does an if statement work in Verilog?

In Verilog, an if statement is used to control the flow of logic based on a condition. If the condition is met, the code inside the if statement will be executed. Otherwise, it will be skipped.

3. What happens if an always block contains multiple if statements?

If an always block contains multiple if statements, they will be evaluated sequentially. This means that the first if statement will be checked, and if the condition is met, the code inside will be executed. If not, the next if statement will be checked, and so on.

4. Can an always block contain both if and else statements?

Yes, an always block can contain both if and else statements. If the condition in the if statement is met, the code inside will be executed. If not, the code inside the else statement will be executed instead.

5. How can I ensure that an always block only activates under certain conditions?

You can use a combination of if statements and conditional expressions to control when an always block activates. By setting up the proper conditions, you can ensure that the code inside the always block only executes when specific criteria are met.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
983
  • Electrical Engineering
Replies
6
Views
4K
  • General Engineering
Replies
1
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Advanced Physics Homework Help
Replies
14
Views
1K
  • Electrical Engineering
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
9K
Back
Top