PIC16F877A is an easy micro controller for the beginners
Here in project:
1) How PIC switches servo motor forward and back ward
When we push one switch it goes to positive 90 degree , on second switch it moves backward to  negative 90 degree

a. MickroC  PRO        // for programming of MICRO CONTROLLER
b. Proteous  8 professional       //For simulation of CKT and Design

Eng. Mahmoud Abuzayed

Step 1: R/C Servo Control Theory

Servos designed for use in radio-controlled airplanes, cars, and boats can be easily interfaced to a PIC MCU. They are often used for robots and applications where simple mechanical movement is required. This might be surprising because a positional servo is considered to be an analog device. The output of an RC servo is usually a wheel that can be rotated from 0 to 90 degrees. (some servos can turn from 0 to 180 and others have very high torque outputs for special applications). Typically, they only require +5 V, ground, and an input signal.

An RC servo is an analog device; the input is a PWM signal at digital voltage level. This pulse is between 1.0 and 2.0 ms long and repeats every 20 ms .The length of the PWM pulse determines the position of the servo's wheel. A 1.0-ms pulse will cause the wheel to go to 0 degrees and a 2.0-ms pulse will cause the wheel to go to 90 degrees.

Pulse Width Modulation (PWM)
       Pulse Width Modulation (PWM) is one of the powerful techniques used in control systems today. They are not only employed in wide range of control application which includes: speed control, power control, measure and communication.

Basic Principal of PWM

PWM is achieved with the help of a square wave whose duty cycle is changed to get a varying voltage output as a result of average value of waveform. A mathematical explanation of this is given below:


Consider a square wave shown in the figure above. Ton is the time for which the output is high and Toff is the time for which output is low. Let Ttotal be time period of the wave such that,

Duty cycle of a square wave is defined as:

The output voltage varies with duty cycle as:

So you can see from the final equation the output voltage can be directly varied by varying the Ton value.

If Ton is 0, Vout is also 0.

If Ton is Ttotal then Vout is Vin or say maximum.

Step 2: PIC PWM Module

In Pulse Width Modulation mode, the CCP1(RC2) pin produces up to a 10-bit resolution PWM output. Since the CCP1 pin is multiplexed with the PORTC data latch, the TRISC<2> bit must be cleared to make the CCP1pin an output.
Figure below shows a simplified block diagram of the CCP module in PWM mode.

A PWM output has a time-base (period) and a time that the output stays high (duty cycle). The frequency of the PWM is the inverse of the period (1/period).


The PWM period is specified by writing to the PR2 register. The PWM period can be calculated using the following formula:

PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)

PWM frequency is defined as 1 / [PWM period]. When TMR2 is equal to PR2, the following three events occur on the next increment cycle:

 TMR2 is cleared
 The CCP1 pin is set (exception: if PWM duty cycle = 0%, the CCP1 pin will not be set)
 The PWM duty cycle is latched from CCPR1L into CCPR1H

The PWM duty cycle is specified by writing to the CCPR1L register and to the CCP1CON<5:4> bits. Up to 10-bit resolution is available. The CCPR1L contains the eight MSbs and the CCP1CON<5:4> contains the two LSbs. This 10-bit value is represented by CCPR1L:CCP1CON<5:4>. The following equation is used to calculate the PWM duty cycle in time:

PWM duty cycle =(CCPR1L:CCP1CON<5:4>) •TOSC • (TMR2 prescale value)

The following steps should be taken when configuring the CCP module for PWM operation:
1. Set the PWM period by writing to the PR2 register.
2. Set the PWM duty cycle by writing to the CCPR1L register and CP1CON<5:4> bits.
3. Make the CCP1 pin an output by clearing the TRISC<2> bit.
4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON.
5. Configure the CCP1 module for PWM operation.

Step 4: Circuit Diagram

Circuit Diagram

Step 5: Code for PIC 16F877A

void main()
  //int x = 125;
  TRISC = (1<<3)|(1<<4) ;   //   configure RC3 as input (1 for input and 0 for output)

  PR2 =  0xFA;                 //oxFA for 250  (used to set time period)
  CCPR2L =  125;               // (used to set duty cycle)
  CCP2CON |= (1<<2)|(1<<3);            //  select for PWM mode
  T2CON |= (1<<2)|(1<<1);              // enabling timer 2, prescalar 16


       if(PORTC.F3==1)                        // forward
        // x=x+1
        CCPR2L =  250+125;               // (used to set duty cycle)
        T2CON |= (1<<2)|(1<<1);              // enabling timer 2, prescalar 16
       if(PORTC.F4==1)                      // back ward
       // x=x-1
        CCPR2L =  250-245;               // (used to set duty cycle)
        T2CON |= (1<<2)|(1<<1);              // enabling timer 2, prescalar 16

Step 6: OUT PUT

output of the code :
<p>sir how can i control 2 servos bidirectional with different buttons</p>
<p>Hi, I'm from Mexico and I made this proyect, thanks a lot. In my proteus, the buttons change the duty cicle but not the servo. I will make this with a pic and I hope i'll work.</p>
<p>Frequecy of the crystal oscillator is not shown in the schematic?</p><p>If the PIC is running at 20MHz crystal, I dont think it is possible to generate a low frequency PWM (20ms cycle time or 50Hz) using the CCP module</p>
<p>Hello</p><p>you can't control the servo while your clock is 20 MHz. the minimum frequency for the PWM will be too high my be 5 KHz.</p><p>i did it by write a for loop and control the servo with out use the PWM model.</p><p>Best regards</p><p>Mohammed Jassim</p>
<p>I made my first instructable. I hope to be useful :)</p>
<p>hi, i'm using proteus to simulate but can you tell me briefly what the name of all components? since this is literally my 1st time using proteus, can you please help me</p>
<p>To search and select your components you must press &quot;Pick Devices&quot; button.</p><p>Component / Proteus's ID</p><p>Pic-------------------------&gt;16F877</p><p>Oscillator-----------------&gt;Crystal</p><p>Ceramic capacitor------&gt;Ceramic Disc</p><p>Servo motor--------------&gt;Servo(First one with 3 wires)</p><p>Resistor-------------------&gt;Resistor</p><p>Button----------------------&gt;Button</p><p>For the little triangles or power supply and ground connections, you need to click on &quot;Terminals Mode&quot;.</p><p>For oscilloscope you only click on &quot;Instruments&quot; button.</p>

About This Instructable




More by eng-mahmoud91:Servo Motor Control by using Microcontroller PIC16F877A 
Add instructable to: