control RC Micro servo motor using PIC16F877

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

control RC Micro servo motor using PIC16F877

Postby ann » Sat May 03, 2014 2:34 pm

Hi,
I had bought RC Micro Servo Motor (HD-1160A) from cytron. I had tried loaded the coding that given into PIC 16F877and it was successful.The servo motor turn 180 degree in anti clockwise. So, i tried modified the PIC programming that given in order to turn the RC Micro servo motor to 90 degree and it will turn back to 0 degree in 50times.But, the RC Micro Servo motor only turn 90 degere and not turned back to 0 degree in 50 times.I not really good in PIC microcontroller programming. Below is the program that I had modified. I need a help to check which part should I modified in order to turn the RC Micro servo motor to 90 degree and it will turn back to 0 degree in 50times. Thanks in advance :)

CODE: SELECT_ALL_CODE
#include <pic.h>                  // this sample code is using 16F877A !!
                       
//   configuration
//==========================================================================
__CONFIG ( 0x3F32 );            //configuration for the  microcontroller
                                 
//   define
//==========================================================================
#define servo   RB1

#define   MHZ   *1000L            /* number of kHz in a MHz */
#define   KHZ   *1               /* number of kHz in a kHz */

// Value x must not more that 255
#define   DelayUs(x)   { unsigned char _dcnt; \
           _dcnt = (((x)*(20MHZ))/(24MHZ))|1; \
           while(--_dcnt != 0) \
              continue; \
           _dcnt = (((x)*    (20MHZ))/(24MHZ))|1; \
           while(--_dcnt != 0) \
              continue; }


//   function                   (every function must have a function prototype)
//==========================================================================

void DelayMs(unsigned char y);    // Value y must not more that 255
                           // delay ms
//   main function               (main fucntion of the program)
//==========================================================================
void main(void)
{
   unsigned int i,a;
   
   //set IO port for servo pin
   TRISB = 0b00000001;         //servo motor is RB1
   PORTB = 0b00000000;         //clear port B

                        //servo will loop infinity
   while(1)               //from one position to another position
   {
      //*******************************************************
      //
      //Delay determine the servo motors position
      //Try change the value for different position
      //
      //Value in DelayUs and DelayMs function must not more than 255
      //Otherwise the timing will not accurate!!
      //
      //******************************************************
     
      for(i=0;i<50;i++)      //continues pulse for 50 times
      {
         servo=1;         //set servo pin high
         DelayUs(150);
         DelayUs(150);
         DelayUs(100);      //delay   250+250+200= 700us = 0.7ms   
                        //These delay only valid using 20MHz!
     
         servo=0;         //set servo pin low
         DelayMs(19.2);      //remain --> 20ms-0.7ms=19.3ms
         DelayUs(50);
         DelayUs(20);      //19ms+0.250ms+0.050ms=19.3ms
                        //These delay only valid using 20MHz!
      }
     
      //  _                   _                  _
      // | |                  | |                | |
      // | |                  | |                | |                 ~~50 times             
      // | |_________________| |________________| |____________________
      // 0.7ms    19ms      0.7ms      19ms     0.7ms   19ms
      // |               |
      // |<-------20ms------>|
     
     // for(i=0;i<50;i++)      //continues pulse for 50 times
      //{
        // servo=1;         //set servo pin high
         //DelayMs(2);         //delay   2ms   
                        //These delay only valid using 20MHz!
                       
         //servo=0;         //set servo pin low
        // DelayMs(18);      //delay 18ms
                        //These delay only valid using 20MHz!
     // }   
         
      //  ___                   ___                 ___
      // |   |               |   |               |   |
      // |   |               |   |               |   |              ~~50 times       
      // |   |_______________|   |_______________|   |____________________
      //  2ms       18ms      2ms      18ms       2ms      18ms
      // |               |
      // |<-------20ms------>|
   }
     
}
   
//subroutine
//============================================================================   
void DelayMs(unsigned char y)
{
   unsigned char   i;
   do {
      i = 4;
      do {
         DelayUs(250);
      } while(--i);
   } while(--y);
}
ann
Newbie
 
Posts: 8
Joined: Thu Oct 10, 2013 7:24 pm

Re: control RC Micro servo motor using PIC16F877

Postby yonghui » Mon May 05, 2014 5:36 pm

Mayb u will need to read some on how the pulse for RC servo.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests

cron