![Image](http://www.matrixmultimedia.com/images/Article_AllVersions.png)
Servo basics
The servo motors used in radio controlled models represent a simple solution for applications requiring accurate position control. The servos are self-contained control devices that require only a power supply (usually 0v and 4.5v - 6v) and a position control signal.
The position control signal consists of a high pulse, usually between 0.7ms and 2.3ms (the exact requirements vary slightly depending on the servo being used), usually representing a rotation range of approximately 150 degrees. The pulse must be repeated every 20ms.
Many microcontrollers now contain the dedicated timing hardware that is capable of generating the required signals.
A typical device, like the PIC16F877A contains a flexible timer module (Timer 1) that can be used to generate accurate timing signals suitable for R/C servo control. With the additional use of interrupts, precise control of up to eight servo channels can be achieved as a background task. The main control program can operate independently, simply loading the required servo positions into a shared data array.
Timer 1 and the CCP modules
Timer 1 is a 16-bit timer that also contains two 16-bit capture/compare modules (CCP1, CCP2) that can be configured to perform different tasks. These functions are particularly useful when combined with their ability to generate interrupts when events occur, requiring the creation of two Flowcode Custom Interrupts.
The CCPR1 module can be used to reset Timer 1 when the value of the timer register matches the value in the CCPR1 registers, generating an interrupt (CCP1IF) when this event occurs, allowing the output for the next servo channel to be set in the interrupt service routine.
The CCPR2 module can be configured to only generate an interrupt (CCP2IF) when the value of the timer 1 register matches the value in the CCPR2 registers. The value of CCPR2 during each servo channel period can be used to control the pulse width of the position control signal for that servo, allowing the output for the current servo channel to be cleared when the match event occurs (within the overall 2.5ms period of the servo channel).
Setting a value in CCPR1 that equates to 2.5ms will provide the channel timing for each servo being controlled
Sequentially servicing eight 2.5ms servo channels will produce the required 20ms overall cycle time.
![Image](http://www.matrixmultimedia.com/images/Article_Multi_Servo1.png)
Simple calculations
Values for each channel can be written as byte values to data arrays (strings) and accessed by the CCP1 interrupt service routine.
The values written to the CCP registers depend on the device clock frequency and Timer 1 prescaler value. Careful selection of these values will allow simple calculations to be used throughout.