The best way to control a servo with a PIC


Closed Thread
Results 1 to 40 of 42

Hybrid View

  1. #1
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697

    Default The best way to control a servo with a PIC

    Hi, I already know how a servo works and what kind of pulse to send to it. I can do that fine with just a PIC and a servo. Recently ive been working on a PWM circuit as some of you know. I wanted to try and control 2 servos with the same circuit but its not going so well. As far as i know TMR0 will overflow every 51.2us. The servo i have takes a pulse from about 500 to 2500us. That only gives me about 39 positions and i was hoping for 256.

    Ive thought about a few things but the best way i can think of is to have 2 seperate chips. One will do the PWM as normal and the other will control the servos. I just want to know if theres a better way. I would like to keep everything on one chip but i dont think its going to happen not even if i upgraded to a 18F with a 40MHz oscillator.

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink Manual ...

    Hi,

    Reading your beloved manual, you will find :

    PULSOUT command

    Which :

    @ 4Mhz Clock gives you ... 200 steps from 500 to 2500 µs

    @ 20Mhz Clock gives you ... 1000 steps from 500 to 2500 µs

    Why look for complicated softs ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697


    Did you find this post helpful? Yes | No

    Default

    Is PULSOUT a blocking command? I always thought it stopped your code like a pause command but im guessing not now youve mentioned it.

    Edit: Just had a quick read of the online manual (im not at home with my proper one). It doesnt mention anything about it blocking/not blocking. Im sure ive read about it before though because i remember the resolution bit
    Last edited by The Master; - 14th October 2008 at 14:36.

  4. #4
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    A single PIC18 @ 20MHz can control 24 servos with 256 steps:
    http://www.mikroe.com/forum/viewtopic.php?t=6192

  5. #5
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697


    Did you find this post helpful? Yes | No

    Default

    Im using a 16F (PIC16F87 to be more specific) @ 20MHz. Im sure it could handle 24 servos even with the way i did it. The problem is that the way i was doing it would have caused problems with the PWM for the LEDs. If PULSOUT allows the next few lines of code to run before it toggles back off then this should be really simple. I dont have anything to test on at the moment but when i get home ill give it a go

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by The Master View Post
    Is PULSOUT a blocking command? I always thought it stopped your code like a pause command but im guessing not now youve mentioned it.
    Edit: Just had a quick read of the online manual (im not at home with my proper one). It doesnt mention anything about it blocking/not blocking. Im sure ive read about it before though because i remember the resolution bit
    Yes, PULSOUT is a blocking command. As are PWM, SERIN(2)/SEROUT(2), POT, and so on.
    Basically, anything that 'bit-bangs' a function is a blocking command. Nothing else can happen until that command is done.

    I'm with you on this one... Send out your servo position data to a 2nd PIC via serial (or whatever) and have that 2nd PIC handle the servo positioning. There are a bunch of boards out there that do exactly that...can't think of any off hand, but I know I've seen them a bunch of times...

  7. #7
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Yes, PULSOUT is a blocking command. As are PWM, SERIN(2)/SEROUT(2), POT, and so on.
    Basically, anything that 'bit-bangs' a function is a blocking command. Nothing else can happen until that command is done.
    That's called a "synchronous" operation.

    Asynchronous is a "do together at the same time" with something else operation.

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by T.Jackson View Post
    That's called a "synchronous" operation.
    Asynchronous is a "do together at the same time" with something else operation.
    Thanks for clearing that up there Sparky...
    I was kinda wondering when you'd get in there with the proper terminology and all...
    Last edited by skimask; - 14th October 2008 at 16:59.

  9. #9
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697


    Did you find this post helpful? Yes | No

    Default

    Oh, I thought it might have been simple for a moment there. Since im using 16F i only get 1 hardware UART to play with but im using a MAX485 chip and in the lights the PIC controls the master/slave pins (the 555 method is only used in the main controller). So i can send data to a second chip without it being sent outside the light. I also have 2 spare IO pins so if i use a PIC chip to controll it i can tell it to only listen for serial data if an input pin goes high.

    Now i just have to think of something to do with all those extra IO pins on the second chip. I dont think you can get anything with less than 12 and A/E/USART

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by The Master View Post
    Oh, I thought it might have been simple for a moment there. Since im using 16F i only get 1 hardware UART to play with but im using a MAX485 chip and in the lights the PIC controls the master/slave pins (the 555 method is only used in the main controller). So i can send data to a second chip without it being sent outside the light. I also have 2 spare IO pins so if i use a PIC chip to controll it i can tell it to only listen for serial data if an input pin goes high.

    Now i just have to think of something to do with all those extra IO pins on the second chip. I dont think you can get anything with less than 12 and A/E/USART
    I've used the 16F688 quite a bit, smallest PIC you can get with a built-in USART as far as I know. And if you set it up right, you should be able to run a load of servo's with it...If you set it up right...
    Just a really simple idea off the top of my head...some pseudo-code if you will...
    Code:
    servo1 var porta.whatever...a lot of servo's...
    ...serial port setup...
    
    loop:
    ....check RCREG for new data which is in this format...
    bit7 = 1 = new data actually here, =0 nothing really here :)
    bit6-bit4 = servo select, one of eight
    bit3 = direction
    bit2-bit0 = 3 bits to define the amount to move...
    
    ...now it gets a bit tricky...depending on clock speed of the PIC
    
    ...do the same thing with the servo's that you did with the LEDs, except the servos are on for XXX microseconds, and off for about 18ms or so...
    
    goto loop
    Last edited by skimask; - 14th October 2008 at 17:05.

Similar Threads

  1. More Servo Woes
    By chrisshortys in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 13th May 2009, 08:40
  2. Help with Servo Control Please!
    By wireman22 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 7th June 2007, 18:15
  3. Servo control / UART / SPI help needed
    By Blackhawk in forum mel PIC BASIC
    Replies: 10
    Last Post: - 10th November 2006, 03:40
  4. How would i write this in pic basic pro (Servo controller)
    By Jhdgkss in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th February 2006, 08:21
  5. Control RC servo via Parallax Servo Control
    By cibotsan in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 17th September 2005, 08:18

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts