📄 lift2.v
字号:
module lift2(clk, //1KHz reset, //reset signal,low(0) active //maintain, //maintain signal,low(0) active up1, //1st floor ,request for up,high active up2, //2nd floor ,request for up,high active up3, //3rd floor,request for up,high active //up4, //4th floor ,request for up,high active //up5, //5th floor ,request for up,high active down2, //2th floor ,request for down,high active down3, //3th floor ,request for down,high active down4, //4th floor ,request for down,high active inter_button1, //inside of the lift,request for 1st floor,high active inter_button2, //inside of the lift,request for 2nd floor,high active inter_button3, //inside of the lift,request for 3rd floor,high active inter_button4, //inside of the lift,request for 4th floor,high active close_door, //the signal for close the lift'door,high active open_door, //the signal for open the lift'door,high active floor, //the signal for the lift arriving at the floor,high active led_up1, //output led_up2, //output led_up3, //output led_down2, //output led_down3, //output led_down4, //output inter1, //output inter2, //output inter3, //output inter4, //output status, //output 1--operate 0--stop door, //output 0--close,1--open number, //output r_up1,r_up2,r_up3,r_down2,r_down3,r_down4, r_inter1,r_inter2,r_inter3,r_inter4 );input clk,reset/*,maintain*/;input up1,up2,up3,down2,down3,down4;input inter_button1,inter_button2,inter_button3,inter_button4;input close_door,open_door;input floor;output led_up1,led_up2,led_up3;output led_down2, led_down3, led_down4;output inter1,inter2,inter3,inter4;output door; //0--close,1--openoutput [1:0]number;output status;output [1:0]r_up1,r_up2,r_up3,r_down2,r_down3,r_down4, r_inter1,r_inter2,r_inter3,r_inter4;reg led_up1,led_up2,led_up3;reg led_down2, led_down3, led_down4;reg inter1,inter2,inter3,inter4;reg door;reg [1:0]direction; //11--up,00--downreg [1:0]cnt; //count for the floorreg [2:0]cnt_time; //count the time for the doorreg status; //operate--1,stop--0reg [1:0]r_up1,r_up2,r_up3,r_down2,r_down3,r_down4;reg [1:0]r_inter1,r_inter2,r_inter3,r_inter4;parameter f1=2'd0,//1st floor f2=2'd1,//2nd floor f3=2'd2,//3rd floor f4=2'd3;//4th floor parameter s1=2'd0,//1st floor s2=2'd1,//2nd floor s3=2'd2,//3rd floor s4=2'd3;//4th floorwire t1=( r_up1!=f1 | r_inter1!=f1 );//-----------------judge the lift's storey----------------// always @(posedge clk or negedge reset)begin if (!reset ) cnt<=f1; else if (status==0) cnt<=cnt; else if (status==1'b1 &&floor==1'b1) begin if (direction==2'b11) cnt<=cnt+1; else if (direction==0) cnt<=cnt-1; else cnt<=cnt; end else cnt<=cnt;endassign number=cnt; //-----------------the lift's door------------------------//always @(posedge clk or negedge reset )begin if(!reset ) door<=1'b0; else if(status==1'b1) //the lift is operating. door<=1'b0; else if(close_door && (status==0 &&floor==1) ) door<=1'b0; else if(cnt_time==3'd5 && (status==0 &&floor==1) ) door<=1'b0; else if( cnt==f1 && s1==0) door<=1'b0; else if (status==0 && floor==1 /*&& cnt_time==3'd0*/) //the lift is stopping. door<=1'b1; else if (open_door&&(status==1'b0 &&floor==1'b1)) door<=1'b1; else door<=door;endalways @(posedge clk or negedge reset /*or posedge door*/)begin if(!reset) cnt_time<=3'd0; else if (door==1'b1) cnt_time<=cnt_time+1; else cnt_time<=3'd0;end//------------------the lift control---------------------//always @(posedge clk or negedge reset) //the lift's request ---- the 1st floor begin if(!reset ) begin led_up1<=1'b0; r_up1<=2'b11; end else if(up1) begin r_up1<=f1; led_up1<=1'b1; end else if(cnt==s1) begin led_up1<=1'b0; r_up1<=2'b11; end end always @(posedge clk or negedge reset) //the lift's request ----the 2nd floor begin if(!reset ) begin led_up2<=1'b0; r_up2<=2'b00; end else if(up2) begin r_up2<=f2; led_up2<=1'b1; end else if(cnt==s2) begin led_up2<=1'b0; r_up2<=2'b00; end end always @(posedge clk or negedge reset) //the lift's request begin if(!reset ) begin led_up3<=1'b0; r_up3<=2'b00; end else if (up3) begin r_up3<=f3; led_up3<=1'b1; end else if (cnt==s3) begin led_up3<=1'b0; r_up3<=2'b00; end end always @(posedge clk or negedge reset) //the lift's request begin if(!reset ) begin led_down2<=1'b0; r_down2<=2'b00; end else if(down2) begin r_down2<=f2; led_down2<=1'b1; end else if(cnt==s2) begin led_down2<=1'b0; r_down2<=2'b00; end end always @(posedge clk or negedge reset) //the lift's request begin if(!reset ) begin led_down3<=1'b0; r_down3<=2'b00; end else if(down3) begin r_down3<=f3; led_down3<=1'b1; end else if(cnt==s3) begin led_down3<=1'b0; r_down3<=2'b00; end end always @(posedge clk or negedge reset) //the lift's request begin if(!reset ) begin led_down4<=1'b0; r_down4<=2'b00; end else if(down4) begin r_down4<=f4; led_down4<=1'b1; end else if(cnt==s4) begin led_down4<=1'b0; r_down4<=2'b00; end end always @(posedge clk or negedge reset) //the lift's request begin if(!reset ) begin inter1<=1'b0; r_inter1<=2'b11; end else if(inter_button1) begin inter1<=1'b1; r_inter1<=f1; end else if(cnt==s1) begin inter1<=1'b0; r_inter1<=2'b11; end endalways @(posedge clk or negedge reset) //the lift's request begin if(!reset ) begin inter2<=1'b0; r_inter2<=2'b00; end else if(inter_button2) begin inter2<=1'b1; r_inter2<=f2; end else if(cnt==s2) begin inter2<=1'b0; r_inter2<=2'b00; end end always @(posedge clk or negedge reset) //the lift's request begin if(!reset ) begin inter3<=1'b0; r_inter3<=2'b00; end else if(inter_button3) begin inter3<=1'b1; r_inter3<=f3; end else if(cnt==s3) begin inter3<=1'b0; r_inter3<=2'b00; end end always @(posedge clk or negedge reset) //the lift's request begin if(!reset ) begin inter4<=1'b0; r_inter4<=2'b00; end else if(inter_button4) begin inter4<=1'b1; r_inter4<=f4; end else if(cnt==s4) begin inter4<=1'b0; r_inter4<=2'b00; end end wire tt = (r_up1==f1|r_up2==f2|r_up3==f3|r_down2==f2|r_down3==f3|r_down4==f4|r_inter2==f2|r_inter3==f3|r_inter4==f4) ; always @ (posedge clk or negedge reset)begin if (!reset) direction<=2'b10; else begin case(cnt) s1:begin if (r_up1==f1|r_up2==f2|r_up3==f3|r_down2==f2|r_down3==f3|r_down4==f4|r_inter2==f2|r_inter3==f3|r_inter4==f4) direction<=2'b11; else direction<=2'b11; end s2:begin if(direction==2'b11) begin if ( r_up2==f2 | r_up3==f3 | r_down4==f4 | r_down3==f3 | r_inter3==f3 | r_inter4==f4 ) direction<=2'b11; else if (r_up1==f1 | r_down2==f2 | r_inter1==f1 ) direction<=2'b00; end else if(direction==2'b00) begin if(r_up1==f1|r_down2==f2|r_inter1==f1) direction<=2'b00; else if (r_up2==f2 | r_up3==f3 | r_down4==f4 | r_down3==f3 | r_inter3==f3 | r_inter4==f4 ) direction<=2'b11; end else direction<=2'b10; end s3:begin if(direction==2'b11) begin if( r_inter4==f4 | r_up3==f3 | r_down4==f4 ) direction<=2'b11 ; else if(r_inter1==f1 | r_inter2==f2) direction<=2'b00 ; else if(r_down2==f2|r_up2==f2) direction<=2'b00 ; else if(r_up1==f1) direction<=2'b00 ; end else if(direction==2'b00) begin if ( r_up1==f1 | r_up2==f2 | r_down2==f2 | r_down3==f3 | r_inter1==f1 | r_inter2==f2 ) direction<=0; else if ( r_up3==f3 | r_down4==f4 | r_inter4==f4 ) direction<=2'b11; end else direction<=2'b10; end s4:begin if( r_up1==f1 | r_up2==f2 | r_up3==f3 | r_down2==f2 | r_down3==f3 | r_down4==f4 | r_inter1==f1 | r_inter2==f2 | r_inter3==f3 ) direction<=2'b00; else direction<=2'b10; end default:direction<=2'b10; endcase endendalways @( posedge clk or negedge reset) begin if (!reset) status<=1'b0; else if ( (floor==0) && (r_up1==f1|r_up2==f2|r_up3==f3|r_down2==f2|r_down3==f3|r_down4==f4|r_inter1==f1|r_inter2==f2|r_inter3==f3|r_inter4==f4)) status<=1'b1; else if (floor &&(r_up1!=f1|r_up2!=f2|r_up3!=f3|r_down2!=f2|r_down3!=f3|r_down4!=f4|r_inter1!=f1|r_inter2!=f2|r_inter3!=f3|r_inter4!=f4)) status<=1'b0; else status<=status; endendmodule
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -