1 module elevator_control(clk,up1,up2,up3,up4,down2,down3,down4,down5,request1,request2,request3,request4,request5,en,open,close,full,broken, 2 motion_state,lift_floor,door_state1,door_state2,over_alarm,led_indicate,broken_flag);//,count_out); 3 input clk;//时钟信号 4 input up1,up2,up3,up4;//各个楼层的升上请求; 5 input down2,down3,down4,down5;//各个楼层的下降请求; 6 input request1,request2,request3,request4,request5;//电梯内到达楼层请求; 7 input open,close;//电梯内打开门请求,快速关门请求; 8 input en,broken,full;//电梯故障信号,超载信号; 9 output[2:0] motion_state;//电梯上下指示运行状态; 10 output[6:0] lift_floor;//电梯当前楼层 11 output[6:0] door_state1,door_state2;//门当前状态指示;00表示已关闭,01表示已打开,10表示正在关门; 12 output over_alarm;//超载报警指示 13 output[4:0] led_indicate;//led指示电梯当前状态,上升为右移,下降为左移,停止为全亮,故障为全灭; 14 output broken_flag;//故障指示 15 //output[6:0] count_out;//计数器输出,便于我控制开关门的状态显示 16 wire real_clk; 17 reg[2:0] motion_state; 18 reg[6:0] lift_floor; 19 reg[6:0] door_state1,door_state2; 20 reg[4:0] position; 21 reg over_alarm; 22 reg[4:0] led_indicate; 23 reg broken_flag; 24 //reg[6:0] count_out; 25 reg up11,up22,up33,up44;//电梯外上升请求信号寄存信号 26 reg down22,down33,down44,down55;//电梯外下降请求信号寄存信号 27 reg request11,request22,request33,request44,request55;//电梯内请求信号寄存信号 28 reg[2:0] q1,q2;//控制关门延时计数器 29 reg flash;//控制数码管闪烁变量 30 reg opendoor;//开门使能信号 31 reg[1:0] up_down_flag;//电梯运动方向信号寄存器 32 //reg en_up,en_down,up_down_state;//预备上升,下降操作使能信号 33 reg[4:0] up,down,request,up_down_req;//电梯内外请求信号寄存器 34 reg[6:0] LIFT_STATE,NEXT_STATE; 35 parameter UP_FLAG=2'b01,DOWN_FLAG=2'b10,STATIC=2'b00; 36 parameter FLOOR0=7'b1000000,FLOOR1=7'b1111001,FLOOR2=7'b0100100;//电梯的楼层 37 parameter FLOOR3=7'b0110000,FLOOR4=7'b0011001,FLOOR5=7'b0010010,HEXMIE=7'b1111111; 38 //parameter LED1=5'b10000,LED2=5'b00001,LED3=5'b11111,LED4=5'b00000;//led的花样状态准备 39 //parameter STATE_LIFT1=14'b11110001000111,STATE_LIFT2=14'b11100011000111,STATE_LIFT3=14'b0111111101111111;//上,下,停指示 40 parameter WAIT=7'b0000001,UP=7'b0000010,DOWN=7'b0000100,UP_STOP=7'b0001000,DOWN_STOP=7'b0010000,OPEN_DOOR=7'b0100000,CLOSE_DOOR=7'b1000000; 41 parameter FLOOR11=5'b00001, FLOOR22=5'b00010, FLOOR33=5'b00100, FLOOR44=5'b01000, FLOOR55=5'b10000;//电梯楼层常量 42 assign real_clk=(clk&(~broken)); 43 always@(posedge real_clk) 44 begin 45 if(request1) 46 request11<=request1; 47 else if(request2) 48 request22<=request2; 49 else if(request3) 50 request33<=request3; 51 else if(request4) 52 request44<=request4; 53 else if(request5) 54 request55<=request5; 55 end 56 always@(posedge real_clk) 57 begin 58 if(up1) 59 up11<=up1; 60 else if(up2) 61 up22<=up2; 62 else if(up3) 63 up33<=up3; 64 else if(up4) 65 up44<=up4; 66 end 67 always@(posedge real_clk) 68 begin 69 if(down2) 70 down22<=down2; 71 else if(down3) 72 down33<=down3; 73 else if(down4) 74 down44<=down4; 75 else if(down5) 76 down55<=down5; 77 end 78 always@(posedge real_clk) 79 begin 80 up[4:0]={'b0,up44,up33,up22,up11}; 81 down[4:0]={down55,down44,down33,down22,'b0}; 82 request[4:0]={request55,request44,request33,request22,request11}; 83 up_down_req=up|down|request; 84 //motion_state=updown1; 85 end 86 always@(posedge real_clk or posedge en) 87 begin 88 if(en) 89 begin 90 q1<=3'b000;q2<=3'b000; 91 end 92 else if((NEXT_STATE==OPEN_DOOR)&&q1<3)//门默认停留三秒 93 begin 94 //NEXT_STATE<=OPEN_DOOR; 95 q1<=q1+1; 96 end 97 else if(((NEXT_STATE==UP)||(NEXT_STATE==DOWN))&&q2<5) 98 begin 99 //NEXT_STATE<=UP; 100 q2<=q2+1; 101 end 102 else 103 begin 104 q1<=0;q2<=0;//清零以备下次使用 105 end 106 end 107 always@(posedge real_clk or posedge en) 108 begin 109 if(en) 110 begin 111 LIFT_STATE<=WAIT; 112 end 113 else 114 begin 115 LIFT_STATE<=NEXT_STATE; 116 end 117 end 118 always@(LIFT_STATE or up or down or request or position or q1 or up_down_flag or open or close) 119 begin 120 case(LIFT_STATE) 121 WAIT: 122 begin 123 if(up_down_req||open) 124 begin 125 if((up&position)||(down&position)||open) 126 NEXT_STATE=OPEN_DOOR; 127 else if((up>position)||(down>position)) 128 NEXT_STATE=UP; 129 else if((up<position)||(down<position)) 130 NEXT_STATE=DOWN; 131 else if(request>0) 132 begin 133 if((position&request)||open) 134 NEXT_STATE=OPEN_DOOR; 135 else if(request>position) 136 NEXT_STATE=UP; 137 else 138 NEXT_STATE=DOWN; 139 end 140 end 141 else 142 begin 143 NEXT_STATE=WAIT; 144 end 145 UP: 146 begin 147 if(up_down_req)//有上升、下降或停靠请求 148 begin 149 if((request&position)||(up&position))//当前楼层停靠、上升请求 150 begin 151 NEXT_STATE=UP_STOP; 152 end 153 else if((request>position)||(up>position))//比当前楼层高的楼层有上升、停靠请求 154 begin 155 NEXT_STATE=UP; 156 end 157 else if(request||up)//比当前楼层低的楼层有上升、停靠请求 158 begin 159 NEXT_STATE=DOWN; 160 end 161 else if(down)//有下降请求 162 begin 163 if((down&position)&&(position<FLOOR55))//比五楼低的当前楼层有下降请求 164 begin 165 NEXT_STATE=UP_STOP; 166 end 167 else if((down&position)&&(position==FLOOR55))//五楼有下降请求 168 begin 169 NEXT_STATE=DOWN_STOP; 170 end 171 else if((down>position)&&((down^position)>position))//比当前楼层高且比五楼低的楼层有下降请求 172 begin 173 NEXT_STATE=UP; 174 end 175 else //比当前楼层低的楼层有下降请求 176 begin 177 NEXT_STATE=DOWN; 178 end 179 end 180 end 181 else 182 begin 183 NEXT_STATE=WAIT; 184 end 185 end 186 DOWN: 187 begin 188 if(up_down_req) 189 begin 190 if((down&position)&&(request&position)) 191 begin 192 NEXT_STATE=DOWN_STOP; 193 end 194 else if((down&FLOOR11)&&((down&FLOOR11)<position)||(down&FLOOR22)&&((down&FLOOR22)<position)||(down&FLOOR33)&&((down&FLOOR33)<position)||(down&FLOOR44)&&((down&FLOOR44)<position)||(down&FLOOR55)&&((down&FLOOR55)<position)) 195 begin 196 NEXT_STATE=DOWN; 197 end 198 else if((request&FLOOR11)&&((request&FLOOR11)<position)||(request&FLOOR22)&&((request&FLOOR22)<position)||(request&FLOOR33)&&((down&FLOOR33)<position)||(down&FLOOR44)&&((down&FLOOR44)<position)||(down&FLOOR55)&&((down&FLOOR55)<position)) 199 begin 200 NEXT_STATE=DOWN; 201 end 202 else if(request||down) 203 begin 204 NEXT_STATE=UP; 205 end 206 else if(up) 207 begin 208 if(((up&FLOOR11)<position)&&(up&FLOOR11)||((up&FLOOR22)<position)&&(up&FLOOR22)||((up&FLOOR33)<position)&&(up&FLOOR33)||((up&FLOOR44)<position)&&(up&FLOOR44)||((up&FLOOR55)<position)&&(up&FLOOR55)) 209 begin 210 NEXT_STATE=DOWN; 211 end 212 else if((up&position)&&(position>FLOOR11)) 213 begin 214 NEXT_STATE=DOWN_STOP; 215 end 216 else if((up&position)&&(position==FLOOR11)) 217 begin 218 NEXT_STATE=UP_STOP; 219 end 220 else 221 begin 222 NEXT_STATE=UP; 223 end 224 end 225 end 226 else 227 begin 228 NEXT_STATE=WAIT; 229 end 230 end 231 UP_STOP: 232 begin 233 if(q2<3'b100) 234 begin 235 NEXT_STATE=UP; 236 end 237 else 238 begin 239 NEXT_STATE=OPEN_DOOR; 240 q2=0;flash='b1; 241 end 242 end 243 DOWN_STOP: 244 begin 245 if(q2<3'b100) 246 begin 247 NEXT_STATE=DOWN; 248 end 249 else 250 begin 251 NEXT_STATE=OPEN_DOOR; 252 q2=0;flash='b1; 253 end 254 end 255 OPEN_DOOR: 256 begin 257 flash='b0; 258 if(q1<3'b010) 259 begin 260 NEXT_STATE=OPEN_DOOR; 261 end 262 else if (CLOSE) 263 begin 264 NEXT_STATE=CLOSE_DOOR; 265 end 266 else 267 begin 268 NEXT_STATE=CLOSE_DOOR; 269 end 270 end 271 CLOSE_DOOR: 272 begin 273 if(up_down_flag==UP_FLAG) 274 begin 275 if((request&position)||(up&position)||open) 276 277 NEXT_STATE=OPEN_DOOR; 278 else if((request>position)||(up>position)) 279 NEXT_STATE=UP; 280 else if(down>0) 281 begin 282 if((down>position)&&(down^position>position)) 283 NEXT_STATE=UP; 284 else if(down<position)//验证代码的正确? 285 NEXT_STATE=DOWN; 286 else 287 NEXT_STATE=OPEN_DOOR;//验证代码的正确? 288 end 289 else if(request||up) 290 NEXT_STATE=DOWN; 291 end 292 else if(up_down_flag==DOWN_FLOG) 293 begin 294 if((request&position)||(down&position)||open) 295 NEXT_STATE=OPEN_DOOR; 296 else if((down&FLOOR11)&&((down&FLOOR11)<position)||(down&FLOOR22)&&((down&FLOOR22)<position)||(down&FLOOR33)&&((down&FLOOR33)<position)||(down&FLOOR44)&&((down&FLOOR44)<position)||(down&FLOOR55)&&((down&FLOOR55)<position)) 297 NEXT_STATE=DOWN; 298 else if((request&FLOOR11)&&((request&FLOOR11)<position)||(request&FLOOR22)&&((request&FLOOR22)<position)||(request&FLOOR33)&&((down&FLOOR33)<position)||(down&FLOOR44)&&((down&FLOOR44)<position)||(down&FLOOR55)&&((down&floor55)<position)) 299 NEXT_STATE=DOWN; 300 else if(down||request) 301 NEXT_STATE=up; 302 else if(up>0) 303 begin 304 if((up&FLOOR11)&&((up&FLOOR11)<position)||(up&FLOOR22)&&((up&FLOOR22)<position)||(up&FLOOR33)&&((up&FLOOR33)<position)||(up&FLOOR44)&&((up&FLOOR44)<position)||(up&FLOOR55)&&((up&FLOOR55)<position)) 305 NEXT_STATE=DOWN; 306 else if(up>position) 307 NEXT_STATE=UP; 308 else 309 NEXT_STATE=OPEN_DOOR; 310 end 311 else 312 NEXT_STATE=WAIT; 313 end 314 else 315 begin 316 if((up&position)||(down&postion)) 317 NEXT_STATE=OPEN_DOOR; 318 else if((up>position)||(down>position)) 319 NEXT_STATE=UP; 320 else if(up||down) 321 NEXT_STATE=DOWN; 322 else if(request>0) 323 begin 324 if(request&position) 325 NEXT_STATE=OPEN_DOOR; 326 else if(request>position) 327 NEXT_STATE=UP; 328 else 329 NEXT_STATE=DOWN; 330 end 331 else 332 NEXT_STATE=WAIT; 333 end 334 end 335 end 336 default: 337 NEXT_STATE=WAIT; 338 endcase 339 end 340 always@(posedge real_clk or posedge en or broken) 341 begin 342 if(broken) 343 begin 344 broken_flag<='b1; 345 end 346 else 347 begin 348 if(en) 349 begin 350 broken_flag<='b0; 351 position<=FLOOR11; 352 door_state1<=FLOOR0; 353 door_state2<=FLOOR0; 354 //led_indicate<=LED4; 355 end 356 else 357 begin 358 broken_flag<='b0; 359 led_indicate<=position; 360 if(full) 361 begin 362 over_alarm<='b1; 363 end 364 else 365 begin 366 over_alarm<='b0; 367 case(NEXT_STATE) 368 WAIT: 369 begin 370 position<=position; 371 door_state1<=FLOOR0; 372 door_state2<=FLOOR0; 373 //led_indicate<=LED4; 374 up_down_flag<=STATIC; 375 end 376 UP: 377 begin 378 position=position<<1; 379 door_state1<=FLOOR0; 380 door_state2<=FLLOR0; 381 up_down_flag<=UP_FLAG; 382 end 383 DOWN: 384 begin 385 position<=position>>1; 386 door_state1<=FLOOR0; 387 door_state2<=FLLOR0; 388 up_down_flag<=DOWN_FLAG; 389 end 390 UP_STOP: 391 begin 392 position<=position; 393 door_state1<=FLOOR0; 394 door_stat2<=FLOOR0; 395 up_down_flag<=UP_FLAG; 396 end 397 DOWN_STOP: 398 begin 399 position<=position; 400 door_state1<=FLOOR0; 401 door_stat2<=FLOOR0; 402 up_down_flag<=DOWN_FLAG; 403 end 404 OPEN_DOOR: 405 begin 406 position<=position; 407 door_state1<=FLOOR0; 408 door_state2<=FLOOR1; 409 up_down_flG<=up_down_flag; 410 end 411 CLOSE_DOOR: 412 begin 413 position<=position; 414 door_state1<=FLOOR1; 415 door_state2<=FLOOR0; 416 up_down_floor<=up_down_floor; 417 end 418 default: 419 begin 420 position<=FLOOR11; 421 door_state1<=FLOOR0; 422 door_state2<=FLOOR0; 423 up_down_flag<=WAIT; 424 end 425 endcase 426 end 427 end 428 end 429 end 430 always@(pos or flash) 431 begin 432 if(flash) 433 lift_floor=HEXMIE; 434 else 435 begin 436 case(pos) 437 FLOOR11: 438 lift_floor<=FLOOR1; 439 FLOOR22: 440 lift_floor<=FLOOR2; 441 FLOOR33: 442 lift_floor<=FLOOR3; 443 floor44: 444 lift_floor<=FLOOR4; 445 floor55: 446 lift_state<=FLOOR5; 447 default: 448 lift_state<=7'bz; 449 endcase 450 end 451 end 452 endmodule 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475