Check my code for controlling servo motors using PIC16F877A

Status
Not open for further replies.

gold12star

Junior Member level 3
Joined
Sep 27, 2010
Messages
28
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,451
hello
I face problame in controlling servo motor
I use pic16f877a programming by mikroC
I need the motor to move (0 , 90, 180) degree is the following code correct or not??
if not please help me as soon as possible
Code:
void main() {
portc=0b00000100;
trisc=0b00000000;
PWM1_Init(5000);
PWM1_Start();
PWM1_Set_Duty(255);
delay_us(1500);
PWM1_Start();
PWM1_Set_Duty(255);
delay_ms(2);
PWM1_Start();
PWM1_Set_Duty(255);
delay_ms(1);

}

thank you
 

Re: control servo motors

did you include the pic header file?
 
Re: control servo motors

I don't include pic header file
I also load different program without header file and work perfectly
any other advice??
 
Re: control servo motors

you should also tris first before port. Tris will set the i/o to whether it is input or output. Port will set te initial values to the respective port. =)
 
Re: control servo motors

thank you john blue
but it doesn't make any different
just move to 45 degree and stuck there
any other suggestion please:???:
 
Re: control servo motors

Try setting this: PWM1_Set_Duty(255); with different value

---------- Post added at 02:38 ---------- Previous post was at 02:36 ----------

Here is one sample:

// microcontroller : P16F877A
// PWM module is set on RC2 Pin No 17.

unsigned short i;

void main() {
PORTC = 00; // Set PORTC to $FF
TRISC = 0; // PORTC is output
Pwm_Init(5000); // Initialize PWM module

Pwm_Start(); // Start PWM

while (1) { // Endless loop
for(i=0;i<=255;i++)

{ Pwm1_Change_Duty(i);

delay_ms(10); }

} }
 

Re: control servo motors

thanks john blue for trying to help me
I appreciate that,but i try the sample code that you post and give me same as the before result .
I try different code without PWM it give me good result
Code:
void main() {
trisb=0b00000000;
while(1)
{
portb=0b10000000;
delay_ms(2);
portb=0b00000000;
delay_ms(20);
}
}
but if i use delay_us(2500) instede of delay_ms(2);
the servo motor not set on constant posetion and keep moving
any idea please??
also thanks alote
 

Hi

I do not program in C, but I do work with RC and servos.
Most servos have range of control from 1ms to 2ms with 1.5ms being the middle settings. (some can operate slightly outside these ranges). I would say that your range of 2500us could be outside the scope of the servo.

They normally have a complete cycle of 20ms (include PPM pulse).

I do not know if that will help.


John
 

thank you foa all
but until now I don't have solution
 

Re: control servo motors

Code:
void main() {
trisb=0b00000000;
while(1)
{
portb=0b10000000;
delay_ms(2);
portb=0b00000000;
delay_ms(20);
}
}

i think why it keep moving because your code is repeat the cycle..
use some "if" command and at a button switch to your hardware temporarily to try your code..
 

Re: control servo motors

Hi

I do not think the repeat is the issue, that is the way servo's work. They normal operate with complete cycle of 20ms, in a normal rx setup. Try dropping the second delay to 18ms in the code example to see if that resolves it.

You might see a slight jitter depending on acturate your clock is.
 

I try all advice
and at the end I think the power supply is the problem
but other problem is I can't reach 0 and 180 degree and I can reach 90 if the delay=1500 us??
I'm very happy when I see someone try to help me
thanks alote for everyone
 

Hi

The delay for 0 is 1000 us and 2000 us for 180.

HTH
 
thank you cubanflyer
I do what you said but I don't reach what I want
I think I keep the servo in the position it reach and complete the project
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…