Rc servo motor

Discussion in 'Embedded Systems and Microcontrollers' started by xinzlee, Dec 13, 2010.

  1. xinzlee

    Thread Starter Member

    Nov 19, 2007
    13
    0
    i am facing a problem for using servo motor.when i want to run the servo motor with pic16f877a after all the program already program in pic , the servo cant rotate, but it like to rotate.

    What is the problem.
    Pls Help ..Thank you very much ..
     
    Last edited by a moderator: Dec 14, 2010
  2. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    .......................................................
     
    Last edited: Jan 16, 2011
  3. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Crystal ball malfunction. Can't possibly diagnose the problem without a schematic and source code.
     
  4. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Aww. I like when they like to rotate. ;)

    MORE INFO. less bold.
     
  5. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    It's like a politician, big speak without any info.
     
  6. nerdegutta

    Moderator

    Dec 15, 2009
    2,514
    784
    What did Elvis Prestley sing about?

    "A little less conversation a little more action" Meaning "A litte less bold and a little more schematic and code"

    I could almost read the bold text without my glasses. Had to check if he wanted us with eye correction devices on the nose to help... :)

    Oh man, sounds like I'm nearly blind and using a camera connected to a projector showing AAC thread on the wall. Well, it's not like that. :)
     
  7. xinzlee

    Thread Starter Member

    Nov 19, 2007
    13
    0
    sorry everyone,
    I do servo motor with using 16f877a.
    The servo motor cant rotate when the power is on.The voltage use is 4.8v.
    The source code is like below:
    Code ( (Unknown Language)):
    1.  
    2.  
    3. //==========================================================================
    4. //    Project                : Sample code for control single servo motors using delays
    5. //                          This project is using 16F877A with 20MHz
    6. //                          Has been try succesfully using SK40B
    7. //                          
    8. //                          Please make sure the hardware is compatible with this code
    9. //
    10. //    Project description    : This source code is used to control a servo motor.
    11. //                          Servo motor will continuosly turn from one position to another position
    12. //                          Delay is use to calculate the pulse width timing.
    13. //                          The delay timing is depend on the crystal.
    14. //                          Difference timing for difference crystal.    
    15. //
    16. //    Date                : Feb 2009
    17. //==========================================================================
    18. //
    19. //    include
    20. //==========================================================================
    21. #include <pic.h>                       // this sample code is using 16F877A !!
    22.                                
    23. //    configuration
    24. //==========================================================================
    25. __CONFIG ( 0x3F32 );                //configuration for the  microcontroller
    26.                                            
    27. //    define
    28. //==========================================================================
    29. #define servo    RB1
    30.  
    31. #define    MHZ    *1000L                /* number of kHz in a MHz */
    32. #define    KHZ    *1                    /* number of kHz in a kHz */
    33.  
    34. // Value x must not more that 255
    35. #define    DelayUs(x)    { unsigned char _dcnt; \
    36.               _dcnt = (((x)*(20MHZ))/(24MHZ))|1; \
    37.               while(--_dcnt != 0) \
    38.                   continue; \
    39.               _dcnt = (((x)*    (20MHZ))/(24MHZ))|1; \
    40.               while(--_dcnt != 0) \
    41.                   continue; }
    42.  
    43.  
    44. //    function                         (every function must have a function prototype)
    45. //==========================================================================
    46.  
    47. void DelayMs(unsigned char y);     // Value y must not more that 255
    48.                                     // delay ms
    49. //    main function                    (main fucntion of the program)
    50. //==========================================================================
    51. void main(void)
    52. {
    53.     unsigned int i,a;
    54.    
    55.     //set IO port for servo pin
    56.     TRISB = 0b00000001;            //servo motor is RB1
    57.     PORTB = 0b00000000;            //clear port B
    58.  
    59.                                 //servo will loop infinity
    60.     while(1)                    //from one position to another position
    61.     {
    62.         //*******************************************************
    63.         //
    64.         //Delay determine the servo motors position
    65.         //Try change the value for different position
    66.         //
    67.         //Value in DelayUs and DelayMs function must not more than 255
    68.         //Otherwise the timing will not accurate!!
    69.         //
    70.         //******************************************************
    71.        
    72.         for(i=0;i<50;i++)        //continues pulse for 50 times
    73.         {
    74.             servo=1;            //set servo pin high
    75.             DelayUs(250);
    76.             DelayUs(250);
    77.             DelayUs(200);        //delay    250+250+200= 700us = 0.7ms    
    78.                                 //These delay only valid using 20MHz!
    79.        
    80.             servo=0;            //set servo pin low
    81.             DelayMs(19);        //remain --> 20ms-0.7ms=19.3ms
    82.             DelayUs(250);
    83.             DelayUs(50);        //19ms+0.250ms+0.050ms=19.3ms
    84.                                 //These delay only valid using 20MHz!
    85.         }
    86.        
    87.         //  _                   _                  _
    88.         // | |                 | |                | |
    89.         // | |                 | |                | |                  ~~50 times              
    90.         // | |_________________| |________________| |____________________
    91.         // 0.7ms    19ms      0.7ms      19ms     0.7ms   19ms
    92.         // |                   |
    93.         // |<-------20ms------>|
    94.        
    95.         for(i=0;i<50;i++)        //continues pulse for 50 times
    96.         {
    97.             servo=1;            //set servo pin high
    98.             DelayMs(2);            //delay    2ms    
    99.                                 //These delay only valid using 20MHz!
    100.                                
    101.             servo=0;            //set servo pin low
    102.             DelayMs(18);        //delay 18ms
    103.                                 //These delay only valid using 20MHz!
    104.         }    
    105.            
    106.         //  ___                 ___                 ___
    107.         // |   |               |   |               |   |
    108.         // |   |               |   |               |   |              ~~50 times      
    109.         // |   |_______________|   |_______________|   |____________________
    110.         //  2ms       18ms      2ms      18ms       2ms        18ms
    111.         // |                   |
    112.         // |<-------20ms------>|
    113.     }
    114.        
    115. }
    116.    
    117. //subroutine
    118. //============================================================================    
    119. void DelayMs(unsigned char y)
    120. {
    121.     unsigned char    i;
    122.     do {
    123.         i = 4;
    124.         do {
    125.             DelayUs(250);
    126.         } while(--i);
    127.     } while(--y);
    128. }
    129.  
    130.  
     
    Last edited by a moderator: Dec 14, 2010
Loading...