Servo motor communicate with PIC16f877
jeremy1409
Posts: 2
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~~
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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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
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.
"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.
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.
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.
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