1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Servo motor control...

Discussion in 'Microcontrollers' started by koolguy, Sep 26, 2013.

  1. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Last edited by a moderator: Sep 26, 2013
  2. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    That is a standard servo. You get the best results if you control it with a standard servo signal:
    http://www.ermicro.com/blog/?p=771

    EDIT: The tutorial is for continuous servo, but the control signal is the same for your servo also.

    EDIT2: Here is another tutorial that looks pretty good: http://www.dwengo.org/node/237
     
    Last edited: Sep 26, 2013
  3. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    For servo i have modified this code for getting 50Hz PWM.
    without ISR it was 305HZ at 1:16 and PR at 255.
    so using ISR I am calculating it...

    Code (text):
    #include <htc.h>
    __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    #define _XTAL_FREQ 20000000
    void main()
      {
      //TRISA = 0xff ;
      //ADCON1=0b00000000;
    //  ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
    //  ADIF=0;
      TRISC = 0 ;
      PR2 = 255 ;
      T2CON = 0b00000100 ;
      CCP1CON = 0b00111100;
      PORTC = 0 ;
      while(1)
        {
      CCPR1L = 0 ;
        }
      }
    void interrupt ISR()    // This just swaps the buffer to the display
      {
      if(TMR2IF)        // make sure its the timer interrupt.
        {
        CCPR1L = 0 ;//??
        }
      TMR2IF = 0;
      }
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    Why don't you buy the PicKit3 that has a 3 channel logic analyzer? It would make your work so much easier. How are you going to verify that the signal is what it is supposed to be? And more importantly; when the signal is wrong, you will need to know how wrong it is. Is it too fast, too slow, or no signal at all etc.
     
    Last edited: Sep 26, 2013
  6. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Right now i don't have PICkit3 so, can't go without it for this project.(very costly)
     
  7. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Ok.. I understand. If I remember correctly, you use PIC16F887 for all your projects.. and you have done PWM before, so you must be familiar with it. What is your question or problem?
     
  8. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I want to create 50Hz and data sheet says..
    Detailed Specifications
    Control System: +Pulse Width Control 1500usec Neutral
    Required Pulse: 3-5 Volt Peak to Peak Square Wave
    Operating Voltage: 4.8-6.0 Volts
    Operating Temperature Range: -20 to +60 Degree C
    Operating Speed (4.8V): 0.19sec/60° at no load
    Operating Speed (6.0V): 0.15sec/60° at no load
     
  9. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I'm sorry I can't help you with complete settings (because I don't work with PICs that much), but you should be reading the PIC datasheet:
    http://ww1.microchip.com/downloads/en/DeviceDoc/41291D.pdf

    Starting from page 128 are the PWM mode instructions.

    The datasheet also gives a nice protocol to setup PWM operation:

    11.5.7 SETUP FOR PWM OPERATION
    The following steps should be taken when configuring the CCP module for PWM operation:

    1. Disable the PWM pin (CCPx) output drivers as an input by setting the associated TRIS bit.

    2. Set the PWM period by loading the PR2 register.

    3. Configure the CCP module for the PWM mode by loading the CCPxCON register with the appropriate values.

    4. Set the PWM duty cycle by loading the CCPRxL register and DCxB<1:0> bits of the CCPxCON register.

    5. Configure and start Timer2:
    • Clear the TMR2IF interrupt flag bit of the PIR1 register.
    • Set the Timer2 prescale value by loading the T2CKPS bits of the T2CON register.
    • Enable Timer2 by setting the TMR2ON bit of the T2CON register.

    6. Enable PWM output after a new PWM cycle has started:
    • Wait until Timer2 overflows (TMR2IF bit of the PIR1 register is set).
    • Enable the CCPx pin output driver by clearing the associated TRIS bit.
     
  10. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I have created PWM but its min frq with 20Mhz and 1:16 pre scaller is
    without ISR it was 305HZ at 1:16 and PR at 255.
    so using ISR I am calculating it...
     
  11. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Well, that sucks.. change to AVR, or slower clock, or do the servo signal with software. Is 16 really the highest prescaler you can get? Lame.

    Here is one very simple software version: http://vshamu.wordpress.com/tag/servo-motor-interfacing-with-pic16f887/

    Or.. was your question about how to do the servo signal using timer interrupts?
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    These servo's are soooo easy to control.... Set up a 1.9khz pwm... set the pwm to 50:50 and the servo sits at mid position.... set the pwm to 99:1 and the servo shoots to 90 degrees swap it round to 1:99 and the servo shots to -90 degrees...
     
  13. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,702
    Likes:
    426
    Location:
    SCOTLAND
    do a forum search, pommie did a really good code for servo's using a junebug, but the code could easily be modded for your chip. i cant remember the post but search for servo code and it should show up. servo pulses are best done with the timer rather than PWM part of chip IIRC.
     
  14. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    for 1.9khz PR2 39 and 1:16 pre scaller @20Mhz 1.9khz and 512uSec. and CCP1L=127 FOR mid , 1 for right and 254 for left right??
     
  15. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Try it.

    If you feed the servo too short pulse, or a too long pulse, it will make an ugly sound, but it will not break it. Just don't stress it for too long. You can find the limits of the servo by experimenting.
     
    Last edited: Sep 26, 2013
  16. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    But i have listen they work on 50Hz
     
  17. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Not as efficiently though.... If your PWM is 20khz the motion is smooth and clean.... At 50hz the motor will appear sluggish.. I found that the control is different when using low frequency.... I have a BIG motor that I control with PWM @ 1.9khz The response is really good ( There is a hum, but as it's on a crane I don't give a monkey's) but the control is excellent.... If I use 250hz it isn't as controllable...
     
  18. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    The frequency is not important. The pulse width is important. The servo takes the information from the pulse width (it calculates how long the incoming pulse is). 50 Hz is the "old" standard. But you can send the pulses at higher rate to modern servos.

    Why don't you just try it.. the servo is not going to break.
     
  19. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,903
    Likes:
    502
    Location:
    Cleveland, OH, USA
    In reading your and misterT's comments (#17), I have learned something worth trying this Winter.
    • What brand and model servo are you using?
    • Are the "modern" servos digital or analog? If digital, what is the deadband spec in uS? (If you just give the brand and model, I can look up the latter info.)
    John
     
  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Here is the brand...
    Exact replacement for Futaba S3003 and Hitec HS-311.
     
  21. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Are you just going to sit and wait for code? All this help and you do nothing.

    Try it! Send it pulses between 1..2 milliseconds and see if it turns! Post your results.
     

Share This Page