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.
-
6\$\begingroup\$ Think before you code. Sit down and work out how you'd do it if you were doing all this, manually. Imagine that there is no such thing as electronics or computers. There's just you, your eyes, your hands, and some levers and such. You are the computer and the electronics. Work out the details. I usually spend 60% or more of my time doing careful consideration and design work (throwing away some, reworking other parts) before I ever write the first line of actual code. (I do, however, write exploratory code where I don't know something about a transducer and the docs fail me.) \$\endgroup\$jonk– jonk2020年02月18日 07:08:16 +00:00Commented Feb 18, 2020 at 7:08
-
\$\begingroup\$ @jonk I tried trial and error and alot of brainstorming to assembly coding since yesterday for hours . I am very new to this coding .Any help is highly appreciated. Thank you so much! \$\endgroup\$MCS_51– MCS_512020年02月18日 07:09:57 +00:00Commented Feb 18, 2020 at 7:09
-
4\$\begingroup\$ @MCS_51 "Trial and error" isn't so good. One of the harder things to get across to someone else is DON'T CODE and instead to sit down, THINK, and work through the details on paper. I mean DOWN TO THE LAST DETAIL on paper. Every single thing. When I was to write a software server application that had to serve thousands of clients and had to withstand a power-off event losing only zero or one transaction (that was my allowance), I sat down on a whiteboard and worked out all the details for weeks. The coding took me four days. It's still running. Zero bugs after 9 yrs now. \$\endgroup\$jonk– jonk2020年02月18日 07:15:27 +00:00Commented Feb 18, 2020 at 7:15
-
\$\begingroup\$ Do you need to use an 8051? There are many, many modern MCUs that have C compilers. \$\endgroup\$Mattman944– Mattman9442020年02月18日 08:31:09 +00:00Commented Feb 18, 2020 at 8:31
-
\$\begingroup\$ I completely agree with @jonk. And here is some more advice. Stop thinking flow diagrams and start thinking state diagrams. en.wikipedia.org/wiki/State_diagram \$\endgroup\$Mattman944– Mattman9442020年02月18日 08:35:55 +00:00Commented Feb 18, 2020 at 8:35
1 Answer 1
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:
Floor_Selected = 0
Next_Floor_Selected = 0
Last_Floor_Sensor = 0
Elev_State = Stop
Loop
{
Read all buttons, save active button in Next_Floor_Selected variable
If Elev_State = Stop // Only allow the next command when stopped
Floor_Selected = Next_Floor_Selected
Read all sensors, save active sensor in Last_Floor_Sensor variable
Update state based on diagram. Set motor direction based on state
}
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.
Explore related questions
See similar questions with these tags.