//黄灯亮5秒,转左转灯;
//左转灯桥戚亮15秒,转黄灯;
//黄灯亮5秒,转红灯;
/桥衡/红灯亮55秒,转绿灯;
//lamp从高到低 分别接红敏消做灯 黄灯 绿灯 左转灯
//account接2个数码管
module traffic(clk,en,lamp,acount)
output[7:0] acount
output[3:0] lamp
input clk,en
reg[7:0] num
reg temp
reg[2:0] count
reg[7:0] red,yellow,green,left
reg[3:0] lamp
always @(en)
if(!en)
begin
red<=8'd55 //55 second
yellow<=8'd5
green<=8'd40
left<=8'd15
end
assign acount=num
always @(posedge clk)
begin
if(en)
begin
if(!temp)
begin
temp<=1
case(count)
0:begin num<=greenlamp<=2count<=1end //green
1:begin num<=yellowlamp<=4count<=2end //yellow
2:begin num<=leftlamp<=1count<=3end //left
3:begin num<=yellowlamp<=4count<=4end //yellow
4:begin num<=redlamp<=8count<=0end //red
default:lamp<=8
endcase
end
else
begin
if(num>1)
if(num[3:0]==0)
begin
num[3:0]<=4'b1001
num[7:4]<=num[7:4]-1
end
else
num[3:0]<=num[3:0]-1
if(num==2) temp<=0
end
end
else
begin
lamp<=4'b1000
count<=0
temp<=0
end
end
endmodule
给一个参考的程序(出自王金明:《Verilog HDL 程序设计教程》):语句间隔自己调整下。
//module traffic.v
/*
信号定义与说明:
CLK : 为同步时钟;
EN : 使能信号,为1的话,则控制器开始工作;
LAMPA : 控制A方向四盏灯的亮灭;其中,LAMPA0~LAMPA3,分别控制A方向的
左拐灯、绿灯、黄灯和红灯;
LAMPB : 控制B方向四盏灯的亮灭;其中,LAMPB0~LAMPB3,分别控制B方向的
左拐灯、绿灯、黄灯和红灯;
ACOUNT : 用于A方向灯的时间显示,8位,可驱动两个数码管;
BCOUNT : 用于B方向灯的时间显示,8位,可驱动两闭乎个数码管。
*/
module traffic(CLK,EN,LAMPA,LAMPB,ACOUNT,BCOUNT)
output [7:0] ACOUNT,BCOUNT
output [3:0] LAMPA,LAMPB
input CLK,EN
reg [7:0] numa,numb
regtempa,tempb
reg [2:0] counta,countb
reg [7:0] aleft,agreen,ayellow,ared
reg [7:0] bleft,bgreen,byellow,bred
reg [3:0] LAMPA,LAMPB
always @ (EN)
if(!EN)
begin
ared <= 8'd55
ayellow <= 8'd5
agreen <= 8'd40
aleft <= 8'轿扒悉d15
bred <= 8'd65
byellow <= 8'd5
bleft <= 8'd15
bgreen <= 8'd30
end
assign ACOUNT = numa
assign BCOUNT = numb
always @ (posedge CLK)
begin
if(EN)
begin
if(!tempa)
begin
tempa<=1'b1
case(counta)
0: begin numa<=agreen LAMPA<=4'd2counta<=3'd1end
1: begin numa<=ayellowLAMPA<=4'd4counta<=3'd2end
2: begin numa<=aleft LAMPA<=4'd1counta<=3'd3end
3: begin numa<=ayellowLAMPA<=4'd4counta<=3'd4end
4: begin numa<=ared LAMPA<=4'd8counta<=3'd0end
default: LAMPA<=4'd8
endcase
end
else
begin
if(numa>1)
if(numa[3:0]==4'd0) begin
numa[3:0]<=4'hf
numa[7:4]<=numa[7:4]-1'b1
end
else numa[3:0]<=numa[3:0]-1'b1
if(numa==2) tempa<=1'b0
end
end
else
begin
LAMPA <=4'd8
counta<=3'd0
tempa <=1'b0
end
end
此拿 always @ (posedge CLK)
begin
if(EN)
begin
if(!tempb)
begin
tempb<=1
case(countb)
0: begin numb<=bred LAMPB<=4'd8countb<=3'd1end
1: begin numb<=bgreenLAMPB<=4'd2countb<=3'd2end
2: begin numb<=byellowLAMPB<=4'd4countb<=3'd3end
3: begin numb<=bleftLAMPB<=4'd1countb<=3'd4end
4: begin numb<=byellowLAMPB<=4'd4countb<=3'b0end
default :LAMPB<=4'd8
endcase
end
else
begin
if(numb>1)
if(numb[3:0]==4'b0)
begin
numb[3:0]<=4'hf
numb[7:4]<=numb[7:4]-1'b1
end
else numb[3:0]<=numb[3:0]-1'b1
if(numb==4'd2)
tempb<=1'b0
end
end
else
begin
LAMPB <=4'd8
tempb <=1'b0
countb<=3'b0
end
end
endmodule
module traffic(clk,urgency,east_west,south_north,led)input clk
input urgency
output [7:0]east_west,south_north
output [5:0]led
reg [7:0]east_west,south_north
reg [5:0]led
initial begin
east_west<=8'b0
south_north<=8'b0
led<=6'b100001end
always @(posedge clk)
begin if(urgency==1) led<=6'b100100
else if(east_west==8'b0 &&south_north==8'b0) begin
east_west<=8'b00101101
south_north<=8'b00101000
led<=6'b100001end
else if(east_west==8'b00000110 &&south_north==8'b1) begin
east_west<=8'b00000101
south_north<=8'b00000101
led<=6'b100010end
else if(east_west==8'b1 &&south_north==8'b1 &&led[5]==1'b1) begin
east_west<=8'b00101000
south_north<=8'b00101101
led<=6'b001100end
else if(east_west==8'b1 &&south_north==8'b00000110) begin
east_west<=8'b00000101
south_north<=8'b00000101
led<弯带胡=6'b010100end
else if(east_west==8'b1 &&south_north==8'b1 &&led[2]==1'b1) begin
east_west<=8'b00101101
south_north<=8'b00101000
led<=6'b100001end
else if(east_west[3:0]==4'b0000) begin
east_west<=east_west-8'b111
south_north<=south_north-1'b1end
else if(south_north[3:0]==4'b0000) begin
east_west<=east_west-1'b1
south_north<=south_north-8'b111end
else begin
east_west<=east_west-1'b1
south_north<=south_north-1'b1
end
end
endmodule
自己前段时间写的简单交通灯控制模块。定义了两埋拦个输入,三个输出。两个输入分别是时钟信号和紧急情况信号。输出信号分别是南北,东西,以及led灯信号。具体实现行禅方式是先给紧急情况下定义个状态,然后通过计数器来实现各个状态的改变。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)