hor : christ_song
7 // Company : Xidian ISN
8 // Email : christsong0127@gmail.com
9 //
10 // Description : 控制两路红绿灯的状态切换
11 // hold信号表示紧急制动
12 //
13 //==================================================================================================
14 `include "define.v"
15
16 module CONTROL(
17 input clk,
18 input rst_n,
19 input hold_n,
20 output reg red_a,
21 output reg green_a,
22 output reg yellow_a,
23 output reg red_b,
24 output reg green_b,
25 output reg yellow_b,
26 //与倒计时模块的接口
27 output reg [4:0] state
28 );
29
30 // localparam EMRG = 5'b00001;
31 // localparam AgBr = 5'b00010;
32 // localparam AyBr = 5'b00100;
33 // localparam ArBg = 5'b01000;
34 // localparam ArBy = 5'b10000;
35 localparam TimeRound = `AgBr_time + `AyBr_time + `ArBg_time + `ArBy_time + 8'd3;
36
37 reg [4:0] next_state;
38 reg [7:0] time_cnt;
39 always @(posedge clk or negedge rst_n)
40 if (!rst_n)
41 time_cnt <=#1 8'b0;
42 else if(!hold_n)
43 time_cnt <=#1 time_cnt;
44 else if(time_cnt >= TimeRound)
45 time_cnt <=#1 8'b0;
46 else
47 time_cnt <=#1 time_cnt + 1'b1;
48
49 always @(posedge clk or negedge rst_n)
50 if (!rst_n)
51 state <=#1 `EMRG;
52 else
53 state <=#1 next_state;
54 always @(*)
55 case(state)
56 `EMRG:
57 if(!hold_n)
58 next_state = `EMRG;
59 else if(time_cnt < `AgBr_time)
60 next_state = `AgBr;
61 else if(time_cnt < `AgBr_time + `AyBr_time + 8'd1)
62 next_state = `AyBr;
63 else if(time_cnt < `AgBr_time + `AyBr_time + `ArBg_time + 8'd2)
64 next_state = `ArBg;
65 else
66 next_state = `ArBy;
67 `AgBr:
68 if(!hold_n)
69 next_state = `EMRG;
70 else if(time_cnt < `AgBr_time)
71 next_state = `AgBr;
72 else
73 next_state = `AyBr;
74 `AyBr:
75 if(!hold_n)
76 next_state = `EMRG;
77 else if(time_cnt < `AgBr_time + `AyBr_time + 8'd1)
78 next_state = `AyBr;
79 else
80 next_state = `ArBg;
81 `ArBg:
82 if(!hold_n)
83 next_state = `EMRG;
84 else if(time_cnt < `AgBr_time + `AyBr_time + `ArBg_time + 8'd2)
85 next_state = `ArBg;
86 else
87 next_state = `ArBy;
88 `ArBy:
89 if(!hold_n)
90 next_state = `EMRG;
91 else if(time_cnt < TimeRound)
92 next_state = `ArBy;
93 else
94 next_state = `AgBr;
95 default:
96 next_state = `EMRG;
97 endcase
98 always @(posedge clk or negedge rst_n)
99 if (!rst_n) begin
100 {red_a, green_a, yellow_a} <=#1 3'b100;
101 {red_b, green_b, yellow_b} <=#1 3'b100;
102 end
103 else
104 case(state)
105 `EMRG:
106 begin
107 {red_a, green_a, yellow_a} <=#1 3'b100;
108 {red_b, green_b, yellow_b} <=#1 3'b100;
109 end
110 `AgBr:
111 begin
112 {red_a, green_a, yellow_a} <=#1 3'b010;
113 {red_b, green_b, yellow_b} <=#1 3'b100;
114 end
115 `AyBr:
116 begin
117 {red_a, green_a, yellow_a} <=#1 3'b001;
118 {red_b, green_b, yellow_b} <=#1 3'b100;
119 end
120 `ArBg:
121 begin
122 {red_a, green_a, yellow_a} <=#1 3'b100;
123 {red_b, green_b, yellow_b} <=#1 3'b010;
124 end
125 `ArBy:
126 begin
127 {red_a, green_a, yellow_a} <=#1 3'b100;
128 {red_b, green_b, yellow_b} <=#1 3'b001;
129 end
130 endcase
131
132 endmodule
仿真代码:
1 //==================================================================================================
2 // Filename : TB.v
3 // Created On : 2016-06-19 10:05:43
4 // Last Modified : 2016-06-21 21: |