【FPGA】Verilog实现交通信号灯

大二数字电路的课程设计中,有一份日常作业使用Xilinx FPGA实现简易交通信号灯,但很可惜当时时间有限,没能最终完成。正好在这一学期选修SOPC设计课程,同样采用了Xilinx FPGA,故打算重新完成交通信号灯的相关内容。

本项目采用Digilent公司生产的BASYS3开发板,基于Xilinx FPGA,该板子目前可以在马云家买到,不过价格偏贵,在校学生可在digilent官网申请以更低价格买入。

大致的框架如下,只是个构思,还很不完善,后续会进行修改。比如现在我目前并没有把计时功能完全从State模块中摘出来,只是用Timer实例化了一个1s计时器在里面,并且用count计数。

下面是State部分的代码,目前还有很多需要修改完善的地方,比如后续会将State中的count计数也摘出来。注释部分由于Vivado自带编辑器过于磕碜(没有办法字体回退),而我又很不喜欢微软雅黑和宋体,所以只好用我的工地英语写一点注释了。

module BASYS_BIGPROJECT_State(
input clk,
output reg[1:0]state,
output reg[3:0]count
    );
wire clk_1;
Freq_divider #(27)clk_1s(clk,0,,clk_1);
always@(posedge clk_1)
begin
    count <= count + 1;
end

//green led for 4s, yellow led for 2s
always@(posedge clk)
begin
    case(state)
    2'b00:begin //main red, branch green
        if(count<4) begin
            state <= 2'b00;
        end
        else begin
            state <= 2'b01;
            count = 3'b000;
        end
    end
    2'b01:begin //main red, branch yellow
        if(count<2) begin
            state <= 2'b01;
        end
        else begin
            state <= 2'b11;
            count = 3'b000;
        end
    end
    2'b11:begin //main green, branch red
        if(count<4) begin
            state <= 2'b11;
        end
        else begin 
            state <= 2'b10;
            count = 3'b000;
        end
    end
    2'b10:begin //main yellow, branch red
        if(count<2) begin
            state <= 2'b10;
        end
        else begin
            state <= 2'b00;
            count = 3'b000;
        end
    end
    endcase
end
endmodule

猜你喜欢

转载自www.cnblogs.com/acct-zcw/p/11562966.html
今日推荐