⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 traffic_light.v

📁 Traffic light written with Verilog
💻 V
📖 第 1 页 / 共 2 页
字号:
`define TRUE  1'b1
`define FALSE 1'b0

`define GtoS_delay 15
`define StoR_delay  2
`define GtoY_delay  2
`define YtoR_delay  2
`define RtoG_delay  2

`define CLOCK 16'd20000
`define COUNT 16'd1000
`include "counter.v"
`include "switch.v"
`include "settime.v"
`include "display.v"

module traffic_light
			( clock, buttom, switches, en_sw,
					Ncar, Ecar, Nman, Eman, scan , display_time );


input clock, buttom, en_sw;
input [7:0] switches;

output [2:0] Ncar, Ecar;
reg [2:0] Ncar, Ecar;
output [2:0] Nman, Eman;
reg [2:0] Nman, Eman;


output [3:0] scan;
wire [3:0] scan;
output [7:0] display_time;
wire [7:0] display_time;

parameter RED = 3'b001,
          YELLOW = 3'b010,
          GREEN = 3'b100,
		  SPARKLE = 3'b010;

//State definition      Ncar		Nman		Ecar		Eman
parameter S0 = 4'd0,  //GREEN		GREEN		RED			RED
		  S1 = 4'd1,  //GREEN		SPARKLE		RED			RED
          S2 = 4'd2,  //GREEN		RED			RED			RED
          S3 = 4'd3,  //YELLOW		RED			RED			RED
          S4 = 4'd4,  //RED			RED			RED			RED
          S5 = 4'd5,  //RED			RED			GREEN		GREEN		
		  S6 = 4'd6,  //RED			RED			GREEN		SPARKLE
          S7 = 4'd7,  //RED			RED			GREEN		RED
          S8 = 4'd8,  //RED			RED			YELLOW		RED
          S9 = 4'd9;  //RED			RED			RED			RED

parameter Zero  = 4'b0000;
parameter One   = 4'b0001;
parameter Two   = 4'b0010;
parameter Three = 4'b0011;
parameter Four  = 4'b0100;
parameter Five  = 4'b0101;
parameter Six   = 4'b0110;
parameter Seven = 4'b0111;
parameter Eight = 4'b1000;
parameter Nine  = 4'b1001;
parameter No	= 4'b1111;


reg [3:0] state;
reg [3:0] next_state;
reg check1;
reg check2;
reg next_check1;
reg next_check2;
reg [7:0] next_count_sec;
reg [7:0] set_sec;

reg temp1;
reg temp2;


//contact with module Counter
reg reset;
reg [7:0] count_sec;
wire no_work;
wire [7:0] display_sec;
wire [7:0] count_out;
Counter count( clock, reset, count_sec , count_out );

wire dis_work, change;
Switch switch1( clock, !buttom, dis_work, change );

//module SetTime( t_clk, t_sw, d_second, t_diswork , set_sec, t_scan );

wire [7:0] t_sec;
SetTime set_display( clock, en_sw,  switches, count_out, no_work, t_sec, display_time ,scan );

/*
wire temp_Nman;
wire temp_Eman;
assign temp_Nman = !Nman[1];
assign temp_Eman = !Eman[1];
*/
reg en;
wire [15:0] cq;
adderes added( clock, en, cq );


always @(state or temp1 or temp2)
begin
	case(state)
		S0:	begin  Ncar=GREEN;  Nman=GREEN;  Ecar=RED;    Eman=RED;		end
		S1:	begin  Ncar=GREEN;  Nman=SPARKLE;Ecar=RED;    Eman=RED;		Nman[2]=temp1;end
		S2:	begin  Ncar=GREEN;  Nman=RED; 	 Ecar=RED;    Eman=RED;		end
		S3:	begin  Ncar=YELLOW; Nman=RED;	 Ecar=RED;    Eman=RED; 	end
		S4:	begin  Ncar=RED;    Nman=RED;	 Ecar=RED;    Eman=RED;		end
		S5:	begin  Ncar=RED;    Nman=RED;    Ecar=GREEN;  Eman=GREEN;	end
		S6:	begin  Ncar=RED;    Nman=RED;    Ecar=GREEN;  Eman=SPARKLE;	Eman[2]=temp2;end
		S7:	begin  Ncar=RED;    Nman=RED;    Ecar=GREEN;  Eman=RED; 	end
		S8:	begin  Ncar=RED;    Nman=RED;    Ecar=YELLOW; Eman=RED;		end
		S9:	begin  Ncar=RED;    Nman=RED;    Ecar=RED;    Eman=RED;		end
  default: begin  Ncar=GREEN;  Nman=GREEN;  Ecar=RED;    Eman=RED;		end
	endcase
end
////////////////////////////////////

always @(negedge clock)
 begin
  	if(Nman[1]==1)
	  begin
		en=1;
		if( cq == `COUNT )
		 begin
			temp1 = !temp1;
			en=0;
	 	 end
	 end
    if ( Eman[1]==1)
	  begin
	 	en=1;
		if( cq == `COUNT )
		 begin
			temp2 = !temp2;
			en=0; 
		 end
	  end
 end

 


///////////////////////////////

always @( posedge clock )
 begin
	state= next_state;
	reset=0;

//if press switch is 1
	case (state)
		S0:	
		 begin
			check1=next_check1;
			check2=next_check2;

			if( check1 == 0 )
			 begin
				count_sec = `GtoS_delay;
				next_check1=1;
			 end
			
			else if( check2 == 0 )
			 begin
				count_sec = next_count_sec;
				next_check2=1;
			 end

			else 
			 begin
				if( no_work == 0 )
				 begin
					if ( dis_work == 0 )
					 begin
						if ( count_out==0 )
						 begin
							next_state = S1;
							next_check1=0;
							reset=1;
						 end
						else
							next_state = S0;
					 end
					else
						reset=1;

					if ( change==1 )
					 begin
						next_state = S1;
						next_check1=0;
						reset=1;
					 end
				 end
				else
				 begin
					reset=1;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

					next_check2=0;			

				 end
			 end
		 end

		S1:	
		 begin
			check1=next_check1;
			check2=next_check2;

			if( check1 == 0 )
			 begin
				count_sec = `StoR_delay;
				next_check1=1;
			 end
			
			else if( check2 == 0 )
			 begin
				count_sec = next_count_sec;
				next_check2=1;
			 end

			else 
			 begin
				if( no_work == 0 )
				 begin
					if ( dis_work == 0 )
					 begin
						if ( count_out==0 )
						 begin
							next_state = S2;
							next_check1=0;
							reset=1;
						 end
						else
							next_state = S1;
					 end
					else
						reset=1;

					if ( change==1 )
					 begin
						next_state = S2;
						next_check1=0;
						reset=1;
					 end
				 end
				else
				 begin
					reset=1;
					next_check2=0;			
				 end
			 end
		 end

		S2:	
		 begin
			check1=next_check1;
			check2=next_check2;

			if( check1 == 0 )
			 begin
				count_sec = `GtoY_delay;
				next_check1=1;
			 end
			
			else if( check2 == 0 )
			 begin
				count_sec = next_count_sec;
				next_check2=1;
			 end

			else 
			 begin
				if( no_work == 0 )
				 begin
					if ( dis_work == 0 )
					 begin
						if ( count_out==0 )
						 begin
							next_state = S3;
							next_check1=0;
							reset=1;
						 end
						else
							next_state = S2;
					 end
					else
						reset=1;

					if ( change==1 )
					 begin
						next_state = S3;
						next_check1=0;
						reset=1;
					 end
				 end
				else
				 begin
					reset=1;
					next_check2=0;			
				 end
			 end
		 end

		S3:	
		 begin
			check1=next_check1;
			check2=next_check2;

			if( check1 == 0 )
			 begin
				count_sec = `YtoR_delay;
				next_check1=1;
			 end
			
			else if( check2 == 0 )
			 begin
				count_sec = next_count_sec;
				next_check2=1;
			 end

			else 
			 begin
				if( no_work == 0 )
				 begin
					if ( dis_work == 0 )
					 begin
						if ( count_out==0 )
						 begin
							next_state = S4;
							next_check1=0;
							reset=1;
						 end
						else
							next_state = S3;
					 end
					else
						reset=1;

					if ( change==1 )
					 begin
						next_state = S4;
						next_check1=0;
						reset=1;
					 end
				 end
				else
				 begin
					reset=1;
					next_check2=0;			
				 end
			 end
		 end

		S4:	
		 begin
			check1=next_check1;
			check2=next_check2;

			if( check1 == 0 )
			 begin
				count_sec = `RtoG_delay;
				next_check1=1;
			 end
			
			else if( check2 == 0 )
			 begin
				count_sec = next_count_sec;
				next_check2=1;
			 end

			else 
			 begin
				if( no_work == 0 )
				 begin
					if ( dis_work == 0 )
					 begin
						if ( count_out==0 )
						 begin
							next_state = S5;
							next_check1=0;
							reset=1;
						 end
						else
							next_state = S4;
					 end
					else
						reset=1;

					if ( change==1 )
					 begin
						next_state = S5;
						next_check1=0;
						reset=1;
					 end
				 end
				else
				 begin
					reset=1;
					next_check2=0;			
				 end
			 end
		 end

		S5:	
		 begin
			check1=next_check1;
			check2=next_check2;

			if( check1 == 0 )
			 begin
				count_sec = `GtoS_delay;
				next_check1=1;
			 end
			
			else if( check2 == 0 )
			 begin
				count_sec = next_count_sec;
				next_check2=1;
			 end

			else 
			 begin
				if( no_work == 0 )
				 begin
					if ( dis_work == 0 )
					 begin
						if ( count_out==0 )
						 begin
							next_state = S6;
							next_check1=0;
							reset=1;
						 end
						else
							next_state = S5;
					 end
					else
						reset=1;

					if ( change==1 )
					 begin

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -