Servo Motor Control: Software

Single Servo Motor Control Software
           Our first program is from the example video in part 3: 'Inside The Servo Motor'. It moves the motor from: 90° -> 45° -> 0°. First we take a look at how to get the necessary numbers for our program:
           Now that we know the necessary delays to create inorder to get a proper servo PWM input signal, let's look at the code. The code is fairly long so I'll go over the most important part.
Download The Entire Program Here: servo_single.c
« Begin Code »
for(count=0;count<50;count++) //50 * 0.020 Seconds = 1 Second
     PORTB = 0x01;     //PortB Pin 0 = 5v (logic 1)
     Delay1KTCYx(7); //Delay 1.5mS
     PORTB = 0x00;   //PortB Pin 0 = 0v (logic 0)
     Delay1KTCYx(92);//Delay 18.5mS

« End Code »
           This little chunk of code will move the servo to a 45° angle and hold there for 1 second. The servos are very strong so for that 1 second try as you might but you won't be able to change the angle it's at.

***Warning Advanced Topics***
Continue Reading Only If You're Familiar With Timers & Interrupts
Mulitple Servo Motor Control Software
           This next program is more advanced than the ones in previous tutorials. It uses interrupts & timers to control the servos instead of delays. This is a great advantage because we'll be able to control up to 9 servos at the same time. For the sake of keeping the tutorial short i'll just use two servos at the same time.

Download The Entire Program Here: multi_servo.c

           This program is even longer than the first one, so instead of attempting at an explanation of the code here, I just put more comments in the code than I normally do. If you've been programming with the PIC for a while it should make a decent amount of sense to you. If you have any questions about the code just say something in the forums.