题解 | #交通灯#

交通灯

https://www.nowcoder.com/practice/b5ae79ff08804b61ad61f749eaf157ba

`timescale 1ns/1ns

module triffic_light(
		input rst_n, //异位复位信号,低电平有效
        input clk, //时钟信号
        input pass_request,
		output reg[7:0]clock,
        output wire red,
		output wire yellow,
		output wire green
    );

parameter LED_RED = 2'd0,IDLE = 2'd1,LED_GREEN = 2'd2,LED_YELLOW2GREEN = 2'd3;
reg [1:0] state,next;
//reg [7:0]clock;

always@(posedge clk or negedge rst_n)
	if(!rst_n)begin
		state <= IDLE;
	end
	else begin
		state <= next;
	end

always@(*)(1444584)
	if(!rst_n)begin
		next = IDLE;
	end
	else begin
		case(state)
			IDLE:begin
				if(clock == 8'd8)begin
					next = LED_RED;
				end
				else begin
					next = IDLE;
				end
			end
			LED_GREEN:begin
				if(clock == 8'd1)begin
					next = LED_RED;
				end
				else begin
					next = LED_GREEN;
				end
			end
			
			LED_RED:begin
				if(clock == 8'd1)begin
					next = LED_YELLOW2GREEN;
				end
				else begin
					next = LED_RED;
				end
			end
			LED_YELLOW2GREEN:begin
				if(clock == 8'd1)begin
					next = LED_GREEN;
				end
				else begin
					next = LED_YELLOW2GREEN;
				end
			end
		endcase
	end
	
always@(posedge clk or negedge rst_n)
	if(!rst_n)begin
		clock  <= 8'd10;
	end
	else begin
		case(next)
			IDLE:begin
				clock <= clock - 1'd1;
			end
			LED_GREEN:begin				
				if(pass_request)begin
					if(clock > 8'd10)begin
						clock <= 8'd10;
					end
					else begin
						clock <= clock - 1'd1;
					end
				end
				else if(clock == 8'd1)begin
					clock <= 8'd60;
				end
				else begin
					clock <= clock - 1'd1;	
				end
			end
			
			LED_RED:begin
				if((clock == 8'd1)||((state == IDLE)&&(clock <= 8'd8)))begin
					clock <= 8'd10;
				end
				else begin
					clock <= clock - 1'd1;	
				end
			end
			LED_YELLOW2GREEN:begin
				if(clock == 8'd1)begin
					clock <= 8'd5;
				end
				else begin
					clock <= clock - 1'd1;	
				end
			end
		endcase
	end

assign red = (state == LED_RED);
assign green = (state == LED_GREEN);
assign yellow = (state == LED_YELLOW2GREEN);
endmodule

全部评论

相关推荐

霁华Tel:秋招结束了,好累。我自编了一篇对话,语言别人看不懂,我觉得有某种力量在控制我的身体,我明明觉得有些东西就在眼前,但身边的人却说啥也没有,有神秘人通过电视,手机等在暗暗的给我发信号,我有时候会突然觉得身体的某一部分不属于我了。面对不同的人或场合,我表现出不一样的自己,以至于都不知道自己到底是什么样子的人。我觉得我已经做的很好,不需要其他人的建议和批评,我有些时候难以控制的兴奋,但是呼吸都让人开心。
点赞 评论 收藏
分享
头像
09-29 16:18
门头沟学院 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务