Go to --->part part 1 part 2 part 3 part 4
6> Controller
This part is heart of the project
It will controlle all modul. base on this we have to decide how mush time if we hold key then it reset or etc base on FSM.
If you dont understand the fsm then try to under stand key==10 mans we dont give any key
RTL
// moore fsm for Alarm controller
module controller(clk,rst,alarm_button,time_button,key,one_sec,load_new_c,show_new_time,show_a,load_new_a,shift,rst_count);
input clk,rst,alarm_button,time_button,one_sec;
input [3:0]key;
output load_new_c,show_new_time,show_a,load_new_a,shift,rst_count;
parameter show_time=3'b000,
key_stored=3'b001,
key_waited=3'b010,
key_entry=3'b011,
set_alarm_time=3'b100,
set_current_time=3'b101,
show_alarm=3'b110;
reg [3:0]count1; //10sec for key_waited
reg [3:0]count2; //10sec for key_entry
reg [2:0]state,next_state;
wire time_out;
always@(posedge clk)
begin
if(rst)
state<=show_time;
else
state<=next_state;
end
always@(posedge clk)
begin
if(rst)
count1<=0;
else if(!(state==key_waited))
count1<=0;
else if(count1==9)
count1<=0;
else if(one_sec)
count1<=count1+1;
end
always@(posedge clk)
begin
if(rst)
count2<=0;
else if(!(state==key_entry))
count2<=0;
else if(count2==9)
count2<=0;
else if(one_sec)
count2<=count2+1;
end
assign time_out=(count1==9 || count2==9) ? 1'b0 : 1'b1;
always@(*)
begin
case(state)
show_time : begin
if(alarm_button)
next_state=show_alarm;
else if(key!=10)
next_state=key_stored;
else
next_state=show_time;
end
key_stored : next_state=key_waited;
key_waited : begin
if(key==10)
next_state=key_entry;
else if(time_out==0)
next_state=show_time;
else
next_state=key_waited;
end
key_entry : begin
if(alarm_button)
next_state=set_alarm_time;
else if(time_button)
next_state=set_current_time;
else if(time_out==0)
next_state=show_time;
else if(key!=10)
next_state=key_stored;
else
next_state=key_entry;
end
set_alarm_time : next_state=show_time;
set_current_time : next_state=show_time;
show_alarm : begin
if(!alarm_button)
next_state=show_time;
else
next_state=show_alarm;
end
default next_state=show_time;
endcase
end
assign show_a=(state==show_alarm) ? 1'b1 : 1'b0;
assign show_new_time=(state==key_entry || state==key_stored || state==key_waited) ? 1'b1 : 1'b0;
assign load_new_a=(state==set_alarm_time)? 1'b1 : 1'b0;
assign load_new_c=(state==set_current_time)? 1'b1 : 1'b0;
assign rst_count =(state==set_current_time)? 1'b1 : 1'b0;
assign shift=(state==key_stored)? 1'b1 : 1'b0;
endmodule
TEST BENCH
0 Comments