|
楼主 |
发表于 2012-9-11 09:49:10
|
显示全部楼层
代码如下:
module traffic_state
(
clk_1Hz, nrst, no_pass,
one_data, ten_data,
led_r1, led_y1, led_g1,
led_r2, led_y2, led_g2
);
input clk_1Hz, nrst;
input no_pass;
output [3:0]one_data, ten_data; //倒计时个位和十位
output led_r1, led_y1, led_g1;
output led_r2, led_y2, led_g2;
/*******************************/
parameter state_0 = 2'b00,
state_1 = 2'b01,
state_2 = 2'b10,
state_3 = 2'b11; //四种工作状态
/******************************交通状态转换*****************************/
reg red_1, yellow_1, green_1;
reg red_2, yellow_2, green_2;
reg flag; //倒计时赋值标志位
reg [1:0]CS, NS; //Current state, Next state
reg [3:0]count_H, count_L;
always @ ( posedge clk_1Hz or negedge nrst )
if ( !nrst )
CS <= state_0;
else
CS <= NS;
always @ ( CS, no_pass, flag, count_H, count_L )
begin
NS = 2'bx;
case( CS )
state_0: //状态state_0, 主干道通行35s
begin
if( !no_pass )
begin
if( !flag )
begin
state_0_init;
flag <= 1'b1;
end
else
begin
if( !count_H && !count_L ) //如果倒计时结束,则转至state_1状态
begin
NS <= state_1;
flag <= 1'b0;
count_H <= 4'b0000;
count_L <= 4'b0000;
end
else if( !count_L )
begin
count_L <= 4'b1001;
count_H <= count_H-1'b1;
end
else
begin
count_L <= count_L-1'b1;
end
end
end
end
state_1: //状态state_1,主干道黄灯倒计时5s
begin
if( !no_pass )
begin
if( !flag )
begin
state_1_init;
flag <= 1'b0;
end
else
begin
if( !count_L )
begin
NS <= state_2;
flag <= 1'b0;
count_H <= 4'b0000;
count_L <= 4'b0000;
end
else
begin
count_L <= count_L-1'b1;
end
end
end
end
state_2: //状态state_2,支干道通行25s
begin
if( !no_pass )
begin
if( !flag )
begin
flag <= 1'b1;
state_2_init;
end
else
begin
if( !count_H && !count_L )
begin
NS <= state_3;
flag <= 1'b0;
count_H <= 4'b0000;
count_L <= 4'b0000;
end
else if( !count_L )
begin
count_L <= 4'b1001;
count_H <= count_H-1'b1;
end
else
begin
count_L <= count_L-1'b1;
end
end
end
end
state_3: //状态state_3,支干道黄灯倒计时5s
begin
if( !no_pass )
begin
if( !flag )
begin
flag <= 1'b1;
state_3_init;
end
else
begin
if( !count_L ) //如果倒计时结束,则转到state_0状态
begin
NS <= state_0;
flag <= 1'b0;
count_H <= 4'b0000;
count_L <= 4'b0000;
end
else
begin
count_L <= count_L-1'b1;
end
end
end
end
default:
NS <= state_0;
endcase
end |
|