Shop OBEX P1 Docs P2 Docs Learn Events
Servo motor communicate with PIC16f877 — Parallax Forums

Servo motor communicate with PIC16f877

jeremy1409jeremy1409 Posts: 2
edited 2010-06-03 12:55 in Robotics
hi
Can anyone share me some sample coding to control the continuous servo motor by using C language on 16f877? And i need some guide on this~~thx~~

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2010-05-31 17:33
    Not really. Try www.sourceboost.com, they support 16F877 and have a very nice 'C' compiler.

    The longer answer is to use some sort of library "pulsout" call, then some library "pause(20)" call.· Put that in a loop, and you're good to go.
    Or:
    int main()
    {
    · while(1)
    · {
    ··· pulsout(Servopin, 1500);· // Assuming 1500 mSec
    ··· pause(20);· // Assuming is 20 mSec
    ··} // End while
    }

    Post Edited (allanlane5) : 5/31/2010 7:36:04 PM GMT
  • FranklinFranklin Posts: 4,747
    edited 2010-05-31 17:35
    Best place to get that kind of information is a pic forum. Microchip seems to be down right now or I would have given you the link but you can Google it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • jeremy1409jeremy1409 Posts: 2
    edited 2010-06-01 02:41
    THX alot, but does it need any headed file to program that?then what is the maximum time i can set for the servo motor?
  • al1970al1970 Posts: 64
    edited 2010-06-01 04:56
    Hi jeremy1409

    If you never program a PIC in ASM then don't write your code in C. You have to get to know your PIC 1st before you program in C, otherwise you will write very poor programs! I would also pick another PIC. PIC 16F887 is also a 40 pin chip and cost 1/2 the price.

    Hi allanlane5

    ·while(1)
    · {
    ··· pulsout(Servopin, 1500);· // Assuming 1500 mSec
    ··· pause(20);· // Assuming is 20 mSec
    ··} // End while

    Why would you want to write code that throws away 20 msec. for no reason?

    Al
  • Mike GreenMike Green Posts: 23,101
    edited 2010-06-01 05:02
    al1970 (and jeremy1409),

    The pulsout is supposed to be in units of microseconds, not milliseconds, so 1500 is 1500 microseconds or 1.5 milliseconds.

    The pause is indeed supposed to be in units of milliseconds. The reason for using this pause is to repeat the pulse about 50 times a second which comes out to roughly every 20 milliseconds. If a servo doesn't see a control pulse coming in about 50 times a second, it assumes that the controller isn't working and the servo shuts down until it receives the next pulse.

    There's a good article on R/C servo motors in the Wikipedia. There's also a description of how servos work in the Robotics with the BoeBot tutorial from Parallax. Go to the main Parallax webpage and click on the Downloads button. That'll take you to the downloads page where you can find Stamps in Class Downloads where the tutorial is located.
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-06-01 14:10
    Yes, there's probably a #include "servo.h" or something -- but again, this is not a "C" shop or site, there's a HUGE variability in the various 'C' language library implementations for PIC's, you'd be MUCH better off finding the web-site of your compiler vendor and asking them.
  • al1970al1970 Posts: 64
    edited 2010-06-02 06:18
    Hi Mike Green


    "The pause is indeed supposed to be in units of milliseconds. The reason for using this pause is to repeat the pulse about 50 times a second which comes out to roughly every 20 milliseconds. If a servo doesn't see a control pulse coming in about 50 times a second, it assumes that the controller isn't working and the servo shuts down until it receives the next pulse."

    Yes, I know this. My point is, a programmer would never use a 20 msec. " pause(20);"
    They would do something else for 20 msec then come back to the servo subroutine to update the servos. There are new programmers when they see a pause command take it to heart and do it. Other than a few NOPs, there should never be a pause used in a working program.
  • W9GFOW9GFO Posts: 4,010
    edited 2010-06-02 07:23
    al1970 said...
    My point is, a programmer would never use a 20 msec. " pause(20);"

    I agree, what a waste of time that would be!

    I use Pause 18. [noparse]:D[/noparse]

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-06-02 19:31
    Al1970 -- I don't disagree with you. So, when YOU write the examples, you can put in something that will take 18 to 20 mSec to execute. And a small multi-tasking round-robin scheduler, why not?

    Note, this entire thread is on 'C' language with PIC processors, none of which Parallax supports. I was simply trying to generate a quick and dirty (quick to write, anyway) example to give the OP a place to start.
  • John R.John R. Posts: 1,376
    edited 2010-06-03 12:55
    al1970 said...


    Yes, I know this. My point is, a programmer would never use a 20 msec. " pause(20);"
    They would do something else for 20 msec then come back to the servo subroutine to update the servos. There are new programmers when they see a pause command take it to heart and do it. Other than a few NOPs, there should never be a pause used in a working program.
    "Never" (and for that matter "alwaysw") is a very long time.· What should a programmer do if there is nothing else to do?· Make up tasks for the processor to do just so there isn't a "pause"?

    Also, when you're trying to get something working for the first time, isn't it a good idea to keep things as simple as possible, and minimize the number of variables (not "program" variables but "things going on" variables) you're trying to deal with?

    Why do people write some type of "hello world" program when the first experience a new programming environment?· That would "never" be used in a working program.· They do it to make sure all the pieces (editor/compiler/assembler [noparse][[/noparse]and user])·are working.· It's probably good, and certainly·not bad, practice to do the same thing with any new device/object/etc.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.
    Click here to see my Nomad Build Log
Sign In or Register to comment.