The control signals for the rotation of the motor are provided by 8051 microcontroller.For basic concepts and know-how of a servo motor, refer to the article Servo Motor.

Source Code

Here,i have used AT89S51  microcontroller.Code is written in C using  Keil uVision4.
#include<reg51.h>
sbit output=P1^0;          //Output to motor

void timer(int msec)     // Function for timer
{
int i;

for(i=0;i<msec;i++)

{
TMOD=0x20;         // Mode2
TH1=0xFF;
TL1=0xD1;
//TH1= -23;        // 50usec timer
TR1=1;
while(TF1==0);
TF1=0;
TR1=0;
}
}

void main()
{
int i;

output=0;

while(1)
{
//anticlockwise direction
for(i=0;i<500;i++)
{
output=1;
timer(20);
output=0;
timer(360);
}
//clockwise direction
for(i=0;i<500;i++)
{
output=1;
timer(40);
output=0;
timer(360);
}
}
}

Circuit

Servo motor control

 

Advertisements

Comments on: "Servo Motor Control Using 8051 Micrcontroller" (37)

  1. HELLO

    IS THIS CODE SUITABLE FOR 89C52??
    I WANT TO ROTATE THE SERVO FOR 180 DEGREE…

  2. What’s the utility of while(TF1==0);? It doesn’t have nothing inside its loop.

    • while(TF1==0); only monitoring the timer flag when raising (TF1==1) then repeat the process with clearing the timer.No instruction is needed for this purpose inside the loop.You can write-
      while(TF1==0){

      };

      No difference between both of two.

      How TF works ,to see timer programming.

      • Oh yes. I thought that it was just garbage code, but then I noticed that you put it in mode 2, which makes the timer a 8 bit counter, increasing every clock, and when it goes to all 1’s to all 0’s it sets TF1 to 1 and then exiting the loop while…

      • hello sir i need to ur help for writing pgm on DC servo motor control using DSP processor.
        can u pls help me

    • Wait until Timer 1 is finished. When it goes to all 1’s to all 0’s it sets TF1 to 1.

  3. Anonymous said:

    What’s the frequency of the oscillator you used?

  4. mayur14 said:

    I want c program to control servo motor with P89V51RD2 microcontroller.
    Crystal frequency is 11.0592 Mhz…..Plzzz help….

  5. mohit narkhede said:

    i am not getting d circuit diagram…??
    cn u plz xplain those notations frm 17 to 40

  6. This is just the information I’ve been looking for. thank you for sharing! motor soft starter

  7. K.N.SREEKUMAR said:

    hi….
    i just modified ur code, i am using at89c51 with 11.0592 xtal since am using two keys in port 1.0 and p1.1 similarly motor is connected in P2.0 but in proteus it is not working. Can u check my code; I have been attaching my code thanks in advance…

    #include
    sbit M = P2^0;
    sbit S1 = P1^0;
    sbit S2 = P1^1;
    int i;
    void timer(int msec);
    void main()
    {
    while(1)
    {
    if(S1==0)
    {
    for(i=0;i<500;i++)
    {
    M=1;
    timer(20);
    M=0;
    timer(360);
    }
    }
    if(S2==0)
    {for(i=0;i<500;i++)
    {
    M=1;
    timer(40);
    M=0;
    timer(360);
    }
    }
    else M=0;
    }

    }

    void timer(int msec)
    {
    int i;
    for(i=0;i<msec;i++)
    {
    TMOD=0x20;
    TH1=0xFF;
    TL1=0xD1;
    TR1=1;
    while(TF1==0);
    TF1=0;
    TR1=0;
    }
    }

    • Baber khan said:

      ok.thanks brother im trying this source code ,but you sent me souce code in asembly languge for steper motor. again thanks

  8. Anonymous said:

    can u write code for servo 360degree rotation

  9. puss in boots said:

    Rebuild target ‘Target 1’
    assembling STARTUP.A51…
    compiling Code.c…
    Code.c(260): error C100: unprintable character 0xD7 skipped

    this is the error that I am getting do you know why ?

  10. hey hi puss..just replace cross in “TMOD=0x20;” with “x”.

  11. IF i need this program for an 18.432 MHz crystal what I have to change please ?

  12. hi i have tried this code but the servo is giving a kick and stop an not continuing to turn , is there any problem in tha code ?

  13. what is frequency of crystal taken here….

  14. umang ghadhe

  15. Anonymous said:

    ye program l

  16. am plannin to make a robotic arm which would require servo motors so can u suggest to what microcontroller should i interface the same for maximum performance? also in need of the codes

  17. hey, i want to this code in assembly language.plz rply………

  18. Muhammad usman said:

    how i can control more the 1 servo by this code after 1 each other??

  19. you have made a code bug ………..do need to solve ………..code can not work ………..because you r not genrating 10% duty cycle pwm

  20. Anonymous said:

    tried it practically but the code doesnt work

  21. anil raghuwanshi said:

    i want to make a robitic arm which will initially work with the help of remote and after that will work automatically and will generate progrm itself .is this possible and if so how ………….will u plz help me regarding that ……..

  22. IF i have atmel AT89C51AC3 program and 29.4912 MHz crystal what I have to change please ? Thank you.

  23. Guys if i want to rotate servo motor to the specified angle instead of 0, 90 or 180. What should be the code?
    Please suggest the code.

  24. Anonymous said:

    i want 8051 c code to control the direction of rotation dc motor in both clockwise and anti clockwise using keypad can u help me

  25. sir can u help me because i use this program to control servo motor with NXP P89V51RB2 can, but when i want control servo motor use AT89S52 cannot. why this happen? please help me sir.

  26. sir i have interfaced servo to 8051 , its working perfectly in simulator but on hardware its giving problem.i kept 5 swicthes for 0-45-90-135-180 angles.if i press sw1 motor will b at 0 degree, if i press & release sw5 it is going to 180 degree in proteus but in hardware i need to keep switch pressed till it goes to 180 degree. hope u got me.

  27. sir i wnt clear explanation about the code

  28. wil u explain the code sir..i m nt get it

  29. tell me steps for link circuit with source code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: