-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrafficLightController.v
86 lines (80 loc) · 2.04 KB
/
TrafficLightController.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module TrafficLight(input clk, //LED_NS represent the North-South LEDs
input rst, //LED_WE represent the West-East LEDs
output reg [2:0] LED_NS, LED_WE);
/*Let 100 = Red
010 = Yellow
001 = Green
We have 6 different states, defining them below */
parameter S0 = 6'b000001, //When NS is green and EW is red
S1 = 6'b000010, //When NS is yellow and EW is red
S2 = 6'b000100, //When NS is red and EW is red
S3 = 6'b001000, //When NS is red and EW is green
S4 = 6'b010000, //When NS is red and EW is yellow
S5 = 6'b100000; //When NS is red and EW is red
//Then the cycle repeats
reg [5:0] state;
reg [3:0] count;
//Defining transition of states
always@(posedge clk or posedge rst)
begin
if(rst) begin //Active HIGH reset
state <= S0;
count <= 0; end
else
begin
case(state)
S0: begin
if(count == 4'd14)
begin count <= 0;
state <= S1; end
else begin
count = count + 1; state <= S0; end end
S1: begin
if(count == 4'd2)
begin count <= 0;
state <= S2; end
else begin
count = count + 1; state <= S1; end end
S2: begin
if(count == 4'd2)
begin count <= 0;
state <= S3; end
else begin
count = count + 1; state <= S2; end end
S3: begin
if(count == 4'd14)
begin count <= 0;
state <= S4; end
else begin
count = count + 1; state <= S3; end end
S4: begin
if(count == 4'd2)
begin count <= 0;
state <= S5; end
else begin
count = count + 1; state <= S4; end end
S5: begin
if(count == 4'd2)
begin count <= 0;
state <= S0; end
else begin
count = count + 1; state <= S5; end end
endcase
end
end
/* 100 = Red
010 = Yellow
001 = Green */
//Output of LEDs//
always@(*)
begin
case(state)
S0: begin LED_NS = 3'b001; LED_WE = 3'b100; end
S1: begin LED_NS = 3'b010; LED_WE = 3'b100; end
S2: begin LED_NS = 3'b100; LED_WE = 3'b100; end
S3: begin LED_NS = 3'b100; LED_WE = 3'b001; end
S4: begin LED_NS = 3'b100; LED_WE = 3'b010; end
S5: begin LED_NS = 3'b100; LED_WE = 3'b100; end
endcase
end
endmodule