|
发表于 2005-3-24 23:25:26
|
显示全部楼层
真心跪求!!!!交通灯设计
我自己写的红绿黄带左转四色交通灯verilog程序
给你吧。
module jtd(clk,en,reset,showa,showb,statea,stateb);//clk 时钟,en控制端,showa为A方向时间显示,showb为B方向时间显示,
//statea,stateb为控制端,控制AB方向的红绿灯
input clk,en,reset;
output showa,showb;
output statea,stateb;
reg[7:0] showa,showb;
reg[3:0] statea,stateb;
reg[0:2] state,next_state;
//state,next_state为当前状态和下一状态
//独热编码
parameter A_Green=4'b0001,
A_Left=4'b0010,
A_Yellow=4'b0100,
A_Red=4'b1000,
B_Green=4'b0001,
B_Left=4'b0010,
B_Yellow=4'b0100,
B_Red=4'b1000;
always @(posedge reset)
begin
if(reset) ///如果reset==1给初始状态
begin
state<=3'd0;//初始状态为0
statea<=A_Green; //A初始状态为绿灯
stateb<=B_Red; //B初始状态为红灯
showa<=8'd20;//A方向的初始时间为20个周期
showb<=8'd35;//B方向的初始时间为35个周期
next_state=3'd1;//初始状态的下一状态为1
end
end
always @(state)
begin
case(state)
3'd0: begin statea<=A_Green;
stateb<=B_Red;
showa<=8'd20;
showb<=8'd35;
next_state=3'd1;
end
3'd1:begin statea<=A_Left;
stateb<=B_Red;
next_state=3'd2;
showa<=8'd10;
end
3'd2: begin statea<=A_Yellow;
stateb<=B_Red;
next_state=3'd3;
showa<=8'd5;
end
3'd3:
begin statea<=A_Red;
stateb<=B_Green;
showa<=8'd35;
showb<=8'd20;
next_state=3'd4;
end
3'd4:
begin statea<=A_Red;
stateb<=B_Left;
showb<=8'd10;
next_state=3'd5;
end
3'd5: begin statea<=A_Red;
stateb<=B_Yellow;
next_state=3'd0;
showb<=8'd5;
end
default: //state总共3位,共只有6个状态,没有全部使用了
begin statea<=A_Green;
stateb<=B_Red;
showa<=8'd20;
showb<=8'd35;
next_state=3'd1;
end
endcase
end
always @(posedge clk)
begin
if(en) if((showa==0)||(showb==0))//当A方向和B方向的时间有一个为0的话,就要切换状态
begin
state<=next_state;
end
end
always @(posedge clk)//时间计数
begin
if(en)
begin
showa<=showa-1;
showb<=showb-1;
end
end
endmodule
|
|