/*
blue 00_11_11
yellow 11_00_11
blue 11_11_00
0 bu liang
1 liang
*/
module traffic(led_l,led_r,clk,seg_reg1,seg_reg2,inter_key,key1,key2);
input clk,inter_key,key1,key2;
output [6:0] seg_reg1,seg_reg2;
output [5:0] led_l,led_r;
reg [6:0] dispdat1,dispdat2,seg_reg1,seg_reg2;
reg [6:0] reg_dispdat1,reg_dispdat2;
reg [25:0] count;
reg [1:0] led_yellow_l;
reg [1:0] led_blue_l;
reg [1:0] led_red_l;
reg [5:0] led_reg_l;
reg [1:0] led_yellow_r;
reg [1:0] led_blue_r;
reg [1:0] led_red_r;
reg [5:0] led_reg_r;
reg [3:0] flag_shuma0,flag_shuma1;
reg [5:0] flag_led_l,flag_led_r;
reg flag1,flag2,flag;
reg clk_1hz;
reg [1:0] flag_rgy_status;
reg sec;
reg [1:0] rgy_status;
reg [7:0] rgy_time_l,rgy_time_s,rgy_time,rgy_time_init;
initial
begin
flag_rgy_status=2'd0;
rgy_status=flag_rgy_status;
rgy_time_init=8'h30; //set status 0 initial time,long time.
rgy_time_l=rgy_time_init;
rgy_time_s=8'h20;
led_reg_l=6'b000000;
led_reg_r=6'b000000;
flag1=1'b0;
flag2=1'b0;
//flag=1'b0;
end
////////////////////////fen ping chan sheng 2HZ shi zhong/////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
always @(posedge clk)
begin
/*
if(flag==1'b1)
begin
//rgy_time_l=8'h0;
end
else*/
begin
count=count+1;
if(count==26'd2500_0000) //should be 2500_0000
begin
count=0;
sec=~sec; //2 HZ
clk_1hz=sec;
end
end
end
///////////////////////////////status 0,long time/////////////////////////////////////////////////
always @(posedge sec)
begin
if(rgy_status==2'd0)
begin
flag_shuma0=rgy_time_l[3:0];
flag_shuma1=rgy_time_l[7:4];
if(rgy_time_l==8'h0)
begin
rgy_status=2'd1;
//rgy_time_l=8'h30;
rgy_time_s=8'h20; //set status 1 time,short time
//led_red=led_reg[3:0];
end
else
begin
led_reg_l=6'b110000; //blue
led_reg_r=6'b000011; //red
rgy_time_l=rgy_time_l-1'b1; //status 0,long time.
if(rgy_time_l[3:0]>9)
rgy_time_l[3:0]=9;
end
end
////////////////////////////////////status 1,short time/////////////////////////////////////////////
if(rgy_status==2'd1)
begin
flag_shuma0=rgy_time_s[3:0];
flag_shuma1=rgy_time_s[7:4];
if(rgy_time_s==8'h0)
begin
rgy_status=2'd2;
//if(inter_key==0&&key1==1&&key2==1)
rgy_time_l=rgy_time_init; //set status 2 time,long time.
end
else
begin
led_reg_l=6'b001100; //yellow
led_reg_r=6'b000011; //red
rgy_time_s=rgy_time_s-1; //status 1,short time
if(rgy_time_s[3:0]>9)
rgy_time_s[3:0]=9;
end
end
////////////////////////////////////status 2,long time//////////////////////////////////////
if(rgy_status==2'd2)
begin
flag_shuma0=rgy_time_l[3:0];
flag_shuma1=rgy_time_l[7:4];
if(rgy_time_l==8'h0)
begin
rgy_status=2'd3;
rgy_time_s=8'h20; //set status 3 time,short time.
end
else
begin
led_reg_l=6'b000011; //red
led_reg_r=6'b110000; //blue
rgy_time_l=rgy_time_l-1; //status 2,long time.
if(rgy_time_l[3:0]>9)
rgy_time_l[3:0]=9;
end
end
//////////////////////////////////status 3///////////////////////////////////////////
if(rgy_status==2'd3)
begin
flag_shuma0=rgy_time_s[3:0];
flag_shuma1=rgy_time_s[7:4];
if(rgy_time_s==8'h0)
begin
rgy_status=2'd0; //return to status 0
// rgy_time_init=8'h30;
rgy_time_l=rgy_time_init; //set status 0 time,long time.
rgy_time_s=8'h20;
end
else
begin
led_reg_l=6'b000011; //red
led_reg_r=6'b001100; //yellow
rgy_time_s=rgy_time_s-1; //status 3,short time
if(rgy_time_s[3:0]>9)
rgy_time_s[3:0]=9;
end
end
end
////////////////////////////xian shi ji shi///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
/////////////////////super time(red------->red)/////////////////////////////////////////////
always
begin
dispdat1=flag_shuma0;
dispdat2=flag_shuma1;
if(inter_key==0) //jin ji kai guan wei an xia
begin
flag_led_l=led_reg_l;
flag_led_r=led_reg_r;
reg_dispdat1=dispdat1;
reg_dispdat2=dispdat2;
flag<=1'b0;
if(flag1==0&&flag2==0)
if((key1!=0)&&(key2!=0)&&(flag1==0)&&(flag2==0)) //jian wei an xia
begin
rgy_time_init=8'h30;
end
end
else //jin ji kai guan an xia
begin
dispdat1=4'h10;
dispdat2=4'h10;
flag_led_l=6'b000011;
flag_led_r=6'b000011;
flag<=1'b1;
reg_dispdat1=rgy_time_init[3:0];
reg_dispdat2=rgy_time_init[7:4];
if(key1!=0) //jian wei an xia
begin
//rgy_time_init=8'h30;
end
else //jian an xia
begin
//reg_dispdat1=reg_dispdat1+1;
flag1=1'b1;
rgy_time_init[3:0]=rgy_time_init[3:0]+1;
if(rgy_time_init[3:0]==4'h9)
rgy_time_init[3:0]=4'h0;
//reg_dispdat1=rgy_time_init[3:0];
end
if(key2!=0) //jian wei an xia
begin
//rgy_time_init=8'h30;
end
else //jian an xia
begin
//reg_dispdat2=reg_dispdat2+1;
flag2=1'b1;
rgy_time_init[7:4]=rgy_time_init[7:4]+1;
|
文件: |
学校试验_交通灯控制系统测试版(六)(带紧急按键,变量分离,时间可调,必须调后才正确,调时led闪).rar |
大小: |
828KB |
下载: |
下载 | |
if(rgy_time_init[7:4]==4'h9)
rgy_time_init[7:4]=4'h0;
//reg_dispdat2=rgy_time_init[7:4];
end
end
end
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////cun zai wen ti(dai wan shan)///////////////////////////
///////////////////////////cun zai wen ti///////////////////////////////////////
always @(reg_dispdat1)
begin
case(reg_dispdat1)
4'h0:seg_reg1=7'b1000000;//0
4'h1:seg_reg1=7'b1111001;//1
4'h2:seg_reg1=7'b0100100;//2
4'h3:seg_reg1=7'b0110000;//3
4'h4:seg_reg1=7'b0011001;//4
4'h5:seg_reg1=7'b0010010;//5
4'h6:seg_reg1=7'b0000010;//6
4'h7:seg_reg1=7'b1111000;//7
4'h8:seg_reg1=7'b0000000;//8
4'h9:seg_reg1=7'b0010000;//9
4'h10:seg_reg1=7'b1111111;//not display
endcase
if(flag==1'b1)
begin
seg_reg1[6]=seg_reg1[6]&clk_1hz;
seg_reg1[5]=seg_reg1[5]&clk_1hz;
seg_reg1[4]=seg_reg1[4]&clk_1hz;
seg_reg1[3]=seg_reg1[3]&clk_1hz;
seg_reg1[2]=seg_reg1[2]&clk_1hz;
seg_reg1[1]=seg_reg1[1]&clk_1hz;
seg_reg1[0]=seg_reg1[0]&clk_1hz;
if(seg_reg1==7'b0000_000)
seg_reg1=7'b1111_111;
end
end
//////////////////////////////////////////////////////////////////////////////////////////
always @(reg_dispdat2)
begin
case(reg_dispdat2)
4'h0:seg_reg2=7'b1000000;//0
4'h1:seg_reg2=7'b1111001;//1
4'h2:seg_reg2=7'b0100100;//2
4'h3:seg_reg2=7'b0110000;//3
4'h4:seg_reg2=7'b0011001;//4
4'h5:seg_reg2=7'b0010010;//5
4'h6:seg_reg2=7'b0000010;//6
4'h7:seg_reg2=7'b1111000;//7
4'h8:seg_reg2=7'b0000000;//8
4'h9:seg_reg2=7'b0010000;//9
4'h10:seg_reg2=7'b1111111;//not display
endcase
if(flag==1'b1)
begin
seg_reg2[6]=seg_reg2[6]&clk_1hz;
seg_reg2[5]=seg_reg2[5]&clk_1hz;
seg_reg2[4]=seg_reg2[4]&clk_1hz;
seg_reg2[3]=seg_reg2[3]&clk_1hz;
seg_reg2[2]=seg_reg2[2]&clk_1hz;
seg_reg2[1]=seg_reg2[1]&clk_1hz;
seg_reg2[0]=seg_reg2[0]&clk_1hz;
if(seg_reg2==7'b0000_000)
seg_reg2=7'b1111_111;
end
end
assign led_l=flag_led_l;
assign led_r=flag_led_r;
endmodule
阅读(986) | 评论(0) | 转发(0) |