In: Electrical Engineering
Design an elevator control system for an 8 floors building, draw the logic diagram, and illustrate the system with the following situations:
1. The car is on the ground floor, John Doe wants to go from ground floor to 7th floor;
2. The car is on 8th floor, John Doe wants to go from 6th floor to the ground floor;
3. The car is moving up from 4th floor to 7th floor, John Doe wants to go from 6th floor to the ground floor;
4. The car is moving down from 7th floor to 4th floor, John Doe wants to go from 6th floor to the ground floor.
;################################################################################
;# THIS IS A 3-LEVEL MINI-ELEVATOR PROJECT. IT CONSISTS OF 9 SWITCHES OF #
;# WHICH 3 ARE FOR UP, 3 FOR DOWN, AND 3 FOR LEVEL. THE UP AND DOWN SWITCHES ARE#
;# USED OUTSIDE THE ELEVATOR WHILE THE OTHER 3 SWITCHES ARE FOR THE INSIDE OF #
;# ELEVATOR. #
;################################################################################
;################################################################################
;# THIS IS THE INITIALIZATION PROCESS... IT WILL MAKE SURE THAT THE ELEV #
;# LOCATION IS IN THE GND FLOOR... AFTER DETECTING THE GND FLOOR, IT STOPS THE #
;# MOTOR AND THEN DECLARES ALL PINS AT THE RIGHT SIDE OF AT89C2051 (P1 AND P3.5)#
;# AS INPUTS... #
;################################################################################
ORG 0000H
MOV TMOD,#11H ;SET BOTH TIMERS INTO MODE 1... TIMER 0 FOR WAIT_5S... TIMER 1 FOR TIMER
SETB P3.3 ;SET P3.3,P3.4,P3.5 AS INPUT FOR ELEV LOCATION
SETB P3.4
SETB P3.5
CLR P3.0 ;STOP ELEV FROM MOVING
CLR P3.1
INIT: JNB P3.5,STOP_MOTOR_INIT ;SEE IF ELEV IS IN GND FLOOR
CLR P3.0 ;MOTOR DOWN
SETB P3.1
SJMP INIT ;GO BACK IS ELEV IS NOT YET IN GND FLOOR
STOP_MOTOR_INIT: CALL MOTOR_STOP ;STOP THE MOTOR SINCE ELEV IS IN GND FLOOR
MOV R3,#01H ;R3 IS ELEV LOCATION... ELEV IS IN GND FLOOR
MOV P1,#0FFH ;P1 ARE ALL INPUTS FOR FLOOR SELECTION...
SETB P3.7 ;P3.7 IS AN INPUT TOO FOR 3-DOWN...
;################################################################################
;# THIS IS THE POLLING PROCESS... IT POLLS EVERY SWITCH OF THE FLOOR #
;# SELECTION BY OR-ING THE CORRESPONDING VALUE TO THE R-BANK 0... OR LOGIC #
;# WAS USED INSTEAD OF A MOV OR ANY OTHER COMMAND IN ORDER TO PRESERVE OTHER #
;# FLOOR SELECTIONS WHEN THEY ARE SWITCHED/SELECTED... R0 IS FOR THE UP #
;# FUNCTION, R1 IS FOR THE LEVEL FUNCTION, R2 IS FOR THE DOWN FUNCTION... THE #
;# VALUES ARE: GND FLOOR = 0000 0001, 2ND FLOOR = 0000 0010, #
;# 3RD FLOOR = 0000 0100... ACC IS USED TEMPORARILY TO STORE THESE VALUES AND #
;# TO OR LOGIC THEM ACCORDINGLY... 1U MEANS FIRST FLOOR WITH UP FUNCTION... #
;# 1D MEANS FIRST FLOOR WITH DOWN FUNCTION... 1 MEANS THE FIRST FLOOR SWITCH #
;# INSIDE THE ELEVATOR... I CALL THIS 'LEVEL'... #
;################################################################################
POLL_1U:JB P1.0,POLL_1A ;SEE IF 1UP IS SELECTED
MOV R7,A ;STORE ACC VALUE TEMPORARILY
MOV A,#01H ;0000 0001 IN R0 MEANS THAT 1UP IS SELECTED
ORL A,R0 ;OR LOGIC TO SAVE OTHER SWITCHES THAT WERE SELECTED
MOV R0,A ;SEND TO R0 THE SELECTION FOR UP SWITCHES
MOV A,R7 ;GIVE BACK TO ACC ITS ORIGINAL VALUE
POLL_1A:JB P1.3,POLL_2U ;CKECK THE SWITCH INSIDE ELEV IF GND IS SELECTED
MOV R7,A
MOV A,#01H
ORL A,R1 ;R1 STORES THE SELECTION OF THE SWITCHES INSIDE ELEV...
MOV R1,A
MOV A,R7
POLL_2U:JB P1.1,POLL_2A ;CKECK IF 2UP IS SELECTED
MOV R7,A
MOV A,#02H ;0000 0010 IS THE VALUE FOR 2ND FLOOR
ORL A,R0 ;UP FUNCTION IS STORED IN R0
MOV R0,A
MOV A,R7
POLL_2A: JB P1.4,POLL_3U
MOV R7,A
MOV A,#02H
ORL A,R1
MOV R1,A
MOV A,R7
POLL_3U: JB P1.2,POLL_3A
MOV R7,A
MOV A,#04H
ORL A,R0
MOV R0,A
MOV A,R7
POLL_3A: JB P1.5,POLL_3D
MOV R7,A
MOV A,#04H
ORL A,R1
MOV R1,A
MOV A,R7
POLL_3D: JB P3.7,POLL_3B
MOV R7,A
MOV A,#04H
ORL A,R2
MOV R2,A
MOV A,R7
POLL_3B: JB P1.5,POLL_2D
MOV R7,A
MOV A,#04H
ORL A,R1
MOV R1,A
MOV A,R7
POLL_2D: JB P1.7,POLL_2B
MOV R7,A
MOV A,#02H
ORL A,R2
MOV R2,A
MOV A,R7
POLL_2B: JB P1.4,POLL_1D
MOV R7,A
MOV A,#02H
ORL A,R1
MOV R1,A
MOV A,R7
POLL_1D: JB P1.6,POLL_1B
MOV R7,A
MOV A,#01H
ORL A,R2
MOV R2,A
MOV A,R7
POLL_1B: JB P1.3,UP ;AFTER POLLING EVERY SWITCH, SERVE THEM IF THERE ARE ANY... SEE DESCRIPTION BELOW
MOV R7,A
MOV A,#01H
ORL A,R1
MOV R1,A
MOV A,R7
RET ;RET TO KEEP INPUTS POSTED WHILE IN WAIT_5S, MOTOR_DOWN, MOTOR_UP, MOTOR_STOP, TIMER
;################################################################################
;# AFTER POLLING, THIS PROCEDURE WILL SERVE EACH FLOOR ACCORDINGLY... THE #
;# SEQUENCE OF SERVICE IS JUST LIKE ANY OTHER ELEVATOR... FOR MY SYSTEM, THE #
;# ELEVATOR WILL SERVICE THIS WAY: 1U,1,2U,2,3U,3,3D,3,2D,2,1D,1...
;# IN THIS WAY, THE ELEVATOR HAS A SYSTEM THAT DETERMINES THE FLOORS TO GO.#
;# THE ELEVATOR IS ASSUMED TO BE AT THE GND FLOOR... THEN IT SERVES ANY ONE WHO #
;# SWITCHED UP OR LEVEL BECAUSE THE ELEVATOR IS GOING UP... WHEN THE ELEVATOR #
;# (1)REACHES THE HIGHEST FLOOR OR (2a)THAT THE ELEVATOR HAS REACHED THE HIGHEST#
;# FLOOR VALUE SELECTED BY THE USER AND (2b) NOTHING MORE IS SWITCHED ABOVE IT, #
;# THE ELEVATOR THEN WILL SERVE THE DOWN FUNCTION IF THERE ARE ANY... OTHERWISE,#
;# WHEN THERE WAS AN UP SWITCH LEFT BEHIND DUE TO (e.g.) A SLOW MOVING PERSON #
;# WHO WISHES TO GO UP FROM 2ND FLOOR TO 3RD FLOOR, BUT THE ELEVATOR IS ALREADY #
;# AT THE 3RD FLOOR, THAT PERSON HAS TO WAIT UNTIL THE DOWN FUNCTION ARE FULLY #
;# SERVED OR UNTIL IT REACHES GND FLOOR... WHEN THERE IS NO ONE TO SERVE FOR THE#
;# DOWN FUNCTION, OR THAT THE ELEVATOR HAS REACHED GND FLOOR, THEN THE ELEVATOR #
;# WILL SERVE THE SLOW MOVING PERSON... THE SAME IS THE PROCEDURE FOR THE DOWN #
;# FUNCTION, ONLY INVERTED IN LOGIC... #
;################################################################################
UP: MOV R7,A ;BEFORE STARTING, STORE IN R7 THE ACC SO THAT DOWN PROCEDURE'S 2ND FLOOR CAN BE SERVED OF NECESSARY...
MOV A,R0 ;REMEMBER THAT R0 CONTAINS THE UP SWITCHES... OR LOGIC IT
; WITH R1 TO COMBINE SELECTIONS OF UP AND LEVEL
ORL A,R1 ;ACC HAS THE FLOOR VALUES TO BE SERVED WHEN ELEV GOES UP...
ORL A,R6 ;OR LOGIC AGAIN IF THE SERVICE TO THE 2ND FLOOR IS NECESSARY...
CLR AC ;AC FLAG DETERMINES IF ELEV IS GOING UP OR DOWN... AC=0 FOR UP... AC=1 FOR DOWN
JZ DOWN ;IF ACC IS ZERO (MEANS NO FLOOR WAS SELECTED FOR UP FUNCTION AND INSIDE ELEV),
;THEN CHECK DOWN SWITCHES IF THERE WERE ANY SELECTED
CHECK_1U: JNB ACC.0, CHECK_2U ; IF ACC.0 IS ZERO, THEN IT MEANS THAT NEITHER THE 1UP NOR 1-LEVEL WAS SELECTED... CHECK 2UP IF TRUE...
CALL CHECK_ELEV_LOC ;CHECK ELEVATOR LOCATION TO DETERMINE THE SOURCE OF WHICH THE ELEVATOR IS COMING FROM...
SERVE_1U: JNB P3.5, STOP_MOTOR_1U ;SINCE GND IS LOWEST FLOOR IN THIS PROJECT, SET LOGIC FOR MOTOR-DOWN...
CALL MOTOR_DOWN ; P3.5 IS THE DETECTOR IF THE ELEVATOR HAS REACHED GND FLOOR...
SJMP SERVE_1U
STOP_MOTOR_1U: CLR ACC.0 ;SINCE GND FLOOR IS SERVED, CLEAR THIS BIT...
MOV R3,#01H ;THEN UPDATE R3, WHICH CONTAINS THE VALUE OF THE LOCATION OF THE ELEVATOR...
CALL MOTOR_STOP ;MAKE THE MOTOR STOP BECAUSE DESTINATION IS REACHED...
CALL WAIT_5S ;WAIT 5 SECONDS FOR A SIMULATION THAT THE CARGOS ARE LOADED IN/OUT...
CHECK_2U: JNB ACC.1, CHECK_3U ;AFTER SERVING FIRST FLOOR, DO THE SAME PROCEDURE OF CHECKING TO 2U...
CALL CHECK_ELEV_LOC
SERVE_2U: CJNE R3,#02H, CHECK_2U_CY ;IF EQUAL, MEANS ELEV IS ALREADY IN 2ND FLOOR, IF NOT CHECK CARRY FLAG
JMP SERVE_2U_FROM1 ;WHAT IF ELEV DROPS A LITTLE DUE TO GRAVITY...
;JMP STOP_MOTOR_2U
CHECK_2U_CY:JNC CHECK_2U_AC ;CHECK CY FLAG... CY=1 IF ELEV IS IN GND FLOOR... CY=0 IF ELEV IS IN 3RD FLOOR...
SERVE_2U_FROM1:JNB P3.4, STOP_MOTOR_2U ;IF ELEV IS IN 1ST FLOOR, PROCEED...
CALL MOTOR_UP
JMP SERVE_2U_FROM1
CHECK_2U_AC:JB AC,CHECK_3U ;CY=0, SO ELEV IS IN 3RD FLOOR... CHECK AC FLAG... AC=0 FOR UP PROCEDURE, AC=1 FOR DOWN PROCEDURE
SERVE_2U_FROM3:JNB P3.4, STOP_MOTOR_2U ;IF AC=1, THEN IT MEANS THAT THE ELEVATOR IS SERVING THE DOWN PROCEDURE... SO WAIT UNTIL DOWN PROCEDURE IS FINISHED
CALL MOTOR_DOWN ;WHEN NOTHING WAS SELECTED AND ELEV DID NOT MOVE, IT MEANS ELEV IS IN 3RD FLOOR SO LET MOTOR-DOWN
SJMP SERVE_2U_FROM3 ;WHEN SOMETHING WAS SELECTED, THEN IT MEANS THAT THE ELEV CHANGED LOCATION, SO SERVE 2ND FLOOR ACCORDING TO FUNCTIONS ABOVE...
STOP_MOTOR_2U: CLR ACC.1 ;SINCE 2ND FLOOR IS SERVED, CLEAR THIS BIT, JUST LIKE WHAT I DID TO CHECK-1U
MOV R3,#02H
CALL MOTOR_STOP
CALL WAIT_5S
CHECK_3U: JNB ACC.2, DOWN ;SAME LOGIC AS BEFORE...
CALL CHECK_ELEV_LOC
SERVE_3U: JNB P3.3, STOP_MOTOR_3U
CALL MOTOR_UP
SJMP SERVE_3U
STOP_MOTOR_3U: CLR ACC.2
CALL MOTOR_STOP
MOV R3,#04H
CALL WAIT_5S
;################################################################################
;# THE DOWN PROCEDURE IS ALMOST THE SAME AS THE UP PROCEDURE, ONLY THE #
;# LOGIC FOR THE 2ND FLOOR IS INVERTED... THIS TIME, AFTER THE DOWN, START TIMER#
;# WHENEVER THERE ARE NO FLOORS THAT NEEDS TO BE SERVED... #
;################################################################################
DOWN: MOV R6,A ;BEFORE STARTING, STORE IN R6 THE ACC SO THAT THE UP PROCEDURE'S 2ND FLOOR CAN BE SERVED IF NECESSARY...
MOV A,R2 ;REMEMBER THAT R2 CONTAINS THE DOWN SWITCHES... AGAIN... SAME PROCEDURE...
ORL A,R1 ;THIS TIME, START CHECKING FROM 3RD FLOOR SINCE DECREMENTING A VALUE STARTS FROM
ORL A,R7 ;OR LOGIC AGAIN IF THE SERVICE TO THE 2ND FLOOR IS NECESSARY...
SETB AC ;A HIGHER VALUE...
JZ TIMER ;IF ACC IS ZERO, NO FLOOR IS SELECTED... START THE TIMER...
CHECK_3D: JNB ACC.2,CHECK_2D ;SAME LOGIC AS BEFORE...
CALL CHECK_ELEV_LOC
SERVE_3D: JNB P3.3, STOP_MOTOR_3D
CALL MOTOR_UP
JMP SERVE_3D
STOP_MOTOR_3D: CLR ACC.2
CALL MOTOR_STOP
CALL WAIT_5S
MOV R3,#04H
CHECK_2D: JNB ACC.1, CHECK_1D ;SAME CHECKING AS I DID IN CHECK_2U... ALSO, I INVERTED SOME CONDITIONS...
CALL CHECK_ELEV_LOC
SERVE_2D: CJNE R3, #02H, CHECK_2D_CY ;IF EQUAL, MEANS ELEV IS ALREADY IN 2ND FLOOR, IF NOT CHECK CARRY FLAG
JMP SERVE_2D_FROM1 ;WHAT IF ELEV DROPS A LITTLE DUE TO GRAVITY...
;JMP STOP_MOTOR_2D
CHECK_2D_CY:JC CHECK_2D_AC ;CHECK CY FLAG... CY=1 IF ELEV IS IN GND FLOOR... CY=0 IF ELEV IS IN 3RD FLOOR...
SERVE_2D_FROM3:JNB P3.4, STOP_MOTOR_2D ;IF ELEV IS IN 3RD FLOOR, PROCEED...
CALL MOTOR_DOWN
JMP SERVE_2D_FROM3
CHECK_2D_AC:JNB AC,CHECK_1D ;ELEV IS IN GND FLOOR... CHECK AC FLAG... AC=0 FOR UP PROCEDURE, AC=1 FOR DOWN PROCEDURE
SERVE_2D_FROM1:JNB P3.4, STOP_MOTOR_2D
CALL MOTOR_UP
SJMP SERVE_2D_FROM1
STOP_MOTOR_2D: CLR ACC.1
MOV R3,#02H
CALL MOTOR_STOP
CALL WAIT_5S
CHECK_1D: JNB ACC.0, FINISH
CALL CHECK_ELEV_LOC
SERVE_1D: JNB P3.5, STOP_MOTOR_1D
CALL MOTOR_DOWN
JMP SERVE_1D
STOP_MOTOR_1D: CLR ACC.0
CALL MOTOR_STOP
CALL WAIT_5S
MOV R3,#01H
FINISH: JMP TIMER ;AFTER SERVING ALL FLOORS, CHECK AGAIN AND SERVE THEM...
;################################################################################
;# THE FUNCTIONS BELOW ARE ONLY SUBROUTINES, EXCEPT FOR TIMER, WHICH HAS A #
;# CHARACTERISTIC TO JMP BACK TO POLL_1U WHENEVER THERE IS A FLOOR THAT NEEDS #
;# TO BE SERVED... #
;################################################################################
;################################################################################
;# FIND THE LOCATION OF THE ELEVATOR... THIS WILL UPDATE THE SYSTEM FOR THE#
;# THE LOCATION OF THE ELEVATOR (WHEN DUE TO A LIMITATION THAT MY MECHANICAL #
;# SYSTEM HAS NO STOPPER, GRAVITY CAN LOWER THE ELEVATOR LOCATION)... #
;################################################################################
CHECK_ELEV_LOC: JNB P3.5, MAYBE_2
MOV R3,#01H
MAYBE_2: JNB P3.4, MAYBE_3
MOV R3, #02H
MAYBE_3: JNB P3.3, ELEV_LOCATED
MOV R3,#04H
ELEV_LOCATED: RET ;RETURN BECAUSE ELEV IS ALREADY LOCATED
;################################################################################
;# WAIT 5S FOR PASSENGERS TO GET IN AND OUT OF THE ELEV... ***ONLY FOR #
;# MINI-ELEVATOR DESIGN... THIS CAME FROM TIMER 0... #
;################################################################################
WAIT_5S: MOV R4, #70
WAIT_5S_LOAD: MOV TL0,#00
MOV TH0,#00
SETB TR0
WAIT_5S_TF0: JB TF0, WAIT_5S_R4
CALL POLL_1U ;POLL WHILE WAITING SO THAT THE ELEV CAN STILL INTERACT ON THIS WAIT TIME...
JMP WAIT_5S_TF0
WAIT_5S_R4: CLR TF0
CLR TR0
DJNZ R4, WAIT_5S_LOAD
RET
;################################################################################
;# TO GO BACK TO DEFAULT FLOOR (GND FLOOR) WHEN ELEVATOR IS UNINTERRUPTED #
;# FOR A LONG TIME (15 SECONDS)... #
;################################################################################
TIMER: MOV R5, #211
TIMER_LOAD: MOV TL1,#00
MOV TH1,#00
SETB TR1
TIMER_TF1: JB TF1, TIMER_R5
CALL QUICK_POLL ;QUICK POLL CAN DETERMINE FASTER IF TIMER IS TO BE RESET AND SERVE THE FLOOR...
JB TR1, TIMER_PROCEED
JMP POLL_1U ;IF TR1 = 0, THEN IT MEANS THERE WAS AN INTERRUPTION, OR THAT THERE HAS A FLOOR TO BE SERVED...
TIMER_PROCEED: JMP TIMER_TF1
TIMER_R5: CLR TF1
CLR TR1
DJNZ R5, TIMER_LOAD ; IF R5 REACHES ZERO, THEN GO BACK TO DEFAULT FLOOR (GND FLOOR)
TIMER_FULL: SETB TR1 ;JUST TO CHECK FOR THE INTERRUPTION/QUICK_POLL SUBROUTINE... NOT NECESSARILY FOR THE TIMER...
CALL CHECK_ELEV_LOC ;UPDATE FLOOR FIRST BEFORE GOING TO GND...
CALL QUICK_POLL
JNB TR1, TIMER_FULL_INT
CJNE R3,#02H, TIMER_CHECK_CY ;MEANS THAT ELEV IS ON 2ND FLOOR...
TIMER_FROM_2: JNB P3.5, TIMER_REACHED_1 ;IF EQUAL, THEN IT MEANS THAT ELEV IS IN 2ND FLOOR... MOTOR-DOWN...
CALL QUICK_POLL
JNB TR1, TIMER_FULL_INT
CALL MOTOR_DOWN
JMP TIMER_FROM_2
TIMER_REACHED_1:CALL MOTOR_STOP ;P3.5 DETECTED THAT ELEV IS IN GND FLOOR ALREADY... SO MOTOR-STOP...
MOV R3,#01H
JMP POLL_1U ;BACK TO POLLING...
TIMER_CHECK_CY: JC TIMER_REACHED_1 ;COMPARISON CONFIRMS THAT ELEV IS IN GND FLOOR... SO ELEV'S TIMER_REACHED_1...
MOV R3,#04H ;IF THERE IS NO CARRY, MEANS THAT ELEV IS IN 3RD FLOOR...
TIMER_FROM_3: JNB P3.4, TIMER_FROM_2 ;SO MOTOR-DOWN UNTIL IT REACHES 2ND FLOOR...
CALL QUICK_POLL
JNB TR1, TIMER_FULL_INT
CALL MOTOR_DOWN
JMP TIMER_FROM_3
TIMER_FULL_INT: JMP POLL_1U ;TIMER WAS FULL, BUT WAS INTERRUPTED... SO SERVE THE FLOOR(S) SELECTED BY FIRST FINDING WHICH FLOOR WAS SELECTED...
;################################################################################
;# A QUICK POLL THAT IS USED IN THE TIMER TO RESET THE TIMER IF ANY FLOOR #
;# WAS SELECTED... THE TIMER THEN IS INTERRUPTED IF ANY FLOOR IS SELECTED... #
;################################################################################
QUICK_POLL: JNB P1.7, INTERRUPTED
JNB P1.6, INTERRUPTED
JNB P1.5, INTERRUPTED
JNB P1.4, INTERRUPTED
JNB P1.3, INTERRUPTED
JNB P1.2, INTERRUPTED
JNB P1.1, INTERRUPTED
JNB P1.0, INTERRUPTED
JNB P3.7, INTERRUPTED
JNB ACC.0, INTERRUPTED ;GND FLOOR GENERAL
JNB ACC.1, INTERRUPTED ;2ND FLOOR GENERAL
JNB ACC.2, INTERRUPTED ;3RD FLOOR GENERAL
RET
INTERRUPTED: CLR TR1
MOV R5, #211
MOV TL0,#00
MOV TH0,#00
CLR TF1
RET
;################################################################################
;# MOTOR MECHANISMS TO GO UP, DOWN, OR STOP, BY MANIPULATING TWO PORT BITS #
;# P3.0 AND P3.1... I NEED H-BRIDGE HERE... #
;################################################################################
MOTOR_UP: SETB P3.0
CLR P3.1
CALL POLL_1U ;WHILE DOING THIS FUNCTION, MAKE SURE USER CAN SELECT OTHER FLOORS AS WELL...
RET
MOTOR_DOWN: CLR P3.0
SETB P3.1
CALL POLL_1U
RET
MOTOR_STOP: CLR P3.0
CLR P3.1
CALL POLL_1U
RET
END
this is the code for the elevator