ORG 00H
MAIN:
MOV P1, #00H ; motor
MOV P2, #00H ; buttons P2.0-2.2, sensors P2.3-2.5
FLOOR1:
JNB P2.0, FLOOR2 ; check button
SETB P1.1 ; start motor to go down
JNB P2.3, $ ; check sensor, cont until it reaches 1st floor
CLR P1.1 ; stops motor
SJMP MAIN
FLOOR2:
JNB P2.1, FLOOR3
JNB P2.3, FLOOR2DOWN ; check if lift is on floor 1
SETB P1.0 ; starts motor upwards
JNB P2.4, $
CLR P1.0
SJMP MAIN
FLOOR2DOWN:
JNB P2.1, FLOOR3
JNB P2.5, FLOOR2 ; check if lift is on floor 3
SETB P1.1
JNB P2.4, $
CLR P1.1
SJMP MAIN
FLOOR3:
JNB P2.2, MAIN ; check button floor 3
SETB P1.0
JNB P2.5, $ ; cont until it reaches floor 3
CLR P1.0
SJMP MAIN
END
I am working on a very simple model elevator using 8051 microcontroller (above is my assembly coding for it). It will have:
- Ground Floor
- First Floor and,
- Second floor.
I have trouble in configuring the coding with sensors (I will use IR sensors later). Now I am using pushbutton to replace the sensor.
The thing is, when I press button on my keypad which is a "pushbutton" the motor ONLY keeps running as long as i keep holding and pressing the switch.
And I need the motor to be running until it reaches a certain floor and sensor detect it reached that floor and stops.
The elevator motor should work like, it will check all the sensors and if there is elevator car in that respective floor it will move the elevator car/cage to that position and motor will stop.
Best Answer
Design first, code later. Plan everything out using logic, flow diagrams, state diagrams, etc. Your problems are with the logic, not the coding. Jonk has done 8051, I have not. I have done 6502 and 6800 assembly, these are similar.
At first you might be tempted to have states based on physical position.
But, an elevator really only has 3 states, going up, going down, stopped.
This is only one possible design, there are other valid designs. I have made some assumptions, if I got some wrong, you may need to scrap all this and start over.
All of your buttons and sensors are momentary, you need variables to remember the previous (last) button/sensor activated for each of these. A variable can be stored in a memory location or a register. I have used long descriptive names, in assembler you probably want shorter names.
Floor_Selected: a variable to remember what floor the user has selected. I have assumed a queue of one.
Last_Floor_Sensor: a variable to remember what floor sensor was activated last.
The third is not as obvious, but without it, the elevator will switch directions mid-floor, you probably don't want that.
Next_Floor_Selected: If the elevator is in motion, remember where to go next.
You also need a variable to represent the state of the elevator: Elev_State: let: Stop = 0, Up = 1, Down = 2
Now, the basic flow:
Notes:
The loop must be iterated fast, the buttons and sensor must be polled fast.
If multiple buttons are pressed at the same time, you will need to arbitrarily prioritize them. It doesn't really matter, the last button released will get priority.
I have guessed on some of the logic details. As it is, the next floor is in a queue, the car will travel to the next floor selected, it will not stop at in-between floors.
The preempt logic that was added to prevent reversals mid-floor has a downside, if the elevator starts at floor 0, floor 2 button is pressed, elevator starts moving toward floor 1, if button 1 is pressed before it gets to floor 1, a real elevator will stop at floor 1; With this logic, it will not.
Before you code, test the logic by stepping through it manually. Test multiple scenarios until you are sure that it does what you want. Then code.