Servo Motor Control  
Servos

Servos are DC motors with built in gearing and feedback control loop circuitry. And no motor drivers required. They are extremely popular with robot, RC plane, and RC boat builders. Most servo motors can rotate about 90 to 180 degrees. Some rotate through a full 360 degrees or more.

However, servos are unable to continually rotate, means they can't be used for driving wheels, unless they are modified, but their precision positioning makes them ideal for robot legs and arms, rack and pinion steering etc. To use a servo, simply connect the black wire to ground, the red to a 4.8-6V source, and the yellow/white wire to a signal generator (such as from your microcontroller). Vary the square wave pulse width from 1-2 ms and your servo is now position/velocity controlled.

Pulse width modulation (PWM) is a powerful technique for controlling analog circuits with a processor's digital outputs. The general concept is to simply send an ordinary logic square wave to your servo at a specific wave length, and your servo goes to a particular angle . The wavelength directly maps to servo angle.



Basic working concept of servo motors -


How to derive your logic ???

>> we need a 20ms total pulse width (high+low).

>> Simultaneously we need to generate acurate pulse width to control angle of shaft.

>> In microcontroller world whenever we need to perform two task almost at same time like in this case we must use Interrupts.

How many interrupts ???

>> Another question how many interrupts ???

>> That is depend on what is our task and how we are performing it.

>> we are going to make a system in which we will make servo shaft to take different angular positions on different button pressed.

>> One thing is very clear we can't generate this much precise delays using 'for loops' method. So we need to use internal timers.

>> We are going to use Timer0 and Timer1 overflow interrupts to generate PWM signal and shaft control pulse delays respectively.




Schematic Diagram - /***********************************************************
Example Servo Motor Control electroons.com 8051 CD
Code by- devesh@electroons.com
Control signal is connected to P1_0
AT89S52 is working on 11.0592 MHz
1 machine cycle = 1.085usec
For 20msec --> time required is 20000/1.085 counts = 18433 counts
Timer 1 count => 65535-18433 => 0xB7FE
Timer 0 count =>
For 1msec => 0xFC65 (0 degree)
For 2msec =>0xF8CB (180 degree)
************************************************************/
#include < at89x52.h> // Header file for AT89S52

void timer1_ovf(void) interrupt 3 // timer 1 for 20ms
{
TH1=0xB7;
TL1=0xFE;
P1_0=1;
TR0=1;
}

void timer0_ovf(void) interrupt 1 // timer 0 for various shaft position
{
if(P2_0==0) //PB2 pressed
{
TH0=0xFC; //0 degree shaft position
TL0=0×65;
}
else if(P2_1==0) // PB1 pressed
{
TH0=0xF8; //180 degree shaft position
TL0=0xCB;
}
P1_0=0;
TR0=0;
}


void main(void)
{
P2=0xFF; // Using PORT2 as input
TMOD=0×11;
ET1=1;
ET0=1;
TH1=0xB7;
TL1=0xFE;
TR1=1;
EA=1;
while(1){}
}




devesh@electroons.com
Best viewed at Firefox 1024x768 resolution