Assembly language 8051 for servo motor

Thread Starter

Eugene Yip

Joined Mar 18, 2018
23
Hi, I wrote an assembly code for servo motor.
I found that the delay will set the maximum angle of the motor.
I'm trying to make it rotate 90 degree clockwise, hold it there for a few seconds and then rotate 90 degree anti-clockwise back to its original spot.
In the assembly code, I managed to write two delay loops which can make it rotate 90 degree clockwise and anticlockwise but it only runs the first loop (as in the one on top) and ignore the bottom one. Is there any way for it to run the whole thing? If it isn't troublesome can someone pinpoint the mistake I made? Thank you.
 

Attachments

MaxHeadRoom

Joined Jul 18, 2013
19,006
Is this servo as in RC or industrial positioning servo?
If the latter, what kind of motor?
For accurate positioning of a Industrial servo you will need some kind of relatively high resolution feedback, encoder etc.
Max.
 

Thread Starter

Eugene Yip

Joined Mar 18, 2018
23
Is this servo as in RC or industrial positioning servo?
If the latter, what kind of motor?
For accurate positioning of a Industrial servo you will need some kind of relatively high resolution feedback, encoder etc.
Max.
It's a servo motor SG90
 

MaxHeadRoom

Joined Jul 18, 2013
19,006
It's a servo motor SG90
So it is a RC servo!
Many here, at least I do, only deal in industrial servo's, so just using the term 'servo' is vague.
It would be nice if those requiring info on RC type products would at least say so.
Max.
 

LesJones

Joined Jan 8, 2017
2,317
I'm not familiar with programming the 8051 but just the comments about starting and stoppping the motor make me think you are driving it incorrectly. For the servo to remain in the desired position the pulse train must continue being sent to with the pulse width for that position. The pulse repetition frequency is about 50 hz ( Not critical.) The pulse width between 1 mS and 2 mS (1.5 ms gives the centre position of the servo.

Les.
 
Last edited:

ebp

Joined Feb 8, 2018
2,332
If you want people to help debug your program you need to add sufficient meaningful comments to show intent. Additional documentation is usually helpful. I'm not a huge fan of spending time drawing flow charts, but they do show what is intended. Without looking at the datasheet for the processor, we have no idea what Timer 1 in Mode 1 does.Without documentation we have no idea why that should be specifically related to clockwise - would you use a different mode for counterclockwise? We don't know what setting bit TR1 is supposed to do. We don't know what TF1 is. It appears that you set up a timer, start the motor, start the timer, then poll as status bit that shows the timer has "run down", then stop the motor. But if this is a pulsewidth-proportional hobby servo, that is a long way from what is required.

TRY, AGAIN, and BACK aren't meaningful labels. Try what? Do what again? Back where? TRY, AGAIN and DELAY are useless labels in terms of program execution. Using a label as a means of identifying a block of code isn't wrong, but isn't common. Usually labels are used to identify a place in the program that will, in some manner, be jumped to.

We have no schematic to go with the program, so we have to guess what is being done with the hardware.

You do realize that the delay loop will only be executed once after the line with the stop the motor comment, and the program will simply end. Since the delay loop does nothing in terms of controlling anything, it is useless. You might just as well replace it with END. Sometimes a delay is intended to be used only once, but often it is intended to be CALLed in multiple places in the main program. Something that is CALLed must end with RETURN so the program continues execution at the line after the CALL.

Your previous post did not use a hobby servo. Is this now a completely different circuit?
 

Thread Starter

Eugene Yip

Joined Mar 18, 2018
23
If you want people to help debug your program you need to add sufficient meaningful comments to show intent. Additional documentation is usually helpful. I'm not a huge fan of spending time drawing flow charts, but they do show what is intended. Without looking at the datasheet for the processor, we have no idea what Timer 1 in Mode 1 does.Without documentation we have no idea why that should be specifically related to clockwise - would you use a different mode for counterclockwise? We don't know what setting bit TR1 is supposed to do. We don't know what TF1 is. It appears that you set up a timer, start the motor, start the timer, then poll as status bit that shows the timer has "run down", then stop the motor. But if this is a pulsewidth-proportional hobby servo, that is a long way from what is required.

TRY, AGAIN, and BACK aren't meaningful labels. Try what? Do what again? Back where? TRY, AGAIN and DELAY are useless labels in terms of program execution. Using a label as a means of identifying a block of code isn't wrong, but isn't common. Usually labels are used to identify a place in the program that will, in some manner, be jumped to.

We have no schematic to go with the program, so we have to guess what is being done with the hardware.

You do realize that the delay loop will only be executed once after the line with the stop the motor comment, and the program will simply end. Since the delay loop does nothing in terms of controlling anything, it is useless. You might just as well replace it with END. Sometimes a delay is intended to be used only once, but often it is intended to be CALLed in multiple places in the main program. Something that is CALLed must end with RETURN so the program continues execution at the line after the CALL.

Your previous post did not use a hobby servo. Is this now a completely different circuit?
I'm sorry for the mistake that I made. I will take note on that and will write more meaningful comments in the code next time. It's actually the same circuit using RC servo. The previous post was about how I tried to convert the C to assembly language with the help of converter. Since I wasn't able to understand the converted assembly code, I tried to write the code in assembly straight and managed to get some results.
 

Thread Starter

Eugene Yip

Joined Mar 18, 2018
23
So it is a RC servo!
Many here, at least I do, only deal in industrial servo's, so just using the term 'servo' is vague.
It would be nice if those requiring info on RC type products would at least say so.
Max.
Ahh I'm apologize for being vague for the term I used, I will certainly keep that in mind the next time I write/post a thread. Thank you very much!
 

Thread Starter

Eugene Yip

Joined Mar 18, 2018
23
I'm not familiar with programming the 8051 but just the comments about starting and stoppping the motor make me think you are driving it incorrectly. For the servo to remain in the desired position the pulse train must continue being sent to with the pulse width for that position. The pulse repetition frequency is about 50 hz ( Not critical.) The pulse width between 1 mS and 2 mS (1.5 ms gives the centre position of the servo.

Les.
For the delay to rotate the motor clockwise and anti-clockwise, I calculated it based on the clock frequency of the micro-controller. However, the result I obtained wasn't able to provide a full 90 degree rotation, it only managed to rotate up to around 60-70 degrees.
 

Ian Rogers

Joined Dec 12, 2012
660
it only managed to rotate up to around 60-70 degrees.
Cheap motors sometimes do not turn 180 degrees.. If you try my tutorial that outputs 1mS, 1.5mS and 2mS so if the motor doesn't do -90 and + 90 then the motor isn't set right!
 
Top