qq_63152113 2022-05-24 22:08 采纳率: 0%
浏览 77

verilog交通灯


module traffic(en,clk,rst,num1,num2,light1,light2,out1,out2,out3,out4,in1,in2);
input en,clk,rst,in1,in2; //en为使能端,in1,in2控制红灯和绿灯亮的时间
output reg[7:0]num1,num2;
output reg[2:0]light1,light2;
output reg[6:0]out1,out2,out3,out4;//输出部分四个数码管
reg tim1,tim2;
reg [1:0]state1,state2;
reg [7:0]red1,red2,green1,green2,yellow1,yellow2;
parameter s0=2'b00,s1=2'b01,s2=2'b10,s3=2'b11;//三个循环状态
reg[31:0]count;
reg clk1hz;

always@(en or in1 or in2)
if(en)
begin//设置计数初值
    if(in1)begin green1<=8'b00110000;red2<=8'b00110000;end
    else begin green1<=8'b01100000;red2<=8'b01100000;end
    if(in2)begin green2<=8'b00100000;red1<=8'b00100000;end
    else begin green2<=8'b01000000;red1<=8'b01000000;end
    yellow1<=8'b00000101;yellow2<=8'b00000101;
end

else begin 
        green1<=8'b0;green2<=8'b0;
        red1<=8'b0;red2<=8'b0;
        yellow1<=8'b0;yellow2<=8'b0;
        end
    
always@(posedge clk)//分频器,50MHz
if(count==32'd25000000)begin clk1hz<=1;count<=count+1;end
    else if(count==32'd50000000)begin clk1hz<=0;count<=0;end
    else count<=count+1'b1;
    
always@(posedge clk1hz)//主干道
begin
if(rst)//复位控制
    begin
    state1<=s0;
    light1<=3'b001;
    num1<=green1;
    end
else if(en)//使能端有效开始控制计数
    begin
        if(!tim1)//计数控制
        begin//交通灯点亮
        tim1<=1;
        case(state1)
        s0:begin num1<=green1;light1<=3'b001;state1<=s1;end
        s1:begin num1<=yellow1;light1<=3'b010;state1<=s2;end
        s2:begin num1<=red1;light1<=3'b100;state1<=s0;end
        default:light1<=3'b100;
        endcase
        end

        else
        begin//倒数计时
        if(num1>0)
            if(num1[3:0]==0)
            begin
                num1[3:0]<=4'b1001;
                num1[7:4]<=num1[7:4]-1'b1;//十位数减一
                end
            else
            num1[3:0]<=num1[3:0]-1'b1;//个位数减一
        if(num1==1)tim1<=0;
        end
    end

    else
    begin//使能端无效时全部变为初态
        light1<=3'b111;
        state1<=s0;
        tim1<=0;
        end
    end
    
always@(posedge clk1hz)
begin
        if(rst)
        begin
            light2<=3'b100;
            num2<=red2;
            end
        else if(en)
        begin
            if(!tim2)
            begin
                tim2<=1;
                case(state2)
                s0:begin num2<=red2;light2<=3'b100;state2<=s1;end
                s1:begin num2<=yellow2;light2<=3'b010;state2<=s2;end
                s2:begin num2<=green2;light2<=3'b001;state2<=s3;end
                s3:begin num2<=yellow2;light2<=3'b010;state2<=s0;end
                default:light2<=3'b100;
                endcase
                end
                
            else 
            begin
                if(num2>0)
                if(num2[3:0]==0)
                begin
                    num2[3:0]<=4'b1001;
                    num2[7:4]<=num2[7:4]-1'b1;
                    end
                else num2[3:0]<=num2[3:0]-1'b1;
                if(num2==1) tim2<=0;
                end
            end
            end
            
always@(posedge clk)
begin//七段数码管显示
case(num1[3:0])
4'b0000:out1<=~7'b0111111;
4'b0001:out1<=~7'b0000110;
4'b0010:out1<=~7'b1011011;
4'b0011:out1<=~7'b1001111;
4'b0100:out1<=~7'b1100110;
4'b0101:out1<=~7'b1101101;
4'b0110:out1<=~7'b1111101;
4'b0111:out1<=~7'b0000111;
4'b1000:out1<=~7'b1111111;
4'b1001:out1<=~7'b1101111;
default:out1<=7'b0111111;
endcase
end

always@(posedge clk)
begin
case(num1[7:4])
4'b0000:out2<=~7'b0111111;
4'b0001:out2<=~7'b0000110;
4'b0010:out2<=~7'b1011011;
4'b0011:out2<=~7'b1001111;
4'b0100:out2<=~7'b1100110;
4'b0101:out2<=~7'b1101101;
4'b0110:out2<=~7'b1111101;
4'b0111:out2<=~7'b0000111;
4'b1000:out2<=~7'b1111111;
4'b1001:out2<=~7'b1101111;
default:out2<=7'b0111111;
endcase
end

always@(posedge clk)
begin
case(num2[3:0])
4'b0000:out3<=~7'b0111111;
4'b0001:out3<=~7'b0000110;
4'b0010:out3<=~7'b1011011;
4'b0011:out3<=~7'b1001111;
4'b0100:out3<=~7'b1100110;
4'b0101:out3<=~7'b1101101;
4'b0110:out3<=~7'b1111101;
4'b0111:out3<=~7'b0000111;
4'b1000:out3<=~7'b1111111;
4'b1001:out3<=~7'b1101111;
default:out3<=7'b0111111;
endcase
end

always@(posedge clk)
begin
case(num2[7:4])
4'b0000:out4<=~7'b0111111;
4'b0001:out4<=~7'b0000110;
4'b0010:out4<=~7'b1011011;
4'b0011:out4<=~7'b1001111;
4'b0100:out4<=~7'b1100110;
4'b0101:out4<=~7'b1101101;
4'b0110:out4<=~7'b1111101;
4'b0111:out4<=~7'b0000111;
4'b1000:out4<=~7'b1111111;
4'b1001:out4<=~7'b1101111;
default:out4<=7'b0111111;
endcase
end
endmodule

在仿真波形时输出很奇怪,有没有人知道仿真应该怎么设置呀。软件是quartus2

img

img

  • 写回答

1条回答 默认 最新

  • 老皮芽子 2022-05-25 09:36
    关注

    这贴子你发了两遍吧,前个贴子已经给你答复了。你没看吗?再给你发一遍。
    代码有些问题,激励代码也有问题,激励代码你没发,自己去改一下。主要是激励代码中rst时间太短,改长点,按你的代码需要1秒以上的rst
    1: count 没有初始值,需要在 rst 下赋值或声明变量时赋初始值。
    2:always@(posedge clk1hz) 这样的逻辑中,时钟只有 1HZ ,rst 激励信号,要等最少 1 秒以上。你给的太少。
    3:你这个是1秒的时钟,你模拟仿真需要有足够的耐心才能得到结果,估计需要数十分钟时间才能看到秒时钟。建议在仿真时将秒时钟的分频参数整小点,最后烧程序时再改回来。
    4:tim1,tim2,state2 也没有赋初始值

    评论

报告相同问题?

问题事件

  • 创建了问题 5月24日

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型