Servo Motor Control by Using Microcontroller PIC16F877A

14
Servo Motor Control by using Microcontroller PIC16F877A 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 BEST OF LUCK. Eng. Mahmoud Abuzayed Paso 1: R/C Servo Control Theory

Transcript of Servo Motor Control by Using Microcontroller PIC16F877A

Page 1: Servo Motor Control by Using Microcontroller PIC16F877A

Servo Motor Control by using Microcontroller PIC16F877A

PIC16F877A is an easy micro controller for the beginnersHere in project:1) How PIC switches servo motor forward and back wardWhen 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 CONTROLLERb. Proteous  8 professional       //For simulation of CKT and Design

BEST OF LUCK.Eng. Mahmoud Abuzayed

Paso 1: R/C Servo Control Theory

Servos designed for use in radio-controlled airplanes, cars, and boats can be easily

Page 2: Servo Motor Control by Using Microcontroller PIC16F877A

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

Page 3: Servo Motor Control by Using Microcontroller PIC16F877A

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

Page 4: Servo Motor Control by Using Microcontroller PIC16F877A

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:

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.

Page 5: Servo Motor Control by Using Microcontroller PIC16F877A

Paso 2: PIC PWM Module

Page 6: Servo Motor Control by Using Microcontroller PIC16F877A

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).

Paso 3: PWM PERIOD & DUTY CYCLE

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

Page 7: Servo Motor Control by Using Microcontroller PIC16F877A

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

Page 8: Servo Motor Control by Using Microcontroller PIC16F877A

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)

SETUP FOR PWM OPERATIONThe 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

Page 9: Servo Motor Control by Using Microcontroller PIC16F877A

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.

Page 10: Servo Motor Control by Using Microcontroller PIC16F877A

Paso 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

  while(1)  {

Page 11: Servo Motor Control by Using Microcontroller PIC16F877A

       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       }       delay_ms(100);

Page 12: Servo Motor Control by Using Microcontroller PIC16F877A

  }}