|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Servo moving one way |
Posted: Thu Mar 26, 2009 5:08 am |
|
|
Hello. I plan to design a wiper using servo motor.
90degrees up, 90 degrees downwards.
However, I can't make it moving in both ways.
Only one way is all I can do.
Can you guys have a look at my program?
Code: |
#include <16F877A.h>
#device adc=10
#fuses HS,NOWDT,NOLVP,BROWNOUT,NOPROTECT
#use delay (clock=20000000)
#use rs232(baud=9600,uart1)
#byte PORTC = 7
#define servo (pin_c0)
void main()
{
//set_tris_c(0x00);
while(TRUE)
{
output_high(servo);
delay_us(1250);
output_low(servo);
delay_us(120);
delay_ms(1000);
output_high(servo);
delay_us(1250);
output_low(servo);
delay_us(120);
delay_ms(1000);
}
} |
thank you |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Thu Mar 26, 2009 8:06 am |
|
|
When you set the servo line low, you only leave it there for 120uS, yet when the servo line is high, you leave it in that state for 1250uS.
Come to think of it, do you really want uS? I think you probably want mS.
As you have it now, I don't think the motor even has a chance to respond to the 120uS. So it is actually seeing a series of 1250uS pulses. Probably is why you are only seeing it go one direction.
Good Luck. _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Sun Mar 29, 2009 7:49 pm |
|
|
Most hobby servos usually respond to the following sequence of signals - (a pulse between 1ms and 2ms), followed by (a 'rest' time of duration between 1ms-20ms).
This is repeated indefinitely to hold the servo at a certain position. The pulse length determines what the position is. Typically 1.5ms is the mean position ('0'), 1ms is -90, and 2ms is +90 degrees. This may vary slightly depending on the servo you buy (I once bought a servo that used to respond to pulses from 0.6ms to 2.4ms).
After providing the pulse you need to wait a short while - more than 1ms but less than 20ms, before providing the next pulse. It is the train of pulses that 'locks' the servo at a fixed position. In your code, after providing Code: | output_low(servo);
delay_us(120); | you delay for a further 1000ms. That prevents the servo from holding its position properly. Try this in the while loop:
Code: | while(TRUE)
{
output_high(servo);
delay_us(1250);
output_low(servo);
delay_ms(10);
} |
Rohit |
|
|
dbotkin
Joined: 08 Sep 2003 Posts: 197 Location: Omaha NE USA
|
|
Posted: Sun Mar 29, 2009 11:04 pm |
|
|
Or maybe do this...
Code: | void main(void) {
int position = 50; // 0 is fully CCW, 100 is fully CW
// Or is it the other way around? I
// don't remember.
while(TRUE) {
output_high(servo);
delay_ms(1);
delay_us(position*10);
output_low(servo);
delay_ms(20);
}
} |
Better yet, set up RTCC or some other timer to interrupt every 20ms and just do the pulse inside the ISR. Use the other 90-95% of your CPU time to do something useful like serial comms, watching switches, reading a position pot or solving world hunger. |
|
|
vert02
Joined: 23 Jan 2009 Posts: 8 Location: Kuala Lumpur, Malaysia
|
|
Posted: Tue May 12, 2009 9:15 am |
|
|
Hi guys...
I have managed to try and yes, it does move two ways.
Now I wanted to do 90 degrees downwards and upwards.
These are my coding:
Code: |
#include <16F877A.h>
#device adc=10
#fuses HS,NOWDT,NOLVP,BROWNOUT,NOPROTECT
#use delay (clock=20000000)
#use rs232(baud=9600,uart1)
#byte PORTE = 9
#define servo (pin_E2)
void main(void) {
int position = 50;
while(TRUE) {
output_high(servo);
delay_ms(1);
delay_us(position*180);
output_low(servo);
delay_ms(5);
}
} |
I could not achieve 90degrees...any ideas? |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|