-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter2.v
69 lines (59 loc) · 1.35 KB
/
counter2.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module counter2(
input clk, reset, enable,
input [7:0] currHour, currMin,
output reg [7:0] countSec,countMin,countHr);
//define states
parameter idleState=2'b00;
parameter counterState=2'b01;
parameter resetState=2'b10;
parameter timerSetState=2'b11;
//state signals
reg [1:0] currState, nextState;
//fsm state transition and output logic
always @(posedge clk or posedge reset) begin
if(reset) begin
currState <= idleState;
countSec<=8'b0;
countMin<=8'b0;
countHr<=8'b0;
end else begin
//state transition logic
case(currState)
idleState: begin
if(enable)
nextState=counterState;
//else if (timerSet)
//nextState=timerSetState;
else
nextState=idleState;
end
counterState: begin
countSec <= countSec + 1;
if (countSec == 59) begin
countSec <= 0;
countMin <= currMin + 1;
if (countMin == 59) begin
countMin <= 0;
countHr <= currHour + 1;
if (countHr == 23) begin
countHr <= 0;
end
end
end
end
resetState: begin
nextState=resetState;
end
timerSetState: begin
if(enable)
nextState=counterState;
else
nextState=timerSetState;
end
default: nextState=idleState;
endcase
//state update
currState<=nextState;
end
end
endmodule