help with using pwm in pic16f877a to control servo motor

Thread Starter

mastertiger

Joined Jul 9, 2012
24
hi every one
i need to control servo motor by pwm in pic 16f877a
i control it by normal loop
for(b=0;b<100;b++) // graper close
{
portb=0b00000001;
delay_us(3000);
portb=0;
delay_ms(20);
}
void main() {

trisc=0b00000010;
trisb=0;
trisb=0;

pwm2_init(1000);
pwm2_start();
pwm2_set_duty(1);

portb=0;
}
i need to control it by pwm
i try this code but run motor in one direction
help please
thanks
 

sbixby

Joined May 8, 2010
57
Typical servos run with a pulse width of approximately 1.5ms duration, repeating (roughly) every 20ms, with a typical operating range of around 1.0 ms to 2.0ms, and a typical max of 0.5 to 2.5ms. Most non-continuous-rotation servos will hit mechanical limits before 0.5/2.5, so sticking to the 1.0-2.0 range is your best bet.

If I read your code correctly, as I have no familiarity with PIC programming, you're sending a 3.0ms pulse to the servo, which would most likely overdrive it. If it's continuous-rotation it'll just be spinning one way, and if it's not continuous rotation, it probably fried your motor or amplifier by now.

Any which way - experiment with a center of 1.5ms and you should see something closer to centered for a regular servo and not rotating for a continuous servo.

My qualifications? I've used hobby servos for modelling for 25+ 32+ years, but only recently used a uC to drive a servo. Very liberating. :)
 
Last edited:

Thread Starter

mastertiger

Joined Jul 9, 2012
24
Typical servos run with a pulse width of approximately 1.5ms duration, repeating (roughly) every 20ms, with a typical operating range of around 1.0 ms to 2.0ms, and a typical max of 0.5 to 2.5ms. Most non-continuous-rotation servos will hit mechanical limits before 0.5/2.5, so sticking to the 1.0-2.0 range is your best bet.

If I read your code correctly, as I have no familiarity with PIC programming, you're sending a 3.0ms pulse to the servo, which would most likely overdrive it. If it's continuous-rotation it'll just be spinning one way, and if it's not continuous rotation, it probably fried your motor or amplifier by now.

Any which way - experiment with a center of 1.5ms and you should see something closer to centered for a regular servo and not rotating for a continuous servo.

My qualifications? I've used hobby servos for modelling for 25+ 32+ years, but only recently used a uC to drive a servo. Very liberating. :)
thanks for replay
1m to 2m s rotate servo in one direction
i used 3m to rotate it in other direction
its rotate good
only what i need to convert this code to run it by pwm rather than the normal pin
i need to do that because i want run servo and in the same the run some thing else by the same pic
pic read it sequence so i need use pwm
i use 16MHz external crystal for pic 16 f877a
 

sbixby

Joined May 8, 2010
57
Sorry to say, I have no knowledge of PIC programming at this time.

In general, I think you'll want to have a main loop which spins non-stop, and in the loop you call into the pulse code, then call into the rest of your code. Work out the timing such that you hit the pulse code approximately every 20ms, and the servo should be happy.

I toyed with the timing and have been able to get successful servo activity anywhere from 3-4ms between pulses on up to 25-30ms. However, a slower pulse rate will cause the servo to be somewhat jittery under load because it it's not "turned on" enough to hold it's position (or speed).

I'm curious what servo you're using that has a 2ms (?) center pulse width. My experience is limited to servos compatible with R/C hobby needs and those are definitely all 1.5ms centered, or they wouldn't be interchangable.
 
Top