The system clock speed must be 4, 8, 16 or 32 MHz, assuming Timer1 prescalers of 1, 2, 4 and 8 are available on the PIC used.
The servo is assumed to have a theoretical movement of 180 degrees. Angle measurement follows the usual mathematical convention of 0 degrees being along the positive x-axis (due east) and 180 degrees being along the negative x-axis (due west).
There are five constants you define in your calling program to customize how the routine here will work with the servo motor you are using. Here they are with sample values for the Tower Pro SG-90 servo motor running on a PIC16F88:
SERVO_PORT = PortB; // desired port
SERVO_PIN = 0; // desired pin in port
SERVO_0_DEGREE = 825; // duty in uS for 0 deg
SERVO_180_DEGREE = 2550; // duty in uS for 180 deg
SERVO_PERIOD = 20000; // total period in uS
Note that these values depend not only upon the servo you are using but the accuracy of the system clock (internal RC, crystal or ceramic resonator.) Feel free to experiment with these values, but be careful not to clash the gears at the extremes. (You can hear if you have gone too far and have bumped into the stops).
There's a single command. (All setup and initialization of the port pin, interrupts and timer are taken care of automatically).
procedure ServoAngle(Servo_Ang : byte);
This is the angle specified in degrees. Any angle greater than 180 degrees is ignored.
Add It to Your Library Collection
You can get the servo motor library unit "Servo.pas" by clicking the following link:
Be sure to read over the source code for additional details on how to use it.
Try It Out
If you'd like to see it in operation, then head over to the exercises to find the servo motor exercise. Click here to go to it.